open-telemetry / opentelemetry-java-instrumentation

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

Sampler.shouldSample in Redis Span The Attributes for the method parameter cannot be obtained. Why #11639

Closed 1095071913 closed 2 days ago

1095071913 commented 1 week ago

Describe the bug

Sampler.shouldSample in Redis Span The Attributes for the method parameter cannot be obtained. Why

original:

thread.name=lettuce-nioEventLoop-5-3, thread.id=117, network.type=ipv4, network.peer.port=6379, server.port=6379, db.statement=INFO server, db.system=redis, network.peer.address=127.0.0.1, server.address=redis

Attributes parameter in Sampler.shouldSample: network.type=ipv4 network.peer.port=6379 server.port=6379 db.system=redis network.peer.address=127.0.0.1 server.address=redis

Why can't I get some values, such as db.statement

Steps to reproduce

`public class FilterSampler implements Sampler {

@Override
public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {

    System.out.println("name:"+name);

    attributes.forEach((key,value)->{
        System.out.println(key+":"+value);
    });

    return SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE);

}

@Override
public String getDescription() {
    return "FilterSampler";
}

}`

Expected behavior

I want to filter some spans, such as db.statement=INFO server or db.statement=PING

Actual behavior

.

Javaagent or library instrumentation version

https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/tag/v2.4.0

Environment

sampler project environment

`

io.opentelemetry.javaagent
  <artifactId>opentelemetry-javaagent</artifactId>
  <version>2.4.0</version>
</dependency>

<dependency>
  <groupId>io.opentelemetry</groupId>
  <artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
  <version>1.38.0</version>
</dependency>

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-tracing-bridge-otel</artifactId>
  <version>1.3.1</version>
</dependency>`

Additional context

No response

laurit commented 1 week ago

In sampler you can only use the attributes that are available when the span is started. The db.statement attribute you wish to use is set later.

1095071913 commented 4 days ago

在采样器中,您只能使用跨度启动时可用的属性。您要使用的属性db.statement稍后设置。

@laurit Sorry to interrupt. How should the db.statement attribute be set

laurit commented 4 days ago

Have a look at https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/OpenTelemetryTracing.java For the attribute to be available for sampling it should be set on the SpanBuilder before calling startSpan. As you can see the way OpenTelemetryTracing behaves is different for lettuce 6+ and earlier versions. It seems to me that the 6+ support could be adapted to set db.statement before starting the span. Alternatively you could try doing the sampling in the collector.

1095071913 commented 3 days ago

Have a look at https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/OpenTelemetryTracing.java For the attribute to be available for sampling it should be set on the SpanBuilder before calling startSpan. As you can see the way OpenTelemetryTracing behaves is different for lettuce 6+ and earlier versions. It seems to me that the 6+ support could be adapted to set db.statement before starting the span. Alternatively you could try doing the sampling in the collector.

@laurit I have a question, will lettuce open db.statement in future versions? At the moment there is redission and there is no lettuce, which would be a bit strange

laurit commented 3 days ago

@laurit I have a question, will lettuce open db.statement in future versions?

It is possible if someone submits a PR.

At the moment there is redission and there is no lettuce, which would be a bit strange

I don't follow this

1095071913 commented 2 days ago

@laurit I have a question, will lettuce open db.statement in future versions?

It is possible if someone submits a PR.

At the moment there is redission and there is no lettuce, which would be a bit strange

I don't follow this

Thanks for the help, through your program has been solved