-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cleanup * add server header timing reader
- Loading branch information
1 parent
5a818e8
commit 7925bae
Showing
9 changed files
with
205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
custom/src/main/java/com/grafana/extensions/servertiming/ServerTimingHeaderReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Grafana Labs | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.grafana.extensions.servertiming; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.propagation.TextMapGetter; | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientResponseConsumer; | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpCommonAttributesGetter; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ServerTimingHeaderReader implements HttpClientResponseConsumer { | ||
private static final TextMapGetter<Map<String, String>> GETTER = | ||
new TextMapGetter<Map<String, String>>() { | ||
@Override | ||
public Iterable<String> keys(Map<String, String> carrier) { | ||
return carrier.keySet(); | ||
} | ||
|
||
@Override | ||
public String get(Map<String, String> carrier, String key) { | ||
return carrier.get(key); | ||
} | ||
}; | ||
|
||
@Override | ||
public <REQUEST, RESPONSE> void consume( | ||
HttpCommonAttributesGetter<REQUEST, RESPONSE> getter, REQUEST request, RESPONSE response) { | ||
List<String> timings = | ||
getter.getHttpResponseHeader(request, response, ServerTimingHeaderCustomizer.SERVER_TIMING); | ||
|
||
for (String timing : timings) { | ||
if (timing.startsWith("traceparent")) { | ||
String[] parts = timing.split(";"); | ||
for (String part : parts) { | ||
if (part.startsWith("desc=")) { | ||
String traceParent = part.substring(6, part.length() - 1); | ||
Context traceparent = | ||
W3CTraceContextPropagator.getInstance() | ||
.extract( | ||
Context.current(), | ||
Collections.singletonMap("traceparent", traceParent), | ||
GETTER); | ||
SpanContext spanContext = Span.fromContext(traceparent).getSpanContext(); | ||
if (spanContext.getTraceFlags().isSampled()) { | ||
ServerTimingHeaderCustomizer.sampledTraces.add(spanContext.getTraceId()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...in/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientResponseConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Grafana Labs | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.semconv.http; | ||
|
||
// copied from instrumentation-api - use when released | ||
public interface HttpClientResponseConsumer { | ||
<REQUEST, RESPONSE> void consume( | ||
HttpCommonAttributesGetter<REQUEST, RESPONSE> getter, REQUEST request, RESPONSE response); | ||
} |
1 change: 1 addition & 0 deletions
1
...INF/services/io.opentelemetry.instrumentation.api.semconv.http.HttpClientResponseConsumer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.grafana.extensions.servertiming.ServerTimingHeaderReader |
58 changes: 58 additions & 0 deletions
58
custom/src/test/java/com/grafana/extensions/servertiming/ServerTimingHeaderReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Grafana Labs | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.grafana.extensions.servertiming; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class ServerTimingHeaderReaderTest { | ||
|
||
private ServerTimingHeaderReader serverTimingHeaderReader = new ServerTimingHeaderReader(); | ||
|
||
@RegisterExtension InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
ServerTimingHeaderCustomizer.sampledTraces.clear(); | ||
} | ||
|
||
@Test | ||
void notSampled() { | ||
testing.runWithSpan( | ||
"server", | ||
() -> { | ||
String serverTiming = ServerTimingHeaderCustomizer.toHeaderValue(Context.current()); | ||
|
||
serverTimingHeaderReader.consume( | ||
new StringHttpCommonAttributesGetter(serverTiming), "request", "response"); | ||
assertThat(ServerTimingHeaderCustomizer.sampledTraces).isEmpty(); | ||
}); | ||
} | ||
|
||
@Test | ||
void sampled() { | ||
testing.runWithSpan( | ||
"server", | ||
() -> { | ||
String traceId = Span.current().getSpanContext().getTraceId(); | ||
ServerTimingHeaderCustomizer.sampledTraces.add(traceId); | ||
String serverTiming = ServerTimingHeaderCustomizer.toHeaderValue(Context.current()); | ||
|
||
// remove the traceId to see that it is added back by the reader | ||
ServerTimingHeaderCustomizer.sampledTraces.clear(); | ||
serverTimingHeaderReader.consume( | ||
new StringHttpCommonAttributesGetter(serverTiming), "request", "response"); | ||
assertThat(ServerTimingHeaderCustomizer.sampledTraces).contains(traceId); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...m/src/test/java/com/grafana/extensions/servertiming/StringHttpCommonAttributesGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Grafana Labs | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.grafana.extensions.servertiming; | ||
|
||
import io.opentelemetry.instrumentation.api.semconv.http.HttpCommonAttributesGetter; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
class StringHttpCommonAttributesGetter implements HttpCommonAttributesGetter<String, String> { | ||
private String serverTiming; | ||
|
||
public StringHttpCommonAttributesGetter(String serverTiming) { | ||
this.serverTiming = serverTiming; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getHttpRequestMethod(String s) { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public List<String> getHttpRequestHeader(String s, String name) { | ||
return List.of(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Integer getHttpResponseStatusCode(String s, String s2, @Nullable Throwable error) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public List<String> getHttpResponseHeader(String s, String s2, String name) { | ||
if (name.equals("Server-Timing")) { | ||
return List.of(serverTiming); | ||
} | ||
return List.of(); | ||
} | ||
} |