@uoa/lambda-tracing 1.5.0 → 2.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,155 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * Copied from https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-exporter-zipkin/src/zipkin.ts
19
+ */
20
+
21
+ import { diag } from '@opentelemetry/api';
22
+ import { ExportResult, ExportResultCode, getEnv } from '@opentelemetry/core';
23
+ import { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
24
+ import { prepareSend } from './platform/index';
25
+ import * as zipkinTypes from './types';
26
+ import {
27
+ toZipkinSpan,
28
+ defaultStatusCodeTagName,
29
+ defaultStatusErrorTagName,
30
+ } from './transform';
31
+ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
32
+ import { prepareGetHeaders } from './utils';
33
+
34
+ /**
35
+ * Zipkin Exporter
36
+ */
37
+ export class ZipkinExporter implements SpanExporter {
38
+ private readonly DEFAULT_SERVICE_NAME = 'OpenTelemetry Service';
39
+ private readonly _statusCodeTagName: string;
40
+ private readonly _statusDescriptionTagName: string;
41
+ private _urlStr: string;
42
+ private _send: zipkinTypes.SendFunction;
43
+ private _getHeaders: zipkinTypes.GetHeaders | undefined;
44
+ private _serviceName?: string;
45
+ private _isShutdown: boolean;
46
+ private _sendingPromises: Promise<unknown>[] = [];
47
+
48
+ constructor(config: zipkinTypes.ExporterConfig = {}) {
49
+ this._urlStr = config.url || getEnv().OTEL_EXPORTER_ZIPKIN_ENDPOINT;
50
+ this._send = prepareSend(this._urlStr, config.headers);
51
+ this._serviceName = config.serviceName;
52
+ this._statusCodeTagName = config.statusCodeTagName || defaultStatusCodeTagName;
53
+ this._statusDescriptionTagName =
54
+ config.statusDescriptionTagName || defaultStatusErrorTagName;
55
+ this._isShutdown = false;
56
+ if (typeof config.getExportRequestHeaders === 'function') {
57
+ this._getHeaders = prepareGetHeaders(config.getExportRequestHeaders);
58
+ } else {
59
+ // noop
60
+ this._beforeSend = function () {};
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Export spans.
66
+ */
67
+ export(
68
+ spans: ReadableSpan[],
69
+ resultCallback: (result: ExportResult) => void
70
+ ): void {
71
+ const serviceName = String(
72
+ this._serviceName ||
73
+ spans[0].resource.attributes[SemanticResourceAttributes.SERVICE_NAME] ||
74
+ this.DEFAULT_SERVICE_NAME
75
+ );
76
+
77
+ diag.debug('Zipkin exporter export');
78
+ if (this._isShutdown) {
79
+ setTimeout(() =>
80
+ resultCallback({
81
+ code: ExportResultCode.FAILED,
82
+ error: new Error('Exporter has been shutdown'),
83
+ })
84
+ );
85
+ return;
86
+ }
87
+ const promise = new Promise<void>(resolve => {
88
+ this._sendSpans(spans, serviceName, result => {
89
+ resolve();
90
+ resultCallback(result);
91
+ });
92
+ });
93
+
94
+
95
+ this._sendingPromises.push(promise);
96
+ const popPromise = () => {
97
+ const index = this._sendingPromises.indexOf(promise);
98
+ this._sendingPromises.splice(index, 1);
99
+ };
100
+ promise.then(popPromise, popPromise);
101
+ }
102
+
103
+ /**
104
+ * Shutdown exporter. Noop operation in this exporter.
105
+ */
106
+ shutdown(): Promise<void> {
107
+ diag.debug('Zipkin exporter shutdown');
108
+ this._isShutdown = true;
109
+ return new Promise((resolve, reject) => {
110
+ Promise.all(this._sendingPromises).then(() => {
111
+ resolve();
112
+ }, reject);
113
+ });
114
+ }
115
+
116
+ /**
117
+ * if user defines getExportRequestHeaders in config then this will be called
118
+ * everytime before send, otherwise it will be replaced with noop in
119
+ * constructor
120
+ * @default noop
121
+ */
122
+ private _beforeSend() {
123
+ if (this._getHeaders) {
124
+ this._send = prepareSend(this._urlStr, this._getHeaders());
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Transform spans and sends to Zipkin service.
130
+ */
131
+ private _sendSpans(
132
+ spans: ReadableSpan[],
133
+ serviceName: string,
134
+ done?: (result: ExportResult) => void
135
+ ) {
136
+ const zipkinSpans = spans.map(span =>
137
+ toZipkinSpan(
138
+ span,
139
+ String(
140
+ span.attributes[SemanticResourceAttributes.SERVICE_NAME] ||
141
+ span.resource.attributes[SemanticResourceAttributes.SERVICE_NAME] ||
142
+ serviceName
143
+ ),
144
+ this._statusCodeTagName,
145
+ this._statusDescriptionTagName
146
+ )
147
+ );
148
+ this._beforeSend();
149
+ return this._send(zipkinSpans, (result: ExportResult) => {
150
+ if (done) {
151
+ return done(result);
152
+ }
153
+ });
154
+ }
155
+ }