@mereb/shared-packages 0.0.35 → 0.0.36

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;AAOpE,MAAM,WAAW,UAAU;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,gBAAgB,CAAC,EAAE,eAAe,EAAE,CAAC;CACxC;AAED,wBAAgB,aAAa,CAAC,EACI,WAAW,EACX,YAAY,EACZ,gBAAqB,EACxB,EAAE,UAAU,QA0B1C"}
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,QA+B1C;AAED,wBAAgB,oBAAoB,CAChC,WAAW,EAAE,MAAM,EACnB,gBAAgB,GAAE,eAAe,EAAO,QAS3C"}
@@ -2,24 +2,57 @@ 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-grpc';
5
+ import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
6
6
  import { registerInstrumentations } from '@opentelemetry/instrumentation';
7
+ import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
8
+ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
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 FastifyInstrumentation()];
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
38
  const provider = new NodeTracerProvider({ resource });
17
- if (otlpEndpoint) {
18
- provider.addSpanProcessor(new BatchSpanProcessor(new OTLPTraceExporter({ url: otlpEndpoint })));
19
- }
39
+ provider.addSpanProcessor(new BatchSpanProcessor(new OTLPHttpTraceExporter({
40
+ // the HTTP exporter appends /v1/traces when no path is present
41
+ url: endpoint
42
+ })));
20
43
  provider.register();
21
- if (instrumentations.length) {
22
- registerInstrumentations({ instrumentations });
23
- }
44
+ registerInstrumentations({
45
+ instrumentations: instrumentations.length
46
+ ? instrumentations
47
+ : defaultInstrumentations()
48
+ });
24
49
  initialized = true;
25
50
  }
51
+ export function initDefaultTelemetry(serviceName, instrumentations = []) {
52
+ initTelemetry({
53
+ serviceName,
54
+ instrumentations: instrumentations.length > 0
55
+ ? instrumentations
56
+ : defaultInstrumentations()
57
+ });
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mereb/shared-packages",
3
- "version": "0.0.35",
3
+ "version": "0.0.36",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,6 +20,7 @@
20
20
  "@fastify/under-pressure": "^9.0.3",
21
21
  "@opentelemetry/api": "^1.8.0",
22
22
  "@opentelemetry/exporter-trace-otlp-grpc": "^0.52.0",
23
+ "@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
23
24
  "@opentelemetry/host-metrics": "^0.36.2",
24
25
  "@opentelemetry/instrumentation": "^0.52.0",
25
26
  "@opentelemetry/instrumentation-fastify": "^0.52.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": "^8.57.0",
45
+ "eslint": "^9.26.0",
45
46
  "husky": "^9.1.7",
46
47
  "rimraf": "^5.0.5",
47
48
  "typescript": "^5.4.5",