@devopsplaybook.io/otel-utils 1.4.0 → 1.5.0-beta.33.0612f7c
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/README.md +1 -1
- package/dist/src/StandardMeter.d.ts +11 -1
- package/dist/src/StandardMeter.js +2 -2
- package/package.json +1 -1
- package/src/StandardMeter.spec.ts +18 -0
- package/src/StandardMeter.ts +18 -2
package/README.md
CHANGED
|
@@ -121,7 +121,7 @@ const cpuGauge = meter.createObservableGauge(
|
|
|
121
121
|
|
|
122
122
|
- `createCounter(key)` — Creates a `Counter` with name `${serviceName}.${key}`.
|
|
123
123
|
- `createUpDownCounter(key)` — Creates an `UpDownCounter`.
|
|
124
|
-
- `createHistogram(key)` — Creates a `Histogram
|
|
124
|
+
- `createHistogram(key, options?)` — Creates a `Histogram` named `${serviceName}.${key}`; with `{ unprefixed: true }` the histogram is created exactly as `key`, without the service-name prefix.
|
|
125
125
|
- `createObservableGauge(key, callback, description?)` — Creates an `ObservableGauge` with a callback. Description is optional.
|
|
126
126
|
- `shutdown()` — Flushes buffered metrics and shuts the meter provider down.
|
|
127
127
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { Counter, Histogram, ObservableGauge, UpDownCounter } from "@opentelemetry/api";
|
|
2
2
|
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
3
|
+
/**
|
|
4
|
+
* Options accepted by {@link StandardMeter.createHistogram}.
|
|
5
|
+
*/
|
|
6
|
+
export interface StandardMeterCreateHistogramOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Create the histogram without the `${serviceName}.` prefix:
|
|
9
|
+
* exported exactly as `key`.
|
|
10
|
+
*/
|
|
11
|
+
unprefixed?: boolean;
|
|
12
|
+
}
|
|
3
13
|
export declare class StandardMeter {
|
|
4
14
|
private meter;
|
|
5
15
|
private meterProvider;
|
|
@@ -9,6 +19,6 @@ export declare class StandardMeter {
|
|
|
9
19
|
shutdown(): Promise<void>;
|
|
10
20
|
createCounter(key: string): Counter;
|
|
11
21
|
createUpDownCounter(key: string): UpDownCounter;
|
|
12
|
-
createHistogram(key: string): Histogram;
|
|
22
|
+
createHistogram(key: string, options?: StandardMeterCreateHistogramOptions): Histogram;
|
|
13
23
|
createObservableGauge(key: string, callback: (observableResult: any) => void, description?: string): ObservableGauge;
|
|
14
24
|
}
|
|
@@ -46,8 +46,8 @@ class StandardMeter {
|
|
|
46
46
|
createUpDownCounter(key) {
|
|
47
47
|
return this.meter.createUpDownCounter(`${this.serviceName}.${key}`);
|
|
48
48
|
}
|
|
49
|
-
createHistogram(key) {
|
|
50
|
-
return this.meter.createHistogram(`${this.serviceName}.${key}`);
|
|
49
|
+
createHistogram(key, options) {
|
|
50
|
+
return this.meter.createHistogram((options === null || options === void 0 ? void 0 : options.unprefixed) ? key : `${this.serviceName}.${key}`);
|
|
51
51
|
}
|
|
52
52
|
createObservableGauge(key,
|
|
53
53
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/package.json
CHANGED
|
@@ -97,6 +97,24 @@ describe("StandardMeter", () => {
|
|
|
97
97
|
);
|
|
98
98
|
});
|
|
99
99
|
|
|
100
|
+
it("creates a histogram without the service-name prefix when the unprefixed option is set", async () => {
|
|
101
|
+
const meter = new StandardMeter({
|
|
102
|
+
SERVICE_ID: "metric-service",
|
|
103
|
+
VERSION: "1.0.0",
|
|
104
|
+
OPENTELEMETRY_COLLECTOR_HTTP_METRICS: "http://collector:4318/v1/metrics",
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
meter.createHistogram("session.duration", { unprefixed: true }).record(1);
|
|
108
|
+
meter.createHistogram("requests.latency").record(42);
|
|
109
|
+
|
|
110
|
+
await meter.shutdown();
|
|
111
|
+
|
|
112
|
+
const names = metricNames(lastExporter());
|
|
113
|
+
expect(names).toContain("session.duration");
|
|
114
|
+
expect(names).toContain("metric-service.requests.latency");
|
|
115
|
+
expect(names).not.toContain("metric-service.session.duration");
|
|
116
|
+
});
|
|
117
|
+
|
|
100
118
|
it("works without a collector and without a gauge description", async () => {
|
|
101
119
|
const meter = new StandardMeter({
|
|
102
120
|
SERVICE_ID: "metric-service",
|
package/src/StandardMeter.ts
CHANGED
|
@@ -13,6 +13,17 @@ import {
|
|
|
13
13
|
} from "@opentelemetry/sdk-metrics";
|
|
14
14
|
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Options accepted by {@link StandardMeter.createHistogram}.
|
|
18
|
+
*/
|
|
19
|
+
export interface StandardMeterCreateHistogramOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Create the histogram without the `${serviceName}.` prefix:
|
|
22
|
+
* exported exactly as `key`.
|
|
23
|
+
*/
|
|
24
|
+
unprefixed?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
16
27
|
export class StandardMeter {
|
|
17
28
|
private meter: Meter;
|
|
18
29
|
private meterProvider: MeterProvider;
|
|
@@ -66,8 +77,13 @@ export class StandardMeter {
|
|
|
66
77
|
return this.meter.createUpDownCounter(`${this.serviceName}.${key}`);
|
|
67
78
|
}
|
|
68
79
|
|
|
69
|
-
public createHistogram(
|
|
70
|
-
|
|
80
|
+
public createHistogram(
|
|
81
|
+
key: string,
|
|
82
|
+
options?: StandardMeterCreateHistogramOptions,
|
|
83
|
+
): Histogram {
|
|
84
|
+
return this.meter.createHistogram(
|
|
85
|
+
options?.unprefixed ? key : `${this.serviceName}.${key}`,
|
|
86
|
+
);
|
|
71
87
|
}
|
|
72
88
|
|
|
73
89
|
public createObservableGauge(
|