Skip to content

support multiValueHeaders #189

@rsteube

Description

@rsteube

Expected Behavior

Execute DataDog Synthetic Test and see trace linked to the executed request.

Actual Behavior

Traces are not linked.

Steps to Reproduce the Problem

In our case the headers are not passed via headers, but multiValueHeaders:

"multiValueHeaders": {
  "x-datadog-origin": [
    "synthetics"
  ],
  "x-datadog-parent-id": [
    "0"
  ],
  "x-datadog-sampling-priority": [
    "1"
  ],
  "x-datadog-trace-id": [
    "1775877486757059156"
  ],
}

func getHeadersFromEventHeaders(ctx context.Context, ev json.RawMessage) map[string]string {

It is possible to set a custom TraceContextExtractor in the config, but would be nice to have this supported out of the box.

//	lambda.Start(ddlambda.WrapFunction(proxiedHandler, &ddlambda.Config{
//		DDTraceEnabled:        true,
//		DebugLogging:          false,
//		TraceContextExtractor: getHeadersFromEventHeaders,
//	}))


type eventWithHeaders struct {
	MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
}

func getHeadersFromEventHeaders(ctx context.Context, ev json.RawMessage) map[string]string {
	eh := eventWithHeaders{}

	headers := map[string]string{}

	err := json.Unmarshal(ev, &eh)
	if err != nil {
		return headers
	}

	lowercaseHeaders := map[string]string{}
	for k, v := range eh.MultiValueHeaders {
		if len(v) > 0 {
			lowercaseHeaders[strings.ToLower(k)] = v[0]
		}
	}

	return lowercaseHeaders
}

Additionally we had to patch getTraceContext to include x-datadog-origin:

func getTraceContext(ctx context.Context, headers map[string]string) (TraceContext, bool) {

	tc[samplingPriorityHeader] = samplingPriority
	tc[traceIDHeader] = traceID
	tc[parentIDHeader] = parentID
	if origin, ok := headers["x-datadog-origin"]; ok {
		tc["x-datadog-origin"] = origin
	}

	return tc, true

Otherwise it is missing for the check in extractTextMap (no origin, thus ErrSpanContextNotFound):

https://github.com/DataDog/dd-trace-go/blob/fb51babc4beed2e0037c41df97848313e7e43af3/ddtrace/tracer/textmap.go#L557

Specifications

  • Datadog Go Lambda package version: 1.20.0
  • Go version: 1.24.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions