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<>();
|
additionalExtractors = new ArrayList<>();
|
||||||
private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
|
private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
|
||||||
additionalClientExtractors = new ArrayList<>();
|
additionalClientExtractors = new ArrayList<>();
|
||||||
|
private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
|
||||||
|
additionalServerExtractors = new ArrayList<>();
|
||||||
|
|
||||||
private boolean captureExperimentalSpanAttributes;
|
private boolean captureExperimentalSpanAttributes;
|
||||||
|
|
||||||
|
@ -78,6 +80,18 @@ public final class GrpcTelemetryBuilder {
|
||||||
return this;
|
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. */
|
/** Sets custom client {@link SpanNameExtractor} via transform function. */
|
||||||
@CanIgnoreReturnValue
|
@CanIgnoreReturnValue
|
||||||
public GrpcTelemetryBuilder setClientSpanNameExtractor(
|
public GrpcTelemetryBuilder setClientSpanNameExtractor(
|
||||||
|
@ -154,6 +168,7 @@ public final class GrpcTelemetryBuilder {
|
||||||
.addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter))
|
.addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter))
|
||||||
.addAttributesExtractor(
|
.addAttributesExtractor(
|
||||||
NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter()))
|
NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter()))
|
||||||
|
.addAttributesExtractors(additionalServerExtractors)
|
||||||
.addOperationMetrics(RpcServerMetrics.get());
|
.addOperationMetrics(RpcServerMetrics.get());
|
||||||
|
|
||||||
if (peerService != null) {
|
if (peerService != null) {
|
||||||
|
|
|
@ -35,6 +35,7 @@ class GrpcTest extends AbstractGrpcTest {
|
||||||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();
|
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();
|
||||||
|
|
||||||
private static final AttributeKey<String> CUSTOM_KEY = AttributeKey.stringKey("customKey");
|
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 =
|
private static final Metadata.Key<String> CUSTOM_METADATA_KEY =
|
||||||
Metadata.Key.of("customMetadataKey", Metadata.ASCII_STRING_MARSHALLER);
|
Metadata.Key.of("customMetadataKey", Metadata.ASCII_STRING_MARSHALLER);
|
||||||
|
@ -56,6 +57,11 @@ class GrpcTest extends AbstractGrpcTest {
|
||||||
return testing;
|
return testing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* metadataProvided. testing as well 2 extra methods: {@link
|
||||||
|
* GrpcTelemetryBuilder#addClientAttributeExtractor} and {@link
|
||||||
|
* GrpcTelemetryBuilder#addServerAttributeExtractor}
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
void metadataProvided() throws Exception {
|
void metadataProvided() throws Exception {
|
||||||
BindableService greeter =
|
BindableService greeter =
|
||||||
|
@ -76,6 +82,7 @@ class GrpcTest extends AbstractGrpcTest {
|
||||||
.intercept(
|
.intercept(
|
||||||
GrpcTelemetry.builder(testing.getOpenTelemetry())
|
GrpcTelemetry.builder(testing.getOpenTelemetry())
|
||||||
.addAttributeExtractor(new CustomAttributesExtractor())
|
.addAttributeExtractor(new CustomAttributesExtractor())
|
||||||
|
.addServerAttributeExtractor(new CustomAttributesExtractorV2("serverSideValue"))
|
||||||
.build()
|
.build()
|
||||||
.newServerInterceptor())
|
.newServerInterceptor())
|
||||||
.build()
|
.build()
|
||||||
|
@ -87,6 +94,8 @@ class GrpcTest extends AbstractGrpcTest {
|
||||||
.intercept(
|
.intercept(
|
||||||
GrpcTelemetry.builder(testing.getOpenTelemetry())
|
GrpcTelemetry.builder(testing.getOpenTelemetry())
|
||||||
.addAttributeExtractor(new CustomAttributesExtractor())
|
.addAttributeExtractor(new CustomAttributesExtractor())
|
||||||
|
.addClientAttributeExtractor(
|
||||||
|
new CustomAttributesExtractorV2("clientSideValue"))
|
||||||
.build()
|
.build()
|
||||||
.newClientInterceptor()));
|
.newClientInterceptor()));
|
||||||
|
|
||||||
|
@ -117,11 +126,13 @@ class GrpcTest extends AbstractGrpcTest {
|
||||||
span.hasName("example.Greeter/SayHello")
|
span.hasName("example.Greeter/SayHello")
|
||||||
.hasKind(SpanKind.CLIENT)
|
.hasKind(SpanKind.CLIENT)
|
||||||
.hasParent(trace.getSpan(0))
|
.hasParent(trace.getSpan(0))
|
||||||
|
.hasAttribute(CUSTOM_KEY2, "clientSideValue")
|
||||||
.hasAttribute(CUSTOM_KEY, "customValue"),
|
.hasAttribute(CUSTOM_KEY, "customValue"),
|
||||||
span ->
|
span ->
|
||||||
span.hasName("example.Greeter/SayHello")
|
span.hasName("example.Greeter/SayHello")
|
||||||
.hasKind(SpanKind.SERVER)
|
.hasKind(SpanKind.SERVER)
|
||||||
.hasParent(trace.getSpan(1))
|
.hasParent(trace.getSpan(1))
|
||||||
|
.hasAttribute(CUSTOM_KEY2, "serverSideValue")
|
||||||
.hasAttribute(CUSTOM_KEY, "customValue")));
|
.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