/
PropagationHelper.java
96 lines (83 loc) · 3.77 KB
/
PropagationHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.eclipse.microprofile.telemetry.tracing.tck.rest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageEntry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
class PropagationHelper {
public static final String BAGGAGE_VALUE = "test.baggage.value";
public static final String BAGGAGE_METADATA = "test.baggage.metadata";
public static final AttributeKey<List<String>> PROPAGATION_HEADERS_ATTR =
AttributeKey.stringArrayKey("test.propagation.headers");
public static final String BAGGAGE_KEY = "test.baggage.key";
public static final AttributeKey<String> BAGGAGE_VALUE_ATTR = AttributeKey.stringKey("test.baggage");
public static final AttributeKey<String> BAGGAGE_METADATA_ATTR = AttributeKey.stringKey("test.baggage.metadata");
public static final List<String> PROPAGATION_HEADER_NAMES = Arrays.asList("baggage", "traceparent",
"b3",
"X-B3-TraceId", "X-B3-SpanId", "X-B3-ParentSpanId", "X-B3-Sampled",
"uber-trace-id");
@Path("/")
public static class SpanResource {
@GET
@Path("/span")
public String get(@Context HttpHeaders headers) {
Span span = Span.current();
// Extract the propagation headers and store in the span
List<String> propagationHeaders = headers.getRequestHeaders().keySet().stream()
.filter(h -> PROPAGATION_HEADER_NAMES.contains(h) || h.startsWith("uberctx-"))
.collect(Collectors.toList());
span.setAttribute(AttributeKey.stringArrayKey("test.propagation.headers"), propagationHeaders);
// Extract the test baggage value (if present) and store in the span
BaggageEntry baggageEntry = Baggage.current().asMap().get(BAGGAGE_KEY);
if (baggageEntry != null) {
span.setAttribute(BAGGAGE_VALUE_ATTR, baggageEntry.getValue());
span.setAttribute(BAGGAGE_METADATA_ATTR, baggageEntry.getMetadata().getValue());
}
return "OK";
}
}
@RegisterRestClient(configKey = "client")
@Path("/")
public interface SpanResourceClient {
@GET
@Path("/span")
Response span();
}
@ApplicationPath("/")
public static class RestApplication extends Application {
}
public static boolean isPropagationHeader(String header) {
return PROPAGATION_HEADER_NAMES.contains(header)
|| header.startsWith("uberctx-"); // Jaeger baggage headers use keys as part of the header but have a
// common prefix
}
}