opentelemetry-java-instrumentation icon indicating copy to clipboard operation
opentelemetry-java-instrumentation copied to clipboard

how to get attribute in Sampler?

Open kelystor opened this issue 3 years ago • 5 comments

I am developing a sampler to drop some traces that I don't need, e.g: redis ping/pong heartbeat connection. But I found I can't get the attribute I need in Sampler.shouldSample method:

public class XxxSampler implements Sampler {
SamplingResult shouldSample(
      Context parentContext,
      String traceId,
      String name,
      SpanKind spanKind,
      Attributes attributes,
      List<LinkData> parentLinks) {
            System.out.println("attributes.asMap() = " + attributes.asMap());
      }
}

I print all attributes, the output is:

attributes.asMap() = AttributesMap{data={db.system=redis, net.peer.name=localhost, net.transport=ip_tcp, net.peer.ip=127.0.0.1, net.peer.port=6379}, capacity=128, totalAddedValues=5}

there is no db.statement attribute, but there is db.statement attribute in final trace, jaeger snapshot:

image

I can't drop redis ping operation trace without db.statement attribute.

how can I get the missing attribute?any suggestion?

kelystor avatar Jul 01 '22 06:07 kelystor

Hey @kelystor , What version of lettuce are you using? The instrumentation that adds the span (lettuce-5.1) actually sets the attribute when the span ends; I think that this behavior could be improved for at least some versions (>=6.0, with the new start(RedisCommand) method)

mateuszrzeszutek avatar Jul 01 '22 07:07 mateuszrzeszutek

I'm using lettuce 5.3.1.RELEASE. I have tried to upgrade to 6.1.8.RELEASE, but the result is same, there is no db.statement attribute neither.

kelystor avatar Jul 01 '22 09:07 kelystor

I replace lettuce with redisson, and there is db.operation/db.statement attribute:

attributes.asMap() = AttributesMap{data={db.operation=PING, db.statement=PING, db.system=redis}, capacity=128, totalAddedValues=3}

but there are no net.peer.ipnet.peer.port attributes in Sampler.

the attributes in Sampler is different from final trace

kelystor avatar Jul 01 '22 10:07 kelystor

I replace lettuce with redisson, and there is db.operation/db.statement attribute:

attributes.asMap() = AttributesMap{data={db.operation=PING, db.statement=PING, db.system=redis}, capacity=128, totalAddedValues=3}

but there are no net.peer.ipnet.peer.port attributes in Sampler.

Yeah, unlike Lettuce Redisson does not expose the information needed to populate the net.peer.* attributes.

I'm using lettuce 5.3.1.RELEASE. I have tried to upgrade to 6.1.8.RELEASE, but the result is same, there is no db.statement attribute neither.

I think this could actually be fixed for 6.x: current instrumentation first starts the span, and later sets the statement (well, captures it): https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/82118868bfcebb24856e14ad88692d461643b1a1/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/OpenTelemetryTracing.java#L220-L230 This could be changed so that the db.statement is set before the span is started.

mateuszrzeszutek avatar Aug 16 '23 14:08 mateuszrzeszutek

I replace lettuce with redisson, and there is db.operation/db.statement attribute:

attributes.asMap() = AttributesMap{data={db.operation=PING, db.statement=PING, db.system=redis}, capacity=128, totalAddedValues=3}

but there are no net.peer.ipnet.peer.port attributes in Sampler.

Yeah, unlike Lettuce Redisson does not expose the information needed to populate the net.peer.* attributes.

I'm using lettuce 5.3.1.RELEASE. I have tried to upgrade to 6.1.8.RELEASE, but the result is same, there is no db.statement attribute neither.

I think this could actually be fixed for 6.x: current instrumentation first starts the span, and later sets the statement (well, captures it):

https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/82118868bfcebb24856e14ad88692d461643b1a1/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/OpenTelemetryTracing.java#L220-L230

This could be changed so that the db.statement is set before the span is started.

@mateuszrzeszutek At present, I also encounter the same problem when using agent2.4.0. I want to get db.statement condition filter span in Sampler. Is there any good processing method for me to get it

1095071913 avatar Jun 25 '24 08:06 1095071913

Since there has been no activity on this enhancement for the past year we are closing it to help maintain our backlog. Anyone who would like to work on it is still welcome to do so, and we can re-open it at that time.

github-actions[bot] avatar Sep 30 '25 09:09 github-actions[bot]