Skip to content

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

Open
@Hridhay0316

Description

@Hridhay0316

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

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