-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add dual-semconv support to RPC attributes extractors #16130
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
Changes from 3 commits
fc4a9eb
869670a
1a5cf07
287b28b
1ecb0cf
69a898a
83ffb47
7b59479
645034c
2622a3e
efc5ca8
4ede0fe
4183e63
af4d05a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,21 +6,32 @@ | |
| package io.opentelemetry.instrumentation.api.incubator.semconv.rpc; | ||
|
|
||
| import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; | ||
| import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
| import io.opentelemetry.instrumentation.api.internal.SemconvStability; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| abstract class RpcCommonAttributesExtractor<REQUEST, RESPONSE> | ||
| implements AttributesExtractor<REQUEST, RESPONSE> { | ||
|
|
||
| // copied from RpcIncubatingAttributes | ||
| static final AttributeKey<String> RPC_METHOD = AttributeKey.stringKey("rpc.method"); | ||
|
|
||
| // Stable semconv keys | ||
| static final AttributeKey<String> RPC_SYSTEM_NAME = AttributeKey.stringKey("rpc.system.name"); | ||
|
|
||
| // removed in stable semconv (merged into rpc.method) | ||
| static final AttributeKey<String> RPC_SERVICE = AttributeKey.stringKey("rpc.service"); | ||
|
|
||
| // use RPC_SYSTEM_NAME for stable semconv | ||
| static final AttributeKey<String> RPC_SYSTEM = AttributeKey.stringKey("rpc.system"); | ||
|
|
||
| static final AttributeKey<String> RPC_METHOD_ORIGINAL = | ||
| AttributeKey.stringKey("rpc.method_original"); | ||
|
|
||
| private final RpcAttributesGetter<REQUEST, RESPONSE> getter; | ||
|
|
||
| RpcCommonAttributesExtractor(RpcAttributesGetter<REQUEST, RESPONSE> getter) { | ||
|
|
@@ -30,9 +41,30 @@ abstract class RpcCommonAttributesExtractor<REQUEST, RESPONSE> | |
| @SuppressWarnings("deprecation") // for getMethod() | ||
| @Override | ||
| public final void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { | ||
| internalSet(attributes, RPC_SYSTEM, getter.getSystem(request)); | ||
| internalSet(attributes, RPC_SERVICE, getter.getService(request)); | ||
| internalSet(attributes, RPC_METHOD, getter.getMethod(request)); | ||
| String system = getter.getSystem(request); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's introduce getRpcSystemName mark getSystem as deprecated // to be removed in 3.0 then the getter can be responsible for returning the stable / old name from each of the methods I realize this is different from what we did with database, but I think it would be better
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added getRpcSystemName() as default method, deprecated getSystem() with "to be removed in 3.0". Extractor now calls getRpcSystemName() directly for stable semconv instead of going through stableRpcSystemName() mapping. |
||
|
|
||
| if (SemconvStability.emitStableRpcSemconv()) { | ||
| internalSet( | ||
| attributes, | ||
| RPC_SYSTEM_NAME, | ||
| system == null ? null : SemconvStability.stableRpcSystemName(system)); | ||
| String method = getter.getRpcMethod(request); | ||
| if (getter.isPredefined(request)) { | ||
| internalSet(attributes, RPC_METHOD, method); | ||
| } else { | ||
| internalSet(attributes, RPC_METHOD_ORIGINAL, method); | ||
| internalSet(attributes, RPC_METHOD, "_OTHER"); | ||
| } | ||
| } | ||
|
|
||
| if (SemconvStability.emitOldRpcSemconv()) { | ||
| internalSet(attributes, RPC_SYSTEM, system); | ||
| internalSet(attributes, RPC_SERVICE, getter.getService(request)); | ||
| if (!SemconvStability.emitStableRpcSemconv()) { | ||
| // only set old rpc.method on spans when there's no clash with stable rpc.method | ||
| internalSet(attributes, RPC_METHOD, getter.getMethod(request)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -42,6 +74,13 @@ public final void onEnd( | |
| REQUEST request, | ||
| @Nullable RESPONSE response, | ||
| @Nullable Throwable error) { | ||
| // No response attributes | ||
| if (SemconvStability.emitStableRpcSemconv()) { | ||
| String errorType = getter.getErrorType(request, response, error); | ||
| // fall back to exception class name & _OTHER | ||
|
trask marked this conversation as resolved.
Outdated
|
||
| if (errorType == null && error != null) { | ||
| errorType = error.getClass().getName(); | ||
| } | ||
| internalSet(attributes, ERROR_TYPE, errorType); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.