forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OtelToZipkinSpanTransformer.java
236 lines (206 loc) · 9.04 KB
/
OtelToZipkinSpanTransformer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.zipkin;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_PEER_PORT;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_SOCK_PEER_ADDR;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.PEER_SERVICE;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.data.EventData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import java.net.InetAddress;
import java.util.List;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import zipkin2.Endpoint;
import zipkin2.Span;
/**
* This class is responsible for transforming an OpenTelemetry SpanData instance into an instance of
* a Zipkin Span. It is based, in part, on code from
* https://github.com/census-instrumentation/opencensus-java/tree/c960b19889de5e4a7b25f90919d28b066590d4f0/exporters/trace/zipkin
*/
final class OtelToZipkinSpanTransformer {
static final String KEY_INSTRUMENTATION_SCOPE_NAME = "otel.scope.name";
static final String KEY_INSTRUMENTATION_SCOPE_VERSION = "otel.scope.version";
static final String KEY_INSTRUMENTATION_LIBRARY_NAME = "otel.library.name";
static final String KEY_INSTRUMENTATION_LIBRARY_VERSION = "otel.library.version";
static final String OTEL_DROPPED_ATTRIBUTES_COUNT = "otel.dropped_attributes_count";
static final String OTEL_DROPPED_EVENTS_COUNT = "otel.dropped_events_count";
static final String OTEL_STATUS_CODE = "otel.status_code";
static final AttributeKey<String> STATUS_ERROR = stringKey("error");
private final Supplier<InetAddress> ipAddressSupplier;
/**
* Creates an instance of an OtelToZipkinSpanTransformer with the given Supplier that can produce
* an InetAddress, which may be null. This value from this Supplier will be used when creating the
* local zipkin Endpoint for each Span. The default implementation uses
* LocalInetAddressSupplier.getInstance().
*
* @param ipAddressSupplier - A Supplier of an InetAddress.
*/
static OtelToZipkinSpanTransformer create(Supplier<InetAddress> ipAddressSupplier) {
return new OtelToZipkinSpanTransformer(ipAddressSupplier);
}
/**
* Creates an instance of an OtelToZipkinSpanTransformer with the given Supplier that can produce
* an InetAddress. Supplier may return null. This value from this Supplier will be used when
* creating the local zipkin Endpoint for each Span.
*
* @param ipAddressSupplier - A Supplier of an InetAddress, which can be null
*/
private OtelToZipkinSpanTransformer(Supplier<InetAddress> ipAddressSupplier) {
this.ipAddressSupplier = ipAddressSupplier;
}
/**
* Creates an instance of a Zipkin Span from an OpenTelemetry SpanData instance.
*
* @param spanData an OpenTelemetry spanData instance
* @return a new Zipkin Span
*/
Span generateSpan(SpanData spanData) {
long startTimestamp = toEpochMicros(spanData.getStartEpochNanos());
long endTimestamp = toEpochMicros(spanData.getEndEpochNanos());
Span.Builder spanBuilder =
Span.newBuilder()
.traceId(spanData.getTraceId())
.id(spanData.getSpanId())
.kind(toSpanKind(spanData))
.name(spanData.getName())
.timestamp(toEpochMicros(spanData.getStartEpochNanos()))
.duration(Math.max(1, endTimestamp - startTimestamp))
.localEndpoint(getLocalEndpoint(spanData))
.remoteEndpoint(getRemoteEndpoint(spanData));
if (spanData.getParentSpanContext().isValid()) {
spanBuilder.parentId(spanData.getParentSpanId());
}
Attributes spanAttributes = spanData.getAttributes();
spanAttributes.forEach(
(key, value) -> spanBuilder.putTag(key.getKey(), valueToString(key, value)));
int droppedAttributes = spanData.getTotalAttributeCount() - spanAttributes.size();
if (droppedAttributes > 0) {
spanBuilder.putTag(OTEL_DROPPED_ATTRIBUTES_COUNT, String.valueOf(droppedAttributes));
}
StatusData status = spanData.getStatus();
// include status code & error.
if (status.getStatusCode() != StatusCode.UNSET) {
spanBuilder.putTag(OTEL_STATUS_CODE, status.getStatusCode().toString());
// add the error tag, if it isn't already in the source span.
if (status.getStatusCode() == StatusCode.ERROR && spanAttributes.get(STATUS_ERROR) == null) {
spanBuilder.putTag(STATUS_ERROR.getKey(), nullToEmpty(status.getDescription()));
}
}
InstrumentationScopeInfo instrumentationScopeInfo = spanData.getInstrumentationScopeInfo();
if (!instrumentationScopeInfo.getName().isEmpty()) {
spanBuilder.putTag(KEY_INSTRUMENTATION_SCOPE_NAME, instrumentationScopeInfo.getName());
// Include instrumentation library name for backwards compatibility
spanBuilder.putTag(KEY_INSTRUMENTATION_LIBRARY_NAME, instrumentationScopeInfo.getName());
}
if (instrumentationScopeInfo.getVersion() != null) {
spanBuilder.putTag(KEY_INSTRUMENTATION_SCOPE_VERSION, instrumentationScopeInfo.getVersion());
// Include instrumentation library name for backwards compatibility
spanBuilder.putTag(
KEY_INSTRUMENTATION_LIBRARY_VERSION, instrumentationScopeInfo.getVersion());
}
for (EventData annotation : spanData.getEvents()) {
spanBuilder.addAnnotation(toEpochMicros(annotation.getEpochNanos()), annotation.getName());
}
int droppedEvents = spanData.getTotalRecordedEvents() - spanData.getEvents().size();
if (droppedEvents > 0) {
spanBuilder.putTag(OTEL_DROPPED_EVENTS_COUNT, String.valueOf(droppedEvents));
}
return spanBuilder.build();
}
private static String nullToEmpty(String value) {
return value != null ? value : "";
}
private Endpoint getLocalEndpoint(SpanData spanData) {
Attributes resourceAttributes = spanData.getResource().getAttributes();
Endpoint.Builder endpoint = Endpoint.newBuilder();
endpoint.ip(ipAddressSupplier.get());
// use the service.name from the Resource, if it's been set.
String serviceNameValue = resourceAttributes.get(ResourceAttributes.SERVICE_NAME);
if (serviceNameValue == null) {
serviceNameValue = Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME);
}
// In practice should never be null unless the default Resource spec is changed.
if (serviceNameValue != null) {
endpoint.serviceName(serviceNameValue);
}
return endpoint.build();
}
@Nullable
private static Endpoint getRemoteEndpoint(SpanData spanData) {
if (spanData.getKind() == SpanKind.CLIENT || spanData.getKind() == SpanKind.PRODUCER) {
// TODO: Implement fallback mechanism:
// https://opentelemetry.io/docs/reference/specification/trace/sdk_exporters/zipkin/#otlp---zipkin
Attributes attributes = spanData.getAttributes();
String serviceName = attributes.get(PEER_SERVICE);
if (serviceName != null) {
Endpoint.Builder endpoint = Endpoint.newBuilder();
endpoint.serviceName(serviceName);
endpoint.ip(attributes.get(NET_SOCK_PEER_ADDR));
Long port = attributes.get(NET_PEER_PORT);
if (port != null) {
endpoint.port(port.intValue());
}
return endpoint.build();
}
}
return null;
}
@Nullable
private static Span.Kind toSpanKind(SpanData spanData) {
switch (spanData.getKind()) {
case SERVER:
return Span.Kind.SERVER;
case CLIENT:
return Span.Kind.CLIENT;
case PRODUCER:
return Span.Kind.PRODUCER;
case CONSUMER:
return Span.Kind.CONSUMER;
case INTERNAL:
return null;
}
return null;
}
private static long toEpochMicros(long epochNanos) {
return NANOSECONDS.toMicros(epochNanos);
}
private static String valueToString(AttributeKey<?> key, Object attributeValue) {
AttributeType type = key.getType();
switch (type) {
case STRING:
case BOOLEAN:
case LONG:
case DOUBLE:
return String.valueOf(attributeValue);
case STRING_ARRAY:
case BOOLEAN_ARRAY:
case LONG_ARRAY:
case DOUBLE_ARRAY:
return commaSeparated((List<?>) attributeValue);
}
throw new IllegalStateException("Unknown attribute type: " + type);
}
private static String commaSeparated(List<?> values) {
StringBuilder builder = new StringBuilder();
for (Object value : values) {
if (builder.length() != 0) {
builder.append(',');
}
builder.append(value);
}
return builder.toString();
}
}