Skip to content

Commit

Permalink
Always pass Context when recording HttpServerMetrics (#6223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Jun 27, 2022
1 parent 4f7c241 commit 72e0b26
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
context);
return;
}
activeRequests.add(-1, applyActiveRequestsView(state.startAttributes()));
activeRequests.add(-1, applyActiveRequestsView(state.startAttributes()), context);
duration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_MS,
applyServerDurationView(state.startAttributes(), endAttributes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ void collectsMetrics() {
.put("http.status_code", 200)
.build();

Context parent =
Context.root()
.with(
Span.wrap(
SpanContext.create(
"ff01020304050600ff0a0b0c0d0e0f00",
"090a0b0c0d0e0f00",
TraceFlags.getSampled(),
TraceState.getDefault())));

Context context1 = listener.onStart(parent, requestAttributes, nanos(100));
SpanContext spanContext1 =
SpanContext.create(
"ff01020304050600ff0a0b0c0d0e0f00",
"090a0b0c0d0e0f00",
TraceFlags.getSampled(),
TraceState.getDefault());
SpanContext spanContext2 =
SpanContext.create(
"123456789abcdef00000000000999999",
"abcde00000054321",
TraceFlags.getSampled(),
TraceState.getDefault());

Context parent1 = Context.root().with(Span.wrap(spanContext1));
Context context1 = listener.onStart(parent1, requestAttributes, nanos(100));

assertThat(metricReader.collectAllMetrics())
.satisfiesExactlyInAnyOrder(
Expand All @@ -81,18 +85,32 @@ void collectsMetrics() {
.hasExemplarsSatisfying(
exemplar ->
exemplar
.hasTraceId("ff01020304050600ff0a0b0c0d0e0f00")
.hasSpanId("090a0b0c0d0e0f00")))));
.hasTraceId(spanContext1.getTraceId())
.hasSpanId(spanContext1.getSpanId())))));

Context context2 = listener.onStart(Context.root(), requestAttributes, nanos(150));
Context parent2 = Context.root().with(Span.wrap(spanContext2));
Context context2 = listener.onStart(parent2, requestAttributes, nanos(150));

assertThat(metricReader.collectAllMetrics())
.satisfiesExactlyInAnyOrder(
metric ->
assertThat(metric)
.hasName("http.server.active_requests")
.hasLongSumSatisfying(
sum -> sum.hasPointsSatisfying(point -> point.hasValue(2))));
sum ->
sum.hasPointsSatisfying(
point ->
point
.hasValue(2)
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_HOST, "host"),
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"))
.hasExemplarsSatisfying(
exemplar ->
exemplar
.hasTraceId(spanContext2.getTraceId())
.hasSpanId(spanContext2.getSpanId())))));

listener.onEnd(context1, responseAttributes, nanos(250));

Expand All @@ -102,7 +120,20 @@ void collectsMetrics() {
assertThat(metric)
.hasName("http.server.active_requests")
.hasLongSumSatisfying(
sum -> sum.hasPointsSatisfying(point -> point.hasValue(1))),
sum ->
sum.hasPointsSatisfying(
point ->
point
.hasValue(1)
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_HOST, "host"),
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"))
.hasExemplarsSatisfying(
exemplar ->
exemplar
.hasTraceId(spanContext1.getTraceId())
.hasSpanId(spanContext1.getSpanId())))),
metric ->
assertThat(metric)
.hasName("http.server.duration")
Expand All @@ -122,8 +153,8 @@ void collectsMetrics() {
.hasExemplarsSatisfying(
exemplar ->
exemplar
.hasTraceId("ff01020304050600ff0a0b0c0d0e0f00")
.hasSpanId("090a0b0c0d0e0f00")))));
.hasTraceId(spanContext1.getTraceId())
.hasSpanId(spanContext1.getSpanId())))));

listener.onEnd(context2, responseAttributes, nanos(300));

Expand All @@ -140,7 +171,14 @@ void collectsMetrics() {
.hasHistogramSatisfying(
histogram ->
histogram.hasPointsSatisfying(
point -> point.hasSum(300 /* millis */))));
point ->
point
.hasSum(300 /* millis */)
.hasExemplarsSatisfying(
exemplar ->
exemplar
.hasTraceId(spanContext2.getTraceId())
.hasSpanId(spanContext2.getSpanId())))));
}

@Test
Expand Down

0 comments on commit 72e0b26

Please sign in to comment.