Enhancement/add additionalServerExtractors (#7155)
fixes issue #7153 Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
This commit is contained in:
parent
d4b29cf521
commit
deebf6d06c
|
@ -48,6 +48,8 @@ public final class GrpcTelemetryBuilder {
|
|||
additionalExtractors = new ArrayList<>();
|
||||
private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
|
||||
additionalClientExtractors = new ArrayList<>();
|
||||
private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
|
||||
additionalServerExtractors = new ArrayList<>();
|
||||
|
||||
private boolean captureExperimentalSpanAttributes;
|
||||
|
||||
|
@ -78,6 +80,18 @@ public final class GrpcTelemetryBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an extra server-only {@link AttributesExtractor} to invoke to set attributes to
|
||||
* instrumented items. The {@link AttributesExtractor} will be executed after all default
|
||||
* extractors.
|
||||
*/
|
||||
@CanIgnoreReturnValue
|
||||
public GrpcTelemetryBuilder addServerAttributeExtractor(
|
||||
AttributesExtractor<? super GrpcRequest, ? super Status> attributesExtractor) {
|
||||
additionalServerExtractors.add(attributesExtractor);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Sets custom client {@link SpanNameExtractor} via transform function. */
|
||||
@CanIgnoreReturnValue
|
||||
public GrpcTelemetryBuilder setClientSpanNameExtractor(
|
||||
|
@ -154,6 +168,7 @@ public final class GrpcTelemetryBuilder {
|
|||
.addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter))
|
||||
.addAttributesExtractor(
|
||||
NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter()))
|
||||
.addAttributesExtractors(additionalServerExtractors)
|
||||
.addOperationMetrics(RpcServerMetrics.get());
|
||||
|
||||
if (peerService != null) {
|
||||
|
|
|
@ -35,6 +35,7 @@ class GrpcTest extends AbstractGrpcTest {
|
|||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();
|
||||
|
||||
private static final AttributeKey<String> CUSTOM_KEY = AttributeKey.stringKey("customKey");
|
||||
private static final AttributeKey<String> CUSTOM_KEY2 = AttributeKey.stringKey("customKey2");
|
||||
|
||||
private static final Metadata.Key<String> CUSTOM_METADATA_KEY =
|
||||
Metadata.Key.of("customMetadataKey", Metadata.ASCII_STRING_MARSHALLER);
|
||||
|
@ -56,6 +57,11 @@ class GrpcTest extends AbstractGrpcTest {
|
|||
return testing;
|
||||
}
|
||||
|
||||
/**
|
||||
* metadataProvided. testing as well 2 extra methods: {@link
|
||||
* GrpcTelemetryBuilder#addClientAttributeExtractor} and {@link
|
||||
* GrpcTelemetryBuilder#addServerAttributeExtractor}
|
||||
*/
|
||||
@Test
|
||||
void metadataProvided() throws Exception {
|
||||
BindableService greeter =
|
||||
|
@ -76,6 +82,7 @@ class GrpcTest extends AbstractGrpcTest {
|
|||
.intercept(
|
||||
GrpcTelemetry.builder(testing.getOpenTelemetry())
|
||||
.addAttributeExtractor(new CustomAttributesExtractor())
|
||||
.addServerAttributeExtractor(new CustomAttributesExtractorV2("serverSideValue"))
|
||||
.build()
|
||||
.newServerInterceptor())
|
||||
.build()
|
||||
|
@ -87,6 +94,8 @@ class GrpcTest extends AbstractGrpcTest {
|
|||
.intercept(
|
||||
GrpcTelemetry.builder(testing.getOpenTelemetry())
|
||||
.addAttributeExtractor(new CustomAttributesExtractor())
|
||||
.addClientAttributeExtractor(
|
||||
new CustomAttributesExtractorV2("clientSideValue"))
|
||||
.build()
|
||||
.newClientInterceptor()));
|
||||
|
||||
|
@ -117,11 +126,13 @@ class GrpcTest extends AbstractGrpcTest {
|
|||
span.hasName("example.Greeter/SayHello")
|
||||
.hasKind(SpanKind.CLIENT)
|
||||
.hasParent(trace.getSpan(0))
|
||||
.hasAttribute(CUSTOM_KEY2, "clientSideValue")
|
||||
.hasAttribute(CUSTOM_KEY, "customValue"),
|
||||
span ->
|
||||
span.hasName("example.Greeter/SayHello")
|
||||
.hasKind(SpanKind.SERVER)
|
||||
.hasParent(trace.getSpan(1))
|
||||
.hasAttribute(CUSTOM_KEY2, "serverSideValue")
|
||||
.hasAttribute(CUSTOM_KEY, "customValue")));
|
||||
}
|
||||
|
||||
|
@ -149,4 +160,29 @@ class GrpcTest extends AbstractGrpcTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class CustomAttributesExtractorV2
|
||||
implements AttributesExtractor<GrpcRequest, Status> {
|
||||
|
||||
private final String valueOfKey2;
|
||||
|
||||
public CustomAttributesExtractorV2(String valueOfKey2) {
|
||||
this.valueOfKey2 = valueOfKey2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(
|
||||
AttributesBuilder attributes, Context parentContext, GrpcRequest grpcRequest) {
|
||||
|
||||
attributes.put(CUSTOM_KEY2, valueOfKey2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnd(
|
||||
AttributesBuilder attributes,
|
||||
Context context,
|
||||
GrpcRequest grpcRequest,
|
||||
@Nullable Status status,
|
||||
@Nullable Throwable error) {}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue