-
Notifications
You must be signed in to change notification settings - Fork 836
/
Copy pathtransform.ts
103 lines (94 loc) · 3.09 KB
/
transform.ts
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
/*!
* Copyright 2019, OpenTelemetry Authors
*
* 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
*
* https://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.
*/
import * as api from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/tracing';
import { hrTimeToMicroseconds } from '@opentelemetry/core';
import * as zipkinTypes from './types';
import { Resource } from '@opentelemetry/resources';
const ZIPKIN_SPAN_KIND_MAPPING = {
[api.SpanKind.CLIENT]: zipkinTypes.SpanKind.CLIENT,
[api.SpanKind.SERVER]: zipkinTypes.SpanKind.SERVER,
[api.SpanKind.CONSUMER]: zipkinTypes.SpanKind.CONSUMER,
[api.SpanKind.PRODUCER]: zipkinTypes.SpanKind.PRODUCER,
// When absent, the span is local.
[api.SpanKind.INTERNAL]: undefined,
};
export const statusCodeTagName = 'ot.status_code';
export const statusDescriptionTagName = 'ot.status_description';
/**
* Translate OpenTelemetry ReadableSpan to ZipkinSpan format
* @param span Span to be translated
*/
export function toZipkinSpan(
span: ReadableSpan,
serviceName: string,
statusCodeTagName: string,
statusDescriptionTagName: string
): zipkinTypes.Span {
const zipkinSpan: zipkinTypes.Span = {
traceId: span.spanContext.traceId,
parentId: span.parentSpanId,
name: span.name,
id: span.spanContext.spanId,
kind: ZIPKIN_SPAN_KIND_MAPPING[span.kind],
timestamp: hrTimeToMicroseconds(span.startTime),
duration: hrTimeToMicroseconds(span.duration),
localEndpoint: { serviceName },
tags: _toZipkinTags(
span.attributes,
span.status,
statusCodeTagName,
statusDescriptionTagName,
span.resource
),
annotations: span.events.length
? _toZipkinAnnotations(span.events)
: undefined,
};
return zipkinSpan;
}
/** Converts OpenTelemetry Attributes and Status to Zipkin Tags format. */
export function _toZipkinTags(
attributes: api.Attributes,
status: api.Status,
statusCodeTagName: string,
statusDescriptionTagName: string,
resource: Resource
): zipkinTypes.Tags {
const tags: { [key: string]: unknown } = {};
for (const key of Object.keys(attributes)) {
tags[key] = String(attributes[key]);
}
tags[statusCodeTagName] = String(api.CanonicalCode[status.code]);
if (status.message) {
tags[statusDescriptionTagName] = status.message;
}
Object.keys(resource.labels).forEach(
name => (tags[name] = resource.labels[name])
);
return tags;
}
/**
* Converts OpenTelemetry Events to Zipkin Annotations format.
*/
export function _toZipkinAnnotations(
events: api.TimedEvent[]
): zipkinTypes.Annotation[] {
return events.map(event => ({
timestamp: hrTimeToMicroseconds(event.time),
value: event.name,
}));
}