Skip to content
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

Unable to set a specific traceId to the start span when using the OpenTelemetry API in Dart #177

Open
Hridhay0316 opened this issue Jul 2, 2024 · 1 comment

Comments

@Hridhay0316
Copy link

Describe the bug
Unable to set a specific traceId to the start span when using the OpenTelemetry API in Dart

Steps to reproduce
1.Initialize the OpenTelemetry SDK and configure it with the necessary exporters.
2.Create a new span and attempt to set a specific traceId.
3.Start the span.
4.Observe the traceId of the started span in jeager

import 'package:opentelemetry/api.dart' as otel;
import 'package:opentelemetry/sdk.dart' as otel_sdk;

otel.Tracer? globalTracer;

void setupTracing() async {
final resource = otel_sdk.Resource(
[otel.Attribute.fromString('service.name', 'myproject')]);

final exporter = otel_sdk.CollectorExporter(
Uri.parse('Jeager URL'),
);
final processor = otel_sdk.BatchSpanProcessor(exporter);

final provider =
otel_sdk.TracerProviderBase(processors: [processor], resource: resource);

registerGlobalTracerProvider(provider);

globalTracer = provider.getTracer('myproject');

}

import 'package:opentelemetry/api.dart' as otel;

String? traceId = "8888bf2278369600d98ef29b6b628455";
String spanId =
"0000000000000001";
final spanContext = otel.SpanContext(
otel.TraceId.fromString(traceId),
otel.SpanId.fromString(spanId),
otel.TraceFlags.sampled,
otel.TraceState.empty(),
);
final spanKey = otel.ContextKey();
final newContext = otel.Context.current.setValue(spanKey, spanContext);
final span = globalTracer?.startSpan(
'fetchPolicyInfo',
context: newContext,
kind: otel.SpanKind.client,
);

What did you expect to see?
The span should start with the traceId set to the specific value provided

What did you see instead?
The span starts with a different traceId generated by the SDK, ignoring the provided traceId.

What version and what artifacts are you using?
Artifacts: opentelemetry
Version: v0.18.2
opentelemetry: ^0.18.2

Environment
Dart Version: 3.4.1
OS: Windows 11

@blakeroberts-wk
Copy link
Contributor

The ContextKey you are using to set a value in the Context isn't the same key that tracer.startSpan() looks for when setting the span's parent. You will want to use contextWithSpanContext(context, spanContext).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants