-
Notifications
You must be signed in to change notification settings - Fork 177
Add B3 multi header propagation support #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1c339b5
3fc6239
ca6ec64
1cfa98b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -143,6 +143,9 @@ OtelGetTraceId(ngx_http_request_t* req, ngx_http_variable_value_t* v, uintptr_t | |||||
| static ngx_int_t | ||||||
| OtelGetSpanId(ngx_http_request_t* req, ngx_http_variable_value_t* v, uintptr_t data); | ||||||
|
|
||||||
| static ngx_int_t | ||||||
| OtelGetSampled(ngx_http_request_t* req, ngx_http_variable_value_t* v, uintptr_t data); | ||||||
|
|
||||||
| static ngx_http_variable_t otel_ngx_variables[] = { | ||||||
| { | ||||||
| ngx_string("otel_ctx"), | ||||||
|
|
@@ -176,6 +179,14 @@ static ngx_http_variable_t otel_ngx_variables[] = { | |||||
| NGX_HTTP_VAR_NOCACHEABLE | NGX_HTTP_VAR_NOHASH, | ||||||
| 0, | ||||||
| }, | ||||||
| { | ||||||
| ngx_string("opentelemetry_sampled"), | ||||||
| nullptr, | ||||||
| OtelGetSampled, | ||||||
| 0, | ||||||
| NGX_HTTP_VAR_NOCACHEABLE | NGX_HTTP_VAR_NOHASH, | ||||||
| 0, | ||||||
| }, | ||||||
| ngx_http_null_variable, | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -224,6 +235,46 @@ nostd::string_view WithoutOtelVarPrefix(ngx_str_t value) { | |||||
| return {(const char*)value.data + prefixLength, value.len - prefixLength}; | ||||||
| } | ||||||
|
|
||||||
| static ngx_int_t | ||||||
| OtelGetSampled(ngx_http_request_t* req, ngx_http_variable_value_t* v, uintptr_t data) { | ||||||
| (void)data; | ||||||
|
|
||||||
| if (!IsOtelEnabled(req)) { | ||||||
| v->valid = 0; | ||||||
| v->not_found = 1; | ||||||
| return NGX_OK; | ||||||
| } | ||||||
|
|
||||||
| TraceContext* traceContext = GetTraceContext(req); | ||||||
|
|
||||||
| if (traceContext == nullptr || !traceContext->request_span) { | ||||||
| ngx_log_error( | ||||||
| NGX_LOG_ERR, req->connection->log, 0, | ||||||
| "Unable to get trace context when getting span id"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, can be ignored if doesn't look good -
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've signed the CLA, but for some reason I'm getting an EasyCLA error when pushing to this branch, will change the wording in another PR. |
||||||
| return NGX_OK; | ||||||
| } | ||||||
|
|
||||||
| trace::SpanContext spanContext = traceContext->request_span->GetContext(); | ||||||
|
|
||||||
| if (spanContext.IsValid()) { | ||||||
| u_char* isSampled = spanContext.trace_flags().IsSampled() ? (u_char*) "1" : (u_char*) "0"; | ||||||
|
|
||||||
| v->len = 1; | ||||||
| v->valid = 1; | ||||||
| v->no_cacheable = 1; | ||||||
| v->not_found = 0; | ||||||
| v->data = isSampled; | ||||||
| } else { | ||||||
| v->len = 0; | ||||||
| v->valid = 0; | ||||||
| v->no_cacheable = 1; | ||||||
| v->not_found = 1; | ||||||
| v->data = nullptr; | ||||||
| } | ||||||
|
|
||||||
| return NGX_OK; | ||||||
| } | ||||||
|
|
||||||
| static ngx_int_t | ||||||
| OtelGetTraceContextVar(ngx_http_request_t* req, ngx_http_variable_value_t* v, uintptr_t data) { | ||||||
| if (!IsOtelEnabled(req)) { | ||||||
|
|
@@ -778,6 +829,17 @@ std::vector<HeaderPropagation> B3PropagationVars() { | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| std::vector<HeaderPropagation> B3MultiPropagationVars() { | ||||||
| return { | ||||||
| {"proxy_set_header", "x-b3-traceid", "$opentelemetry_trace_id"}, | ||||||
| {"proxy_set_header", "x-b3-spanid", "$opentelemetry_span_id"}, | ||||||
| {"proxy_set_header", "x-b3-sampled", "$opentelemetry_sampled"}, | ||||||
| {"fastcgi_param", "HTTP_B3_TRACEID", "$opentelemetry_trace_id"}, | ||||||
| {"fastcgi_param", "HTTP_B3_SPANID", "$opentelemetry_span_id"}, | ||||||
| {"fastcgi_param", "HTTP_B3_SAMPLED", "$opentelemetry_sampled"}, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| std::vector<HeaderPropagation> OtelPropagationVars() { | ||||||
| return { | ||||||
| {"proxy_set_header", "traceparent", "$opentelemetry_context_traceparent"}, | ||||||
|
|
@@ -798,6 +860,8 @@ char* OtelNgxSetPropagation(ngx_conf_t* conf, ngx_command_t*, void* locConf) { | |||||
|
|
||||||
| if (propagationType == "b3") { | ||||||
| locationConf->propagationType = TracePropagationB3; | ||||||
| } else if (propagationType == "b3multi") { | ||||||
| locationConf->propagationType = TracePropagationB3Multi; | ||||||
| } else if (propagationType == "w3c") { | ||||||
| locationConf->propagationType = TracePropagationW3C; | ||||||
| } else { | ||||||
|
|
@@ -811,6 +875,8 @@ char* OtelNgxSetPropagation(ngx_conf_t* conf, ngx_command_t*, void* locConf) { | |||||
| std::vector<HeaderPropagation> propagationVars; | ||||||
| if (locationConf->propagationType == TracePropagationB3) { | ||||||
| propagationVars = B3PropagationVars(); | ||||||
| } else if (locationConf->propagationType == TracePropagationB3Multi) { | ||||||
| propagationVars = B3MultiPropagationVars(); | ||||||
| } else { | ||||||
| propagationVars = OtelPropagationVars(); | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| $b3_trace_id = $_SERVER["HTTP_B3_TRACEID"]; | ||
| $b3_span_id = $_SERVER["HTTP_B3_SPANID"]; | ||
| $b3_sampled = $_SERVER["HTTP_B3_SAMPLED"]; | ||
|
|
||
| if (!preg_match("/^([0-9a-f]{32}|[0-9a-f]{16})$/", $b3_trace_id)) { | ||
| throw new Exception("invalid or missing x-b3-traceid header"); | ||
| } | ||
|
|
||
| if (!preg_match("/^[0-9a-f]{16}$/", $b3_span_id)) { | ||
| throw new Exception("invalid or missing x-b3-spanid header"); | ||
| } | ||
|
|
||
| if (!preg_match("/^[0-1]$/", $b3_sampled)) { | ||
| throw new Exception("invalid or missing x-b3-sampled header"); | ||
| } | ||
|
|
||
| header("Content-Type: application/json"); | ||
| echo(json_encode(array( | ||
| "x-b3-traceid" => $b3_trace_id, | ||
| "x-b3-spanid" => $b3_span_id, | ||
| "x-b3-sampled" => $b3_sampled | ||
| ))); | ||
| ?> |
Uh oh!
There was an error while loading. Please reload this page.