Skip to content

Commit

Permalink
baggage consistently a map now
Browse files Browse the repository at this point in the history
cleanup + ver bump

add back in some fallback logic for baggage

make generate

some logic fixes in tracing.go

generate
  • Loading branch information
ChrisScotMartin committed Dec 17, 2024
1 parent fa24ebe commit e0d9afd
Show file tree
Hide file tree
Showing 42 changed files with 475 additions and 1,856 deletions.
4 changes: 3 additions & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
v9.5.1
v9.5.2

v9.5.2 Bug fix in baggage options for JS client, cleanup tracing.go a bit

v9.5.1 Minor bug fix in the typing of options in the generated JS client

Expand Down
37 changes: 20 additions & 17 deletions _hardcoded/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"time"

"github.com/google/uuid"

"github.com/Clever/kayvee-go/v7/logger"

"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
Expand Down Expand Up @@ -121,19 +123,19 @@ func MuxServerMiddleware(serviceName string) func(http.Handler) http.Handler {
}
ctx := r.Context()

var crid string

s := trace.SpanFromContext(ctx)
crid = s.SpanContext().TraceID().String()
bags := baggage.FromContext(ctx)
// Add clever-request-id to baggage
member, err := baggage.NewMember("clever-request-id", crid)
if err != nil {
logger.FromContext(ctx).WarnD("error-creating-baggage", logger.M{"error": err.Error()})
} else {
bags, err = bags.SetMember(member)

if bags.Member("clever-request-id").String() == "=" { // if clever-request-id is not set
reqid, err := baggage.NewMember("clever-request-id", uuid.New().String())
if err != nil {
logger.FromContext(ctx).WarnD("error-creating-baggage", logger.M{"error": err.Error()})
logger.FromContext(ctx).ErrorD("error creating baggage member", logger.M{"error": err.Error()})
} else {
bags, err = bags.SetMember(reqid)
if err != nil {
logger.FromContext(ctx).ErrorD("error setting baggage member", logger.M{"error": err.Error()})
}

}
}

Expand All @@ -145,15 +147,16 @@ func MuxServerMiddleware(serviceName string) func(http.Handler) http.Handler {
// Add baggage to the context
ctx = baggage.ContextWithBaggage(ctx, bags)

// Log if sampled
if s.SpanContext().IsSampled() {
logger.FromContext(ctx).AddContext("sampled", "true")
} else {
logger.FromContext(ctx).AddContext("sampled", "false")
}

// Encode the trace/span ids in the DD format
if sc := s.SpanContext(); sc.HasTraceID() {

// Log if sampled
if s.SpanContext().IsSampled() {
logger.FromContext(ctx).AddContext("sampled", "true")
} else {
logger.FromContext(ctx).AddContext("sampled", "false")
}

spanID, traceID := sc.SpanID().String(), sc.TraceID().String()
// datadog converts hex strings to uint64 IDs, so log those so that correlating logs and traces works
if len(traceID) == 32 && len(spanID) == 16 { // opentelemetry format: 16 byte (32-char hex), 8 byte (16-char hex) trace and span ids
Expand Down
50 changes: 17 additions & 33 deletions clients/js/genjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ const RollingNumberEvent = require("hystrixjs/lib/metrics/RollingNumberEvent");
const { Errors } = require("./types");
function parseForBaggage(entries) {
if (!entries) {
return "";
}
// Regular expression for valid characters in keys and values
const validChars = /^[a-zA-Z0-9!#$%&'*+` + "`\\-.^_`" + `|~]+$/;
// Transform the entries object into an array of strings
const baggageItems = Object.entries(entries).map(([key, value]) => {
// Remove invalid characters from key and value
const pairs = [];
entries.forEach((value, key) => {
const validKey = key.match(validChars) ? key : encodeURIComponent(key);
const validValue = value.match(validChars) ? value : encodeURIComponent(value);
return ` + "`${validKey}=${validValue}`" + `;
pairs.push(` + "`" + `${validKey}=${validValue}` + "`" + `);
});
// Combine the array of strings into the final baggageString
const baggageString = baggageItems.join(',');
return baggageString;
return pairs.join(",");
}
/**
Expand Down Expand Up @@ -429,30 +429,14 @@ const methodTmplStr = `
options = {};
}
const optionsBaggage = options.baggage || {}
const optionsBaggage = options.baggage || new Map();
const timeout = options.timeout || this.timeout;
const headers = {};
let headers = {};
if (headers["baggage"]) {
const existingBaggageItems = headers["baggage"].split(',');
const existingBaggage = {};
for (const item of existingBaggageItems) {
const [key, value] = item.split('=');
existingBaggage[key] = value;
}
// Merge existingBaggage and optionsBaggage. Values in optionsBaggage will overwrite those in existingBaggage.
const mergedBaggage = {...existingBaggage, ...optionsBaggage};
// Convert mergedBaggage back into a string using parseForBaggage
headers["baggage"] = parseForBaggage(mergedBaggage);
} else {
// Convert optionsBaggage into a string using parseForBaggage
headers["baggage"] = parseForBaggage(optionsBaggage);
}
// Convert optionsBaggage into a string using parseForBaggage
headers["baggage"] = parseForBaggage(optionsBaggage);
headers["Canonical-Resource"] = "{{.Operation}}";
headers[versionHeader] = version;
Expand Down Expand Up @@ -616,7 +600,7 @@ const singleParamMethodDefinitionTemplateString = `/**{{if .Description}}
* @param {{if $param.JSDocType}}{{.JSDocType}} {{end}}{{$param.JSName}}{{if $param.Default}}={{$param.Default}}{{end}}{{if $param.Description}} - {{.Description}}{{end}}{{end}}
* @param {object} [options]
* @param {number} [options.timeout] - A request specific timeout
* @param {object} [options.baggage] - A request specific baggage to be propagated
* @param {Map<string, string | number>} [options.baggage] - A request-specific baggage to be propagated
* @param {module:{{.ServiceName}}.RetryPolicies} [options.retryPolicy] - A request specific retryPolicy
{{- if .IterMethod}}
* @returns {Object} iter
Expand Down Expand Up @@ -654,7 +638,7 @@ const pluralParamMethodDefinitionTemplateString = `/**{{if .Description}}
* @param {{if $param.JSDocType}}{{.JSDocType}} {{end}}{{if not $param.Required}}[{{end}}params.{{$param.JSName}}{{if $param.Default}}={{$param.Default}}{{end}}{{if not $param.Required}}]{{end}}{{if $param.Description}} - {{.Description}}{{end}}{{end}}
* @param {object} [options]
* @param {number} [options.timeout] - A request specific timeout
* @param {object} [options.baggage] - A request specific baggage to be propagated
* @param {Map<string, string | number>} [options.baggage] - A request-specific baggage to be propagated
* @param {module:{{.ServiceName}}.RetryPolicies} [options.retryPolicy] - A request specific retryPolicy
{{- if .IterMethod}}
* @returns {Object} iter
Expand Down Expand Up @@ -1377,7 +1361,7 @@ interface RetryPolicy {
interface RequestOptions {
timeout?: number;
baggage?: object;
baggage?: Map<string, string | number>;
retryPolicy?: RetryPolicy;
}
Expand All @@ -1398,7 +1382,7 @@ interface CircuitOptions {
interface GenericOptions {
timeout?: number;
baggage?: object;
baggage?: Map<string, string | number>;
keepalive?: boolean;
retryPolicy?: RetryPolicy;
logger?: Logger;
Expand Down
6 changes: 3 additions & 3 deletions hardcoded/hardcoded.go

Large diffs are not rendered by default.

31 changes: 15 additions & 16 deletions samples/gen-go-basic/servertracing/tracing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 15 additions & 16 deletions samples/gen-go-blog/servertracing/tracing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 15 additions & 16 deletions samples/gen-go-client-only/servertracing/tracing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e0d9afd

Please sign in to comment.