open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.9k stars 826 forks source link

How to generate a otel Context/SpanContext based on a string like trace_id:span_id #8358

Closed wys199703 closed 1 year ago

wys199703 commented 1 year ago

I want to extract a otel Context/SpanContext from a String format like "trace_id:span_id". In the legacy jaegertracing implementation we can get it form io.jaegertracing.internal.propagation.TextMapCodec.contextFromString(String). So I wonder is there a similar way to do this in opentemeletry-java. I checked opentelemetry-instrumentation-java has four propagators. Seems none to extract from a String. image

Is there any way to do this ?

my maven opentelemetry dependencies are :

`

io.opentelemetry.instrumentation
            <artifactId>opentelemetry-jaeger-spring-boot-starter</artifactId>
            <version>1.25.0-alpha</version>
        </dependency>`

        `<dependency>
            <groupId>io.opentelemetry.instrumentation</groupId>
            <artifactId>opentelemetry-logback-mdc-1.0</artifactId>
            <version>1.25.0-alpha</version>
        </dependency>`
laurit commented 1 year ago

You could extract trace and span id from the string yourself and set up the context like it is done in https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/1a68f4d07d590a54d09aee8f233d8ffa1720ae97/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/LocalRootSpanTest.java#L38-L45 Otel propagators extract from a map. You can put the string you have into a map and in the propagator use the same key. Something like https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/1a68f4d07d590a54d09aee8f233d8ffa1720ae97/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/SqsParentContext.java#L34-L38

wys199703 commented 1 year ago

@laurit Thanks! I will try and chekc it.

Here is another question about gRPC request.

I brought up this issue as my project write/read trace info to/from request/response header manually. I found opentelemetry instrumentation has a module named 'opentelemetry-grpc-1.6'. Does it has some likely function on grpc request/response inject/extract? And maybe do some customize header.

laurit commented 1 year ago

Our grpc library instrumentation has context propagation support. If this is what your question was.

wys199703 commented 1 year ago

@laurit Yes, I checked that propagator extracts trace from header 'uber-trace-id'. And does otel-java support to filter which requests with specefied expressions not to be traced? Like skippattern definition in jaegertracing

laurit commented 1 year ago

@wys199703 it is possible to write a sampler for this, for example see https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/samplers/src/main/java/io/opentelemetry/contrib/sampler

wys199703 commented 1 year ago

Hi @laurit, Maybe there is something misunderstanding about my question. Actually my situation is I do not want to generate a otel context for some specefied http paths like '/actuator/*' but not in the export stage. I've read the code of 'WebMvcTelemetryProducingFilter' and does that can be done in this filter‘s judgement statement 'this.instrumenter.shouldStart(parentContext, request)' by adding the path pattern match judgement? image

trask commented 1 year ago

@wys199703 maybe discussion on https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/7906 is relevant for you?

wys199703 commented 1 year ago

Hi @trask I checked that issue. Maybe it met my issue. Actually I'm using otel-instrumentation by a otel springboot starter. So I need to replace the default sampler with a self managed 'RuleBasedRoutingSampler'. is that correct?

And another question is I also use otel 'opentelemetry-logback-mdc-1.0' to auto inject trace_id & span_id to my log line. For my understanding is all requests which return true in below judgement 'this.instrumenter.shouldStart(parentContext, request)' will generate trace context in the 'WebMvcTelemetryProducingFilter' and inject the trace info to log line, even through whose trace will not export to the backend. Is that correct? image

BTW. I want to confirm does otel can also propagate from a 64 bits trace id by padding zero to the hex high 16~31 like below example ? SpanContext.createFromRemoteParent( "00000000000000000000000000000001", "0000000000000002", TraceFlags.getSampled(), TraceState.getDefault())))