Skip to content

Commit 606343e

Browse files
authored
Fix gRPC metadata key allowed characters (#178)
Signed-off-by: Pavol Loffay <[email protected]>
1 parent bc1f192 commit 606343e

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/GrpcSemanticAttributes.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,37 @@
2121
public class GrpcSemanticAttributes {
2222
private GrpcSemanticAttributes() {}
2323

24-
public static final String SCHEME = ":scheme";
25-
public static final String PATH = ":path";
26-
public static final String AUTHORITY = ":authority";
27-
public static final String METHOD = ":method";
24+
public static final String SCHEME = "scheme";
25+
public static final String PATH = "path";
26+
public static final String AUTHORITY = "authority";
27+
public static final String METHOD = "method";
2828

2929
/**
3030
* These metadata headers are added in Http2Headers instrumentation. We use different names than
3131
* original HTTP2 header names to avoid any collisions with app code.
3232
*
33-
* <p>We cannot use prefix because e.g. ht.:path is not a valid key.
33+
* <p>For valid characters in keys read
34+
* https://grpc.github.io/grpc-java/javadoc/io/grpc/Metadata.Key.html
3435
*/
35-
private static final String SUFFIX = ".ht";
36+
private static final String PREFIX = "ht.";
3637

3738
public static final Metadata.Key<String> SCHEME_METADATA_KEY =
38-
Metadata.Key.of(SCHEME + SUFFIX, Metadata.ASCII_STRING_MARSHALLER);
39+
Metadata.Key.of(PREFIX + SCHEME, Metadata.ASCII_STRING_MARSHALLER);
3940
public static final Metadata.Key<String> PATH_METADATA_KEY =
40-
Metadata.Key.of(PATH + SUFFIX, Metadata.ASCII_STRING_MARSHALLER);
41+
Metadata.Key.of(PREFIX + PATH, Metadata.ASCII_STRING_MARSHALLER);
4142
public static final Metadata.Key<String> AUTHORITY_METADATA_KEY =
42-
Metadata.Key.of(AUTHORITY + SUFFIX, Metadata.ASCII_STRING_MARSHALLER);
43+
Metadata.Key.of(PREFIX + AUTHORITY, Metadata.ASCII_STRING_MARSHALLER);
4344
public static final Metadata.Key<String> METHOD_METADATA_KEY =
44-
Metadata.Key.of(METHOD + SUFFIX, Metadata.ASCII_STRING_MARSHALLER);
45+
Metadata.Key.of(PREFIX + METHOD, Metadata.ASCII_STRING_MARSHALLER);
4546

46-
public static String removeHypertracePrefix(String key) {
47-
if (key.endsWith(SUFFIX)) {
48-
return key.replace(SUFFIX, "");
47+
public static String removeHypertracePrefixAndAddColon(String key) {
48+
if (key.startsWith(PREFIX)) {
49+
return addColon(key.replaceFirst(PREFIX, ""));
4950
}
5051
return key;
5152
}
53+
54+
public static String addColon(String key) {
55+
return ":" + key;
56+
}
5257
}

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/GrpcSpanDecorator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void addMetadataAttributes(
5858
Key<String> stringKey = Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
5959
Iterable<String> stringValues = metadata.getAll(stringKey);
6060
for (String stringValue : stringValues) {
61-
key = GrpcSemanticAttributes.removeHypertracePrefix(key);
61+
key = GrpcSemanticAttributes.removeHypertracePrefixAndAddColon(key);
6262
span.setAttribute(keySupplier.apply(key), stringValue);
6363
}
6464
}
@@ -80,7 +80,7 @@ public static Map<String, String> metadataToMap(Metadata metadata) {
8080
Key<String> stringKey = Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
8181
Iterable<String> stringValues = metadata.getAll(stringKey);
8282
for (String stringValue : stringValues) {
83-
key = GrpcSemanticAttributes.removeHypertracePrefix(key);
83+
key = GrpcSemanticAttributes.removeHypertracePrefixAndAddColon(key);
8484
mapHeaders.put(key, stringValue);
8585
}
8686
}

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/NettyHttp2HeadersInstrumentationModule.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,26 @@ public static void exit(
9797
Span currentSpan = Java8BytecodeBridge.currentSpan();
9898
if (scheme != null) {
9999
currentSpan.setAttribute(
100-
HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.SCHEME),
100+
HypertraceSemanticAttributes.rpcRequestMetadata(
101+
GrpcSemanticAttributes.addColon(GrpcSemanticAttributes.SCHEME)),
101102
scheme.toString());
102103
}
103104
if (defaultPath != null) {
104105
currentSpan.setAttribute(
105-
HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.PATH),
106+
HypertraceSemanticAttributes.rpcRequestMetadata(
107+
GrpcSemanticAttributes.addColon(GrpcSemanticAttributes.PATH)),
106108
defaultPath.toString());
107109
}
108110
if (authority != null) {
109111
currentSpan.setAttribute(
110-
HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.AUTHORITY),
112+
HypertraceSemanticAttributes.rpcRequestMetadata(
113+
GrpcSemanticAttributes.addColon(GrpcSemanticAttributes.AUTHORITY)),
111114
authority.toString());
112115
}
113116
if (method != null) {
114117
currentSpan.setAttribute(
115-
HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.METHOD),
118+
HypertraceSemanticAttributes.rpcRequestMetadata(
119+
GrpcSemanticAttributes.addColon(GrpcSemanticAttributes.METHOD)),
116120
method.toString());
117121
}
118122
}

instrumentation/grpc-1.5/src/test/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/GrpcInstrumentationTest.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,26 @@ private void assertHttp2HeadersForSayHelloMethod(SpanData span) {
235235
Assertions.assertEquals(
236236
"http",
237237
span.getAttributes()
238-
.get(HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.SCHEME)));
238+
.get(
239+
HypertraceSemanticAttributes.rpcRequestMetadata(
240+
":" + GrpcSemanticAttributes.SCHEME)));
239241
Assertions.assertEquals(
240242
"POST",
241243
span.getAttributes()
242-
.get(HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.METHOD)));
244+
.get(
245+
HypertraceSemanticAttributes.rpcRequestMetadata(
246+
":" + GrpcSemanticAttributes.METHOD)));
243247
Assertions.assertEquals(
244248
String.format("localhost:%d", SERVER.getPort()),
245249
span.getAttributes()
246250
.get(
247-
HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.AUTHORITY)));
251+
HypertraceSemanticAttributes.rpcRequestMetadata(
252+
":" + GrpcSemanticAttributes.AUTHORITY)));
248253
Assertions.assertEquals(
249254
"/org.hypertrace.example.Greeter/SayHello",
250255
span.getAttributes()
251-
.get(HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.PATH)));
256+
.get(
257+
HypertraceSemanticAttributes.rpcRequestMetadata(
258+
":" + GrpcSemanticAttributes.PATH)));
252259
}
253260
}

0 commit comments

Comments
 (0)