Skip to content

Commit a4ca00b

Browse files
committed
fix(gax): derive operation span name from fullMethodName using dot separator
1 parent 90f431a commit a4ca00b

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTelemetryTracingTracer.java‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void injectTraceContext(Map<String, String> carrier) {
107107
this.tracer = tracer;
108108
this.apiTracerContext = apiTracerContext;
109109
this.attemptSpanName = attemptSpanName;
110-
this.operationSpanName = resolveOperationSpanName(attemptSpanName);
110+
this.operationSpanName = resolveOperationSpanName(apiTracerContext);
111111
this.attemptAttributes = new HashMap<>();
112112
this.parentContext = Context.current();
113113
buildAttributes();
@@ -130,18 +130,15 @@ private Span startOperationSpan() {
130130
}
131131

132132
/**
133-
* Derives the operation-level span name from the attempt span name.
133+
* Resolves the operation-level span name from the tracer context.
134134
*
135-
* @param attemptSpanName the attempt span name
135+
* @param apiTracerContext the tracer context containing method metadata
136136
* @return the operation span name
137137
*/
138-
private static String resolveOperationSpanName(String attemptSpanName) {
139-
if (!Strings.isNullOrEmpty(attemptSpanName)) {
140-
if (attemptSpanName.endsWith("/attempt")) {
141-
String name = attemptSpanName.substring(0, attemptSpanName.length() - "/attempt".length());
142-
return name.isEmpty() ? "operation" : name;
143-
}
144-
return "attempt".equals(attemptSpanName) ? "operation" : attemptSpanName;
138+
private static String resolveOperationSpanName(ApiTracerContext apiTracerContext) {
139+
String fullMethodName = apiTracerContext.fullMethodName();
140+
if (!Strings.isNullOrEmpty(fullMethodName)) {
141+
return fullMethodName.replace('/', '.');
145142
}
146143
return "operation";
147144
}

‎sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/OpenTelemetryTracingTracerTest.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void setUp() {
9191
lenient()
9292
.when(operationSpan.storeInContext(any(io.opentelemetry.context.Context.class)))
9393
.thenAnswer(invocation -> invocation.getArgument(0));
94-
lenient().when(tracer.spanBuilder("Service/Method")).thenReturn(operationSpanBuilder);
94+
lenient().when(tracer.spanBuilder("operation")).thenReturn(operationSpanBuilder);
9595

9696
openTelemetryTracingTracer =
9797
new OpenTelemetryTracingTracer(tracer, ApiTracerContext.empty(), ATTEMPT_SPAN_NAME);
@@ -767,4 +767,20 @@ void testInjectTraceContext_withOperationSpanFallback() {
767767
assertThat(carrier.get("traceparent")).contains("00000000000000000000000000000003");
768768
assertThat(carrier.get("traceparent")).contains("0000000000000004");
769769
}
770+
771+
@Test
772+
void testOperationSpanName_replacesSlashWithDotInFullMethodName() {
773+
ApiTracerContext context =
774+
ApiTracerContext.newBuilder()
775+
.setLibraryMetadata(com.google.api.gax.rpc.LibraryMetadata.empty())
776+
.setFullMethodName(
777+
"google.cloud.secretmanager.v1.SecretManagerService/AccessSecretVersion")
778+
.setTransport(ApiTracerContext.Transport.GRPC)
779+
.build();
780+
781+
new OpenTelemetryTracingTracer(tracer, context);
782+
783+
verify(tracer)
784+
.spanBuilder("google.cloud.secretmanager.v1.SecretManagerService.AccessSecretVersion");
785+
}
770786
}

0 commit comments

Comments
 (0)