@mereb/shared-packages 0.0.35 → 0.0.37
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.
|
@@ -5,4 +5,5 @@ export interface OtelConfig {
|
|
|
5
5
|
instrumentations?: Instrumentation[];
|
|
6
6
|
}
|
|
7
7
|
export declare function initTelemetry({ serviceName, otlpEndpoint, instrumentations }: OtelConfig): void;
|
|
8
|
+
export declare function initDefaultTelemetry(serviceName: string, instrumentations?: Instrumentation[]): void;
|
|
8
9
|
//# sourceMappingURL=otel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"otel.d.ts","sourceRoot":"","sources":["../../src/observability/otel.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"otel.d.ts","sourceRoot":"","sources":["../../src/observability/otel.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,gCAAgC,CAAC;AASpE,MAAM,WAAW,UAAU;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,gBAAgB,CAAC,EAAE,eAAe,EAAE,CAAC;CACxC;AA2BD,wBAAgB,aAAa,CAAC,EACI,WAAW,EACX,YAAY,EACZ,gBAAqB,EACxB,EAAE,UAAU,QA8B1C;AAED,wBAAgB,oBAAoB,CAChC,WAAW,EAAE,MAAM,EACnB,gBAAgB,GAAE,eAAe,EAAO,QAS3C"}
|
|
@@ -2,24 +2,60 @@ import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
|
|
|
2
2
|
import { Resource } from '@opentelemetry/resources';
|
|
3
3
|
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
4
4
|
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
5
|
-
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-
|
|
5
|
+
import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
6
6
|
import { registerInstrumentations } from '@opentelemetry/instrumentation';
|
|
7
|
+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
|
|
8
|
+
import { FastifyOtelInstrumentation } from '@fastify/otel';
|
|
7
9
|
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
|
|
8
10
|
let initialized = false;
|
|
11
|
+
function defaultEndpoint() {
|
|
12
|
+
const env = process.env.NODE_ENV ?? 'development';
|
|
13
|
+
const map = {
|
|
14
|
+
production: 'https://otel-collector.mereb.app',
|
|
15
|
+
staging: 'https://otel-collector-stg.mereb.app',
|
|
16
|
+
test: 'http://localhost:4318',
|
|
17
|
+
development: 'https://otel-collector-dev.mereb.app'
|
|
18
|
+
};
|
|
19
|
+
return map[env] ?? map.development;
|
|
20
|
+
}
|
|
21
|
+
function resolveEndpoint(override) {
|
|
22
|
+
const fromEnv = override ??
|
|
23
|
+
process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ??
|
|
24
|
+
process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
|
|
25
|
+
return fromEnv ?? defaultEndpoint();
|
|
26
|
+
}
|
|
27
|
+
function defaultInstrumentations() {
|
|
28
|
+
return [new HttpInstrumentation(), new FastifyOtelInstrumentation()];
|
|
29
|
+
}
|
|
9
30
|
export function initTelemetry({ serviceName, otlpEndpoint, instrumentations = [] }) {
|
|
10
31
|
if (initialized) {
|
|
11
32
|
return;
|
|
12
33
|
}
|
|
34
|
+
const endpoint = resolveEndpoint(otlpEndpoint);
|
|
13
35
|
const resource = new Resource({
|
|
14
36
|
'service.name': serviceName
|
|
15
37
|
});
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
38
|
+
const exporter = new OTLPHttpTraceExporter({
|
|
39
|
+
// the HTTP exporter appends /v1/traces when no path is present
|
|
40
|
+
url: endpoint
|
|
41
|
+
});
|
|
42
|
+
const provider = new NodeTracerProvider({
|
|
43
|
+
resource,
|
|
44
|
+
spanProcessors: [new BatchSpanProcessor(exporter)]
|
|
45
|
+
});
|
|
20
46
|
provider.register();
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
47
|
+
registerInstrumentations({
|
|
48
|
+
instrumentations: instrumentations.length
|
|
49
|
+
? instrumentations
|
|
50
|
+
: defaultInstrumentations()
|
|
51
|
+
});
|
|
24
52
|
initialized = true;
|
|
25
53
|
}
|
|
54
|
+
export function initDefaultTelemetry(serviceName, instrumentations = []) {
|
|
55
|
+
initTelemetry({
|
|
56
|
+
serviceName,
|
|
57
|
+
instrumentations: instrumentations.length > 0
|
|
58
|
+
? instrumentations
|
|
59
|
+
: defaultInstrumentations()
|
|
60
|
+
});
|
|
61
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mereb/shared-packages",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.37",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,14 +15,15 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@fastify/cors": "^9.0.1",
|
|
18
|
+
"@fastify/otel": "^0.16.0",
|
|
18
19
|
"@fastify/rate-limit": "^10.2.0",
|
|
19
20
|
"@fastify/sensible": "^5.6.0",
|
|
20
21
|
"@fastify/under-pressure": "^9.0.3",
|
|
21
22
|
"@opentelemetry/api": "^1.8.0",
|
|
22
23
|
"@opentelemetry/exporter-trace-otlp-grpc": "^0.52.0",
|
|
24
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
|
|
23
25
|
"@opentelemetry/host-metrics": "^0.36.2",
|
|
24
26
|
"@opentelemetry/instrumentation": "^0.52.0",
|
|
25
|
-
"@opentelemetry/instrumentation-fastify": "^0.52.0",
|
|
26
27
|
"@opentelemetry/instrumentation-http": "^0.52.0",
|
|
27
28
|
"@opentelemetry/resources": "^1.8.0",
|
|
28
29
|
"@opentelemetry/sdk-metrics": "^1.18.0",
|
|
@@ -41,7 +42,7 @@
|
|
|
41
42
|
"@types/node": "^20.12.7",
|
|
42
43
|
"@typescript-eslint/eslint-plugin": "^8.18.1",
|
|
43
44
|
"@typescript-eslint/parser": "^8.18.1",
|
|
44
|
-
"eslint": "^
|
|
45
|
+
"eslint": "^9.26.0",
|
|
45
46
|
"husky": "^9.1.7",
|
|
46
47
|
"rimraf": "^5.0.5",
|
|
47
48
|
"typescript": "^5.4.5",
|