@conduit-client/service-instrumentation-o11y 2.0.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.
package/LICENSE.txt ADDED
@@ -0,0 +1,27 @@
1
+ *Attorney/Client Privileged + Confidential*
2
+
3
+ Terms of Use for Public Code (Non-OSS)
4
+
5
+ *NOTE:* Before publishing code under this license/these Terms of Use, please review https://salesforce.quip.com/WFfvAMKB18AL and confirm that you’ve completed all prerequisites described therein. *These Terms of Use may not be used or modified without input from IP and Product Legal.*
6
+
7
+ *Terms of Use*
8
+
9
+ Copyright 2022 Salesforce, Inc. All rights reserved.
10
+
11
+ These Terms of Use govern the download, installation, and/or use of this software provided by Salesforce, Inc. (“Salesforce”) (the “Software”), were last updated on April 15, 2022, ** and constitute a legally binding agreement between you and Salesforce. If you do not agree to these Terms of Use, do not install or use the Software.
12
+
13
+ Salesforce grants you a worldwide, non-exclusive, no-charge, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute the Software and derivative works subject to these Terms. These Terms shall be included in all copies or substantial portions of the Software.
14
+
15
+ Subject to the limited rights expressly granted hereunder, Salesforce reserves all rights, title, and interest in and to all intellectual property subsisting in the Software. No rights are granted to you hereunder other than as expressly set forth herein. Users residing in countries on the United States Office of Foreign Assets Control sanction list, or which are otherwise subject to a US export embargo, may not use the Software.
16
+
17
+ Implementation of the Software may require development work, for which you are responsible. The Software may contain bugs, errors and incompatibilities and is made available on an AS IS basis without support, updates, or service level commitments.
18
+
19
+ Salesforce reserves the right at any time to modify, suspend, or discontinue, the Software (or any part thereof) with or without notice. You agree that Salesforce shall not be liable to you or to any third party for any modification, suspension, or discontinuance.
20
+
21
+ You agree to defend Salesforce against any claim, demand, suit or proceeding made or brought against Salesforce by a third party arising out of or accruing from (a) your use of the Software, and (b) any application you develop with the Software that infringes any copyright, trademark, trade secret, trade dress, patent, or other intellectual property right of any person or defames any person or violates their rights of publicity or privacy (each a “Claim Against Salesforce”), and will indemnify Salesforce from any damages, attorney fees, and costs finally awarded against Salesforce as a result of, or for any amounts paid by Salesforce under a settlement approved by you in writing of, a Claim Against Salesforce, provided Salesforce (x) promptly gives you written notice of the Claim Against Salesforce, (y) gives you sole control of the defense and settlement of the Claim Against Salesforce (except that you may not settle any Claim Against Salesforce unless it unconditionally releases Salesforce of all liability), and (z) gives you all reasonable assistance, at your expense.
22
+
23
+ WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE SOFTWARE IS NOT SUPPORTED AND IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL SALESFORCE HAVE ANY LIABILITY FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES, OR DAMAGES BASED ON LOST PROFITS, DATA, OR USE, IN CONNECTION WITH THE SOFTWARE, HOWEVER CAUSED AND WHETHER IN CONTRACT, TORT, OR UNDER ANY OTHER THEORY OF LIABILITY, WHETHER OR NOT YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
24
+
25
+ These Terms of Use shall be governed exclusively by the internal laws of the State of California, without regard to its conflicts of laws rules. Each party hereby consents to the exclusive jurisdiction of the state and federal courts located in San Francisco County, California to adjudicate any dispute arising out of or relating to these Terms of Use and the download, installation, and/or use of the Software. Except as expressly stated herein, these Terms of Use constitute the entire agreement between the parties, and supersede all prior and contemporaneous agreements, proposals, or representations, written or oral, concerning their subject matter. No modification, amendment, or waiver of any provision of these Terms of Use shall be effective unless it is by an update to these Terms of Use that Salesforce makes available, or is in writing and signed by the party against whom the modification, amendment, or waiver is to be asserted.
26
+
27
+ _*Data Privacy*_: Salesforce may collect, process, and store device, system, and other information related to your use of the Software. This information includes, but is not limited to, IP address, user metrics, and other data (“Usage Data”). Salesforce may use Usage Data for analytics, product development, and marketing purposes. You acknowledge that files generated in conjunction with the Software may contain sensitive or confidential data, and you are solely responsible for anonymizing and protecting such data.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Luvio O11y Instrumentation Service
2
+
3
+ This package provides an instrumentation service implementation using O11y library for the interface defined in `-client/service-instrumentation-types`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -client/service-instrumentation-o11y
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { buildServiceDescriptor } from '@conduit-client/service-instrumentation-o11y/v1';
15
+ import { logger } from '@conduit-client/logger';
16
+
17
+ const instrumentationService = buildServiceDescriptor(logger);
18
+ ```
19
+
20
+ This implementation provides OpenTelemetry compatible APIs backed by the O11y library.
21
+
22
+ ## Types
23
+
24
+ This package implements the interfaces defined in [`-client/service-instrumentation-types`](../instrumentation-types).
@@ -0,0 +1,6 @@
1
+ /*!
2
+ * Copyright (c) 2022, Salesforce, Inc.,
3
+ * All rights reserved.
4
+ * For full license text, see the LICENSE.txt file
5
+ */
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
File without changes
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { buildServiceDescriptor } from './o11y-instrumentation-service';
@@ -0,0 +1,3 @@
1
+ import { type LoggerService } from '@conduit-client/utils';
2
+ import type { InstrumentationServiceDescriptor } from '@conduit-client/service-instrumentation-types/v1';
3
+ export declare function buildServiceDescriptor(logger: LoggerService): InstrumentationServiceDescriptor;
@@ -0,0 +1,24 @@
1
+ import { getInstrumentation } from 'o11y/client';
2
+ import type { LoggerService, NamedLoggerService } from '@conduit-client/utils';
3
+ import type { LuvioOTelMetricsAPI } from '@conduit-client/service-instrumentation-types/v1';
4
+ import type { Attributes, BatchObservableCallback, Counter, Histogram, Meter, MeterOptions, MetricOptions, Observable, ObservableCounter, ObservableGauge, ObservableUpDownCounter, UpDownCounter } from '@opentelemetry/api';
5
+ import { MetricsTags } from 'o11y/shared';
6
+ export declare class O11yOTelMetricsAPI implements LuvioOTelMetricsAPI {
7
+ private services;
8
+ constructor(services: NamedLoggerService);
9
+ getMeter(name: string, _version?: string | undefined, __options?: MeterOptions | undefined): Meter;
10
+ }
11
+ export declare class O11yMeter implements Meter {
12
+ private o11yInstrumentation;
13
+ private logger;
14
+ constructor(o11yInstrumentation: ReturnType<typeof getInstrumentation>, logger: LoggerService);
15
+ createHistogram<AttributesTypes extends Attributes = Attributes>(name: string, _options?: MetricOptions | undefined): Histogram<AttributesTypes>;
16
+ createCounter<AttributesTypes extends Attributes = Attributes>(name: string, options?: MetricOptions | undefined): Counter<AttributesTypes>;
17
+ createUpDownCounter<AttributesTypes extends Attributes = Attributes>(_name: string, _options?: MetricOptions | undefined): UpDownCounter<AttributesTypes>;
18
+ createObservableGauge<AttributesTypes extends Attributes = Attributes>(_name: string, _options?: MetricOptions | undefined): ObservableGauge<AttributesTypes>;
19
+ createObservableCounter<AttributesTypes extends Attributes = Attributes>(_name: string, _options?: MetricOptions | undefined): ObservableCounter<AttributesTypes>;
20
+ createObservableUpDownCounter<AttributesTypes extends Attributes = Attributes>(_name: string, _options?: MetricOptions | undefined): ObservableUpDownCounter<AttributesTypes>;
21
+ addBatchObservableCallback<AttributesTypes extends Attributes = Attributes>(_callback: BatchObservableCallback<AttributesTypes>, _observables: Observable<AttributesTypes>[]): void;
22
+ removeBatchObservableCallback<AttributesTypes extends Attributes = Attributes>(_callback: BatchObservableCallback<AttributesTypes>, _observables: Observable<AttributesTypes>[]): void;
23
+ }
24
+ export declare function sanitizeAttributes(attributes?: Attributes | undefined): MetricsTags | undefined;
@@ -0,0 +1,37 @@
1
+ import { getInstrumentation } from 'o11y/client';
2
+ import type { LoggerService, NamedLoggerService } from '@conduit-client/utils';
3
+ import type { LuvioOTelTraceAPI } from '@conduit-client/service-instrumentation-types/v1';
4
+ import type { Tracer, TracerOptions, Span, SpanContext, SpanOptions, SpanStatus, Context, AttributeValue, Attributes, TimeInput, Exception } from '@opentelemetry/api';
5
+ type Instrumentation = ReturnType<typeof getInstrumentation>;
6
+ type Activity = ReturnType<Instrumentation['startActivity']>;
7
+ export declare class O11yOTelTraceAPI implements LuvioOTelTraceAPI {
8
+ private services;
9
+ constructor(services: NamedLoggerService);
10
+ getTracer(name: string, _version?: string, _options?: TracerOptions): Tracer;
11
+ }
12
+ export declare class O11yTracer implements Tracer {
13
+ private o11yInstrumentation;
14
+ private logger;
15
+ constructor(o11yInstrumentation: ReturnType<typeof getInstrumentation>, logger: LoggerService);
16
+ startSpan(name: string, _options?: SpanOptions, context?: Context): O11ySpan;
17
+ startActiveSpan<F extends (span: Span) => unknown>(name: string, fn: F): ReturnType<F>;
18
+ startActiveSpan<F extends (span: Span) => unknown>(name: string, options: SpanOptions, fn: F): ReturnType<F>;
19
+ startActiveSpan<F extends (span: Span) => unknown>(name: string, options: SpanOptions, context: Context, fn: F): ReturnType<F>;
20
+ }
21
+ export declare class O11ySpan implements Span {
22
+ private activity;
23
+ private logger;
24
+ private _isRecording;
25
+ private attributes;
26
+ constructor(activity: Activity, logger: LoggerService);
27
+ spanContext(): SpanContext;
28
+ setAttribute(key: string, value: AttributeValue): this;
29
+ setAttributes(attributes: Attributes): this;
30
+ addEvent(_name: string, _attributesOrStartTime?: Attributes | TimeInput, _startTime?: TimeInput): this;
31
+ setStatus(_status: SpanStatus): this;
32
+ updateName(_name: string): this;
33
+ end(endTime?: TimeInput): void;
34
+ isRecording(): boolean;
35
+ recordException(exception: Exception, _time?: TimeInput): void;
36
+ }
37
+ export {};
@@ -0,0 +1,269 @@
1
+ /*!
2
+ * Copyright (c) 2022, Salesforce, Inc.,
3
+ * All rights reserved.
4
+ * For full license text, see the LICENSE.txt file
5
+ */
6
+ import { getInstrumentation } from "o11y/client";
7
+ class O11yOTelTraceAPI {
8
+ constructor(services) {
9
+ this.services = services;
10
+ }
11
+ getTracer(name, _version, _options) {
12
+ const o11yInstrumentation = getInstrumentation(name);
13
+ return new O11yTracer(o11yInstrumentation, this.services.logger);
14
+ }
15
+ }
16
+ class O11yTracer {
17
+ constructor(o11yInstrumentation, logger) {
18
+ this.o11yInstrumentation = o11yInstrumentation;
19
+ this.logger = logger;
20
+ }
21
+ startSpan(name, _options, context) {
22
+ const traceId = context == null ? void 0 : context.getValue(Symbol.for("traceId"));
23
+ const spanId = context == null ? void 0 : context.getValue(Symbol.for("spanId"));
24
+ const traceFlags = context == null ? void 0 : context.getValue(Symbol.for("traceFlags"));
25
+ let spanContext = void 0;
26
+ if (traceId !== void 0 && spanId !== void 0 && traceFlags !== void 0) {
27
+ spanContext = {
28
+ traceId,
29
+ spanId,
30
+ traceFlags
31
+ };
32
+ }
33
+ const activityOptions = spanContext === void 0 ? void 0 : {
34
+ instrumentationContext: {
35
+ rootId: spanContext.traceId,
36
+ isRootActivitySampled: spanContext.traceFlags === 1,
37
+ parentId: spanId
38
+ }
39
+ };
40
+ const activity = this.o11yInstrumentation.startActivity(name, activityOptions);
41
+ return new O11ySpan(activity, this.logger);
42
+ }
43
+ startActiveSpan(name, options, context, fn) {
44
+ let opts;
45
+ let ctx;
46
+ let fun;
47
+ if (typeof options === "function") {
48
+ fun = options;
49
+ } else {
50
+ opts = options;
51
+ if (typeof context === "function") {
52
+ fun = context;
53
+ } else {
54
+ ctx = context;
55
+ fun = fn;
56
+ }
57
+ }
58
+ const span = this.startSpan(name, opts, ctx);
59
+ return fun(span);
60
+ }
61
+ }
62
+ class O11ySpan {
63
+ constructor(activity, logger) {
64
+ this.activity = activity;
65
+ this.logger = logger;
66
+ this._isRecording = true;
67
+ this.attributes = {};
68
+ }
69
+ spanContext() {
70
+ return {
71
+ traceId: this.activity.getRootId(),
72
+ spanId: this.activity.getId(),
73
+ traceFlags: this.activity.getIsSampled() ? 1 : 0
74
+ };
75
+ }
76
+ setAttribute(key, value) {
77
+ this.attributes[key] = value;
78
+ return this;
79
+ }
80
+ setAttributes(attributes) {
81
+ this.attributes = { ...this.attributes, ...attributes };
82
+ return this;
83
+ }
84
+ addEvent(_name, _attributesOrStartTime, _startTime) {
85
+ this.logger.warn("O11ySpan does not support addEvents.");
86
+ return this;
87
+ }
88
+ setStatus(_status) {
89
+ this.logger.warn("O11ySpan does not support setStatus.");
90
+ return this;
91
+ }
92
+ updateName(_name) {
93
+ this.logger.warn("O11ySpan does not support updateName.");
94
+ return this;
95
+ }
96
+ end(endTime) {
97
+ let endTimeInternal = endTime;
98
+ try {
99
+ if (typeof endTimeInternal !== "number") {
100
+ this.logger.warn("O11ySpan does not support non number endTime override.");
101
+ endTimeInternal = void 0;
102
+ }
103
+ this.activity.stop(
104
+ void 0,
105
+ void 0,
106
+ endTimeInternal ? { perfStopOverride: endTimeInternal } : void 0
107
+ );
108
+ } finally {
109
+ this._isRecording = false;
110
+ }
111
+ return;
112
+ }
113
+ isRecording() {
114
+ return this._isRecording;
115
+ }
116
+ recordException(exception, _time) {
117
+ this.activity.error(exception);
118
+ }
119
+ }
120
+ class O11yOTelMetricsAPI {
121
+ constructor(services) {
122
+ this.services = services;
123
+ }
124
+ getMeter(name, _version, __options) {
125
+ const o11yInstrumentation = getInstrumentation(name);
126
+ return new O11yMeter(o11yInstrumentation, this.services.logger);
127
+ }
128
+ }
129
+ class O11yMeter {
130
+ constructor(o11yInstrumentation, logger) {
131
+ this.o11yInstrumentation = o11yInstrumentation;
132
+ this.logger = logger;
133
+ }
134
+ createHistogram(name, _options) {
135
+ return new O11yHistogram(name, this.o11yInstrumentation, this.logger);
136
+ }
137
+ createCounter(name, options) {
138
+ if (options) {
139
+ this.logger.warn("counter options not supported in O11y instrumentation");
140
+ }
141
+ return new O11yCounter(name, this.o11yInstrumentation, this.logger);
142
+ }
143
+ createUpDownCounter(_name, _options) {
144
+ return new O11yUpDownCounter(this.logger);
145
+ }
146
+ createObservableGauge(_name, _options) {
147
+ return new O11yObservableGuage(this.logger);
148
+ }
149
+ createObservableCounter(_name, _options) {
150
+ return new O11yObservableCounter(this.logger);
151
+ }
152
+ createObservableUpDownCounter(_name, _options) {
153
+ return new O11yObservableUpDownCounter(this.logger);
154
+ }
155
+ addBatchObservableCallback(_callback, _observables) {
156
+ this.logger.warn("addBatchObservableCallback not supported yet");
157
+ }
158
+ removeBatchObservableCallback(_callback, _observables) {
159
+ this.logger.warn("removeBatchObservableCallback not supported yet");
160
+ }
161
+ }
162
+ class O11yCounter {
163
+ constructor(name, o11yInstrumentation, logger) {
164
+ this.name = name;
165
+ this.o11yInstrumentation = o11yInstrumentation;
166
+ this.logger = logger;
167
+ }
168
+ add(value, attributes, _context) {
169
+ if (value < 0) {
170
+ this.logger.warn(`Counter values must be non-negative. Got ${value}.`);
171
+ return;
172
+ }
173
+ this.o11yInstrumentation.incrementCounter(
174
+ this.name,
175
+ value,
176
+ void 0,
177
+ sanitizeAttributes(attributes)
178
+ );
179
+ }
180
+ }
181
+ class O11yHistogram {
182
+ constructor(name, o11yInstrumentation, logger) {
183
+ this.name = name;
184
+ this.o11yInstrumentation = o11yInstrumentation;
185
+ this.logger = logger;
186
+ }
187
+ record(value, attributes, _context) {
188
+ if (value < 0) {
189
+ this.logger.warn(`Histogram values must be non-negative. Got ${value}.`);
190
+ return;
191
+ }
192
+ this.o11yInstrumentation.trackValue(
193
+ this.name,
194
+ value,
195
+ void 0,
196
+ sanitizeAttributes(attributes)
197
+ );
198
+ }
199
+ }
200
+ class O11yUpDownCounter {
201
+ constructor(logger) {
202
+ this.logger = logger;
203
+ }
204
+ add(_value, _attributes, _context) {
205
+ this.logger.warn("O11yUpDownCounter not supported yet.");
206
+ }
207
+ }
208
+ class O11yObservableCounter {
209
+ constructor(logger) {
210
+ this.logger = logger;
211
+ }
212
+ addCallback(_callback) {
213
+ this.logger.warn("O11yObservableCounter not supported yet. Defaulting to noop.");
214
+ }
215
+ removeCallback(_callback) {
216
+ this.logger.warn("O11yObservableCounter not supported yet. Defaulting to noop.");
217
+ }
218
+ }
219
+ class O11yObservableGuage {
220
+ constructor(logger) {
221
+ this.logger = logger;
222
+ }
223
+ addCallback(_callback) {
224
+ this.logger.warn("O11yObservableGuage not supported yet. Defaulting to noop.");
225
+ }
226
+ removeCallback(_callback) {
227
+ this.logger.warn("O11yObservableGuage not supported yet. Defaulting to noop.");
228
+ }
229
+ }
230
+ class O11yObservableUpDownCounter {
231
+ constructor(logger) {
232
+ this.logger = logger;
233
+ }
234
+ addCallback(_callback) {
235
+ this.logger.warn("O11yObservableUpDownCounter not supported yet. Defaulting to noop.");
236
+ }
237
+ removeCallback(_callback) {
238
+ this.logger.warn("O11yObservableUpDownCounter not supported yet. Defaulting to noop.");
239
+ }
240
+ }
241
+ function sanitizeAttributes(attributes) {
242
+ if (!attributes) return;
243
+ const metricTags = {};
244
+ Object.entries(attributes).forEach(([key, value]) => {
245
+ if (value !== void 0 && !Array.isArray(value)) {
246
+ metricTags[key] = value;
247
+ }
248
+ });
249
+ return metricTags;
250
+ }
251
+ class O11yInstrumentation {
252
+ constructor(services) {
253
+ this.services = services;
254
+ this.currentTimeMs = () => performance.now();
255
+ this.trace = new O11yOTelTraceAPI(this.services);
256
+ this.metrics = new O11yOTelMetricsAPI(this.services);
257
+ }
258
+ }
259
+ function buildServiceDescriptor(logger) {
260
+ return {
261
+ type: "instrumentation",
262
+ version: "1.0",
263
+ service: new O11yInstrumentation({ logger })
264
+ };
265
+ }
266
+ export {
267
+ buildServiceDescriptor
268
+ };
269
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/v1/o11y-trace-api.ts","../../src/v1/o11y-metrics-api.ts","../../src/v1/o11y-instrumentation-service.ts"],"sourcesContent":["/* eslint-disable no-dupe-class-members */\nimport { getInstrumentation } from 'o11y/client';\nimport type { LoggerService, NamedLoggerService } from '@conduit-client/utils';\nimport type { LuvioOTelTraceAPI } from '@conduit-client/service-instrumentation-types/v1';\nimport type {\n Tracer,\n TracerOptions,\n Span,\n SpanContext,\n SpanOptions,\n SpanStatus,\n Context,\n AttributeValue,\n Attributes,\n TimeInput,\n Exception,\n} from '@opentelemetry/api';\n\ntype Instrumentation = ReturnType<typeof getInstrumentation>;\ntype ApiOptions = Parameters<Instrumentation['startActivity']>[1];\ntype Activity = ReturnType<Instrumentation['startActivity']>;\n\nexport class O11yOTelTraceAPI implements LuvioOTelTraceAPI {\n constructor(private services: NamedLoggerService) {}\n\n getTracer(name: string, _version?: string, _options?: TracerOptions): Tracer {\n const o11yInstrumentation = getInstrumentation(name);\n return new O11yTracer(o11yInstrumentation, this.services.logger);\n }\n}\n\nexport class O11yTracer implements Tracer {\n constructor(\n private o11yInstrumentation: ReturnType<typeof getInstrumentation>,\n private logger: LoggerService\n ) {}\n startSpan(name: string, _options?: SpanOptions, context?: Context): O11ySpan {\n const traceId = context?.getValue(Symbol.for('traceId')) as string;\n const spanId = context?.getValue(Symbol.for('spanId')) as string;\n const traceFlags = context?.getValue(Symbol.for('traceFlags')) as number;\n let spanContext: SpanContext | undefined = undefined;\n\n if (traceId !== undefined && spanId !== undefined && traceFlags !== undefined) {\n spanContext = {\n traceId,\n spanId,\n traceFlags,\n };\n }\n\n const activityOptions: ApiOptions =\n spanContext === undefined\n ? undefined\n : {\n instrumentationContext: {\n rootId: spanContext.traceId,\n isRootActivitySampled: spanContext.traceFlags === 1,\n parentId: spanId,\n },\n };\n\n const activity = this.o11yInstrumentation.startActivity(name, activityOptions);\n\n return new O11ySpan(activity, this.logger);\n }\n startActiveSpan<F extends (span: Span) => unknown>(name: string, fn: F): ReturnType<F>;\n startActiveSpan<F extends (span: Span) => unknown>(\n name: string,\n options: SpanOptions,\n fn: F\n ): ReturnType<F>;\n startActiveSpan<F extends (span: Span) => unknown>(\n name: string,\n options: SpanOptions,\n context: Context,\n fn: F\n ): ReturnType<F>;\n startActiveSpan<F extends (span: Span) => unknown>(\n name: string,\n options: unknown,\n context?: unknown,\n fn?: unknown\n ): ReturnType<F> {\n let opts: SpanOptions | undefined;\n let ctx: Context | undefined;\n let fun: F;\n if (typeof options === 'function') {\n fun = options as F;\n } else {\n opts = options as SpanOptions;\n if (typeof context === 'function') {\n fun = context as F;\n } else {\n ctx = context as Context;\n fun = fn as F;\n }\n }\n\n const span = this.startSpan(name, opts, ctx);\n return fun(span) as ReturnType<F>;\n }\n}\n\nexport class O11ySpan implements Span {\n private _isRecording: boolean = true;\n private attributes: Attributes = {};\n\n constructor(\n private activity: Activity,\n private logger: LoggerService\n ) {}\n\n spanContext(): SpanContext {\n return {\n traceId: this.activity.getRootId(),\n spanId: this.activity.getId(),\n traceFlags: this.activity.getIsSampled() ? 1 : 0,\n };\n }\n setAttribute(key: string, value: AttributeValue): this {\n this.attributes[key] = value;\n return this;\n }\n setAttributes(attributes: Attributes): this {\n this.attributes = { ...this.attributes, ...attributes };\n return this;\n }\n addEvent(\n _name: string,\n _attributesOrStartTime?: Attributes | TimeInput,\n _startTime?: TimeInput\n ): this {\n this.logger.warn('O11ySpan does not support addEvents.');\n return this;\n }\n setStatus(_status: SpanStatus): this {\n this.logger.warn('O11ySpan does not support setStatus.');\n return this;\n }\n updateName(_name: string): this {\n this.logger.warn('O11ySpan does not support updateName.');\n return this;\n }\n end(endTime?: TimeInput): void {\n let endTimeInternal = endTime;\n try {\n if (typeof endTimeInternal !== 'number') {\n this.logger.warn('O11ySpan does not support non number endTime override.');\n endTimeInternal = undefined;\n }\n\n // TODO: Add support for stop schemas and data\n this.activity.stop(\n undefined,\n undefined,\n endTimeInternal ? { perfStopOverride: endTimeInternal as number } : undefined\n );\n } finally {\n this._isRecording = false;\n }\n\n return;\n }\n\n isRecording(): boolean {\n return this._isRecording;\n }\n\n recordException(exception: Exception, _time?: TimeInput): void {\n // TODO: Add support for exception schemas and data\n this.activity.error(exception);\n }\n}\n","import { getInstrumentation } from 'o11y/client';\nimport type { LoggerService, NamedLoggerService } from '@conduit-client/utils';\nimport type { LuvioOTelMetricsAPI } from '@conduit-client/service-instrumentation-types/v1';\nimport type {\n Attributes,\n BatchObservableCallback,\n Context,\n Counter,\n Histogram,\n Meter,\n MeterOptions,\n MetricOptions,\n Observable,\n ObservableCallback,\n ObservableCounter,\n ObservableGauge,\n ObservableUpDownCounter,\n UpDownCounter,\n} from '@opentelemetry/api';\nimport { MetricsTags } from 'o11y/shared';\n\nexport class O11yOTelMetricsAPI implements LuvioOTelMetricsAPI {\n constructor(private services: NamedLoggerService) {}\n\n getMeter(\n name: string,\n // TODO: Not sure what to do with these?\n _version?: string | undefined,\n __options?: MeterOptions | undefined\n ): Meter {\n const o11yInstrumentation = getInstrumentation(name);\n return new O11yMeter(o11yInstrumentation, this.services.logger);\n }\n}\n\nexport class O11yMeter implements Meter {\n constructor(\n private o11yInstrumentation: ReturnType<typeof getInstrumentation>,\n private logger: LoggerService\n ) {}\n\n createHistogram<AttributesTypes extends Attributes = Attributes>(\n name: string,\n _options?: MetricOptions | undefined\n ): Histogram<AttributesTypes> {\n return new O11yHistogram(name, this.o11yInstrumentation, this.logger);\n }\n createCounter<AttributesTypes extends Attributes = Attributes>(\n name: string,\n options?: MetricOptions | undefined\n ): Counter<AttributesTypes> {\n if (options) {\n this.logger.warn('counter options not supported in O11y instrumentation');\n }\n return new O11yCounter(name, this.o11yInstrumentation, this.logger);\n }\n createUpDownCounter<AttributesTypes extends Attributes = Attributes>(\n _name: string,\n _options?: MetricOptions | undefined\n ): UpDownCounter<AttributesTypes> {\n return new O11yUpDownCounter(this.logger);\n }\n createObservableGauge<AttributesTypes extends Attributes = Attributes>(\n _name: string,\n _options?: MetricOptions | undefined\n ): ObservableGauge<AttributesTypes> {\n return new O11yObservableGuage(this.logger);\n }\n createObservableCounter<AttributesTypes extends Attributes = Attributes>(\n _name: string,\n _options?: MetricOptions | undefined\n ): ObservableCounter<AttributesTypes> {\n return new O11yObservableCounter(this.logger);\n }\n createObservableUpDownCounter<AttributesTypes extends Attributes = Attributes>(\n _name: string,\n _options?: MetricOptions | undefined\n ): ObservableUpDownCounter<AttributesTypes> {\n return new O11yObservableUpDownCounter(this.logger);\n }\n addBatchObservableCallback<AttributesTypes extends Attributes = Attributes>(\n _callback: BatchObservableCallback<AttributesTypes>,\n _observables: Observable<AttributesTypes>[]\n ): void {\n this.logger.warn('addBatchObservableCallback not supported yet');\n }\n removeBatchObservableCallback<AttributesTypes extends Attributes = Attributes>(\n _callback: BatchObservableCallback<AttributesTypes>,\n _observables: Observable<AttributesTypes>[]\n ): void {\n this.logger.warn('removeBatchObservableCallback not supported yet');\n }\n}\n\nclass O11yCounter implements Counter {\n constructor(\n private name: string,\n private o11yInstrumentation: ReturnType<typeof getInstrumentation>,\n private logger: LoggerService\n ) {}\n\n add(value: number, attributes?: Attributes | undefined, _context?: Context | undefined): void {\n if (value < 0) {\n this.logger.warn(`Counter values must be non-negative. Got ${value}.`);\n return;\n }\n this.o11yInstrumentation.incrementCounter(\n this.name,\n value,\n undefined,\n sanitizeAttributes(attributes)\n );\n }\n}\n\nclass O11yHistogram implements Histogram {\n constructor(\n private name: string,\n private o11yInstrumentation: ReturnType<typeof getInstrumentation>,\n private logger: LoggerService\n ) {}\n record(\n value: number,\n attributes?: Attributes | undefined,\n _context?: Context | undefined\n ): void {\n if (value < 0) {\n this.logger.warn(`Histogram values must be non-negative. Got ${value}.`);\n return;\n }\n this.o11yInstrumentation.trackValue(\n this.name,\n value,\n undefined,\n sanitizeAttributes(attributes)\n );\n }\n}\n\nclass O11yUpDownCounter implements UpDownCounter {\n constructor(private logger: LoggerService) {}\n\n add(\n _value: number,\n _attributes?: Attributes | undefined,\n _context?: Context | undefined\n ): void {\n this.logger.warn('O11yUpDownCounter not supported yet.');\n }\n}\n\nclass O11yObservableCounter implements ObservableCounter {\n constructor(private logger: LoggerService) {}\n addCallback(_callback: ObservableCallback<Attributes>): void {\n this.logger.warn('O11yObservableCounter not supported yet. Defaulting to noop.');\n }\n removeCallback(_callback: ObservableCallback<Attributes>): void {\n this.logger.warn('O11yObservableCounter not supported yet. Defaulting to noop.');\n }\n}\n\nclass O11yObservableGuage implements ObservableGauge {\n constructor(private logger: LoggerService) {}\n addCallback(_callback: ObservableCallback<Attributes>): void {\n this.logger.warn('O11yObservableGuage not supported yet. Defaulting to noop.');\n }\n removeCallback(_callback: ObservableCallback<Attributes>): void {\n this.logger.warn('O11yObservableGuage not supported yet. Defaulting to noop.');\n }\n}\n\nclass O11yObservableUpDownCounter implements ObservableUpDownCounter {\n constructor(private logger: LoggerService) {}\n addCallback(_callback: ObservableCallback<Attributes>): void {\n this.logger.warn('O11yObservableUpDownCounter not supported yet. Defaulting to noop.');\n }\n removeCallback(_callback: ObservableCallback<Attributes>): void {\n this.logger.warn('O11yObservableUpDownCounter not supported yet. Defaulting to noop.');\n }\n}\n\nexport function sanitizeAttributes(attributes?: Attributes | undefined): MetricsTags | undefined {\n if (!attributes) return;\n const metricTags: MetricsTags = {};\n Object.entries(attributes).forEach(([key, value]) => {\n if (value !== undefined && !Array.isArray(value)) {\n metricTags[key] = value;\n }\n });\n return metricTags;\n}\n","import { type LoggerService, type NamedLoggerService } from '@conduit-client/utils';\nimport { O11yOTelTraceAPI } from './o11y-trace-api';\nimport { O11yOTelMetricsAPI } from './o11y-metrics-api';\nimport type { InstrumentationServiceDescriptor } from '@conduit-client/service-instrumentation-types/v1';\n\nclass O11yInstrumentation {\n trace: O11yOTelTraceAPI;\n metrics: O11yOTelMetricsAPI;\n currentTimeMs = () => performance.now();\n\n constructor(private services: NamedLoggerService) {\n this.trace = new O11yOTelTraceAPI(this.services);\n this.metrics = new O11yOTelMetricsAPI(this.services);\n }\n}\n\nexport function buildServiceDescriptor(logger: LoggerService): InstrumentationServiceDescriptor {\n return {\n type: 'instrumentation',\n version: '1.0',\n service: new O11yInstrumentation({ logger }),\n };\n}\n"],"names":[],"mappings":";;;;;;AAsBO,MAAM,iBAA8C;AAAA,EACvD,YAAoB,UAA8B;AAA9B,SAAA,WAAA;AAAA,EAA+B;AAAA,EAEnD,UAAU,MAAc,UAAmB,UAAkC;AACzE,UAAM,sBAAsB,mBAAmB,IAAI;AACnD,WAAO,IAAI,WAAW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SAAO,QAAQ,UAAU,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACjD,QAAI,UAAU,UAAa,CAAC,MAAM,QAAQ,KAAK,GAAG;AAC9C,iBAAW,GAAG,IAAI;AAAA,IACtB;AAAA,EACJ,CAAC;AACD,SAAO;AACX;ACzLA,MAAM,oBAAoB;AAAA,EAKtB,YAAoB,UAA8B;AAA9B,SAAA,WAAA;AAFpB,SAAA,gBAAgB,MAAM,YAAY,IAAA;AAG9B,SAAK,QAAQ,IAAI,iBAAiB,KAAK,QAAQ;AAC/C,SAAK,UAAU,IAAI,mBAAmB,KAAK,QAAQ;AAAA,EACvD;AACJ;AAEO,SAAS,uBAAuB,QAAyD;AAC5F,SAAO;AAAA,IACH,MAAM;AAAA,IACN,SAAS;AAAA,IACT,SAAS,IAAI,oBAAoB,EAAE,QAAQ;AAAA,EAAA;AAEnD;"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@conduit-client/service-instrumentation-o11y",
3
+ "version": "2.0.0",
4
+ "private": false,
5
+ "description": "Luvio O11y Instrumentation Service implementation",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/salesforce-experience-platform-emu/luvio-next.git",
10
+ "directory": "packages/@conduit-client/services/instrumentation-o11y"
11
+ },
12
+ "license": "SEE LICENSE IN LICENSE.txt",
13
+ "exports": {
14
+ "./v1": {
15
+ "import": "./dist/v1/index.js",
16
+ "types": "./dist/types/v1/index.d.ts",
17
+ "require": "./dist/v1/index.js"
18
+ }
19
+ },
20
+ "main": "./dist/main/index.js",
21
+ "module": "./dist/main/index.js",
22
+ "types": "./dist/main/index.d.ts",
23
+ "files": [
24
+ "dist/"
25
+ ],
26
+ "scripts": {
27
+ "build": "vite build && tsc --build --emitDeclarationOnly",
28
+ "clean": "rm -rf dist",
29
+ "test": "vitest run",
30
+ "test:size": "size-limit",
31
+ "watch": "npm run build --watch"
32
+ },
33
+ "dependencies": {
34
+ "@conduit-client/service-instrumentation-types": "2.0.0",
35
+ "@conduit-client/utils": "2.0.0",
36
+ "@opentelemetry/api": "1.7.0",
37
+ "o11y": "252.7.0"
38
+ },
39
+ "volta": {
40
+ "extends": "../../../../package.json"
41
+ },
42
+ "size-limit": [
43
+ {
44
+ "path": "./dist/v1/index.js",
45
+ "limit": "2 kB"
46
+ }
47
+ ]
48
+ }