Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/add additionalServerExtractors #7155

Merged
merged 5 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,6 +80,18 @@ public GrpcTelemetryBuilder addClientAttributeExtractor(
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(
Expand Down Expand Up @@ -154,6 +168,7 @@ public GrpcTelemetry build() {
.addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter))
.addAttributesExtractor(
NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter()))
.addAttributesExtractors(additionalServerExtractors)
.addOperationMetrics(RpcServerMetrics.get());

if (peerService != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -56,6 +57,11 @@ protected InstrumentationExtension testing() {
return testing;
}

/**
* metadataProvided. testing as well 2 extra methods: {@link
* GrpcTelemetryBuilder#addClientAttributeExtractor} and {@link
* GrpcTelemetryBuilder#addServerAttributeExtractor}
*/
@Test
void metadataProvided() throws Exception {
BindableService greeter =
Expand All @@ -76,6 +82,7 @@ public void sayHello(
.intercept(
GrpcTelemetry.builder(testing.getOpenTelemetry())
.addAttributeExtractor(new CustomAttributesExtractor())
.addServerAttributeExtractor(new CustomAttributesExtractorV2("serverSideValue"))
.build()
.newServerInterceptor())
.build()
Expand All @@ -87,6 +94,8 @@ public void sayHello(
.intercept(
GrpcTelemetry.builder(testing.getOpenTelemetry())
.addAttributeExtractor(new CustomAttributesExtractor())
.addClientAttributeExtractor(
new CustomAttributesExtractorV2("clientSideValue"))
.build()
.newClientInterceptor()));

Expand Down Expand Up @@ -117,11 +126,13 @@ public void sayHello(
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")));
}

Expand Down Expand Up @@ -149,4 +160,29 @@ public void onEnd(
}
}
}

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) {}
}
}