@mereb/shared-packages 0.0.34 → 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.
@@ -1,4 +1,4 @@
1
- import { type Consumer, Kafka, type KafkaConfig, type Producer } from 'kafkajs';
1
+ import { type Consumer, type KafkaConfig, type Producer, Kafka } from 'kafkajs';
2
2
  export declare function getKafka(config: KafkaConfig): Kafka;
3
3
  export declare function getProducer(config: KafkaConfig): Promise<Producer>;
4
4
  export declare function createConsumer(config: KafkaConfig, groupId: string): Promise<Consumer>;
@@ -21,5 +21,6 @@ export type KafkaEnvConfigOptions = {
21
21
  sslInsecureDefault?: boolean;
22
22
  };
23
23
  export declare function buildKafkaConfigFromEnv(opts?: KafkaEnvConfigOptions): KafkaConfig | null;
24
+ export declare function ensureTopicExists(config: KafkaConfig, topic: string, partitions?: number, replicationFactor?: number): Promise<void>;
24
25
  export declare function disconnectProducer(): Promise<void>;
25
26
  //# sourceMappingURL=kafka.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"kafka.d.ts","sourceRoot":"","sources":["../../src/messaging/kafka.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,QAAQ,EAAE,KAAK,EAAE,KAAK,WAAW,EAAgB,KAAK,QAAQ,EAAC,MAAM,SAAS,CAAC;AAK5F,wBAAgB,QAAQ,CAAC,MAAM,EAAE,WAAW,SAG3C;AAED,wBAAsB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CASxE;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAK5F;AAED,MAAM,MAAM,qBAAqB,GAAG;IAChC;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,wBAAgB,uBAAuB,CACnC,IAAI,GAAE,qBAA0B,GACjC,WAAW,GAAG,IAAI,CA8BpB;AAED,wBAAsB,kBAAkB,kBAKvC"}
1
+ {"version":3,"file":"kafka.d.ts","sourceRoot":"","sources":["../../src/messaging/kafka.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,EAER,MAAM,SAAS,CAAC;AAKjB,wBAAgB,QAAQ,CAAC,MAAM,EAAE,WAAW,SAG3C;AAED,wBAAsB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CASxE;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAK5F;AAED,MAAM,MAAM,qBAAqB,GAAG;IAChC;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,wBAAgB,uBAAuB,CACnC,IAAI,GAAE,qBAA0B,GACjC,WAAW,GAAG,IAAI,CA8BpB;AAED,wBAAsB,iBAAiB,CACnC,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,MAAM,EACb,UAAU,SAAI,EACd,iBAAiB,SAAI,GACtB,OAAO,CAAC,IAAI,CAAC,CAiBf;AAED,wBAAsB,kBAAkB,kBAKvC"}
@@ -45,6 +45,24 @@ export function buildKafkaConfigFromEnv(opts = {}) {
45
45
  : undefined
46
46
  };
47
47
  }
48
+ export async function ensureTopicExists(config, topic, partitions = 1, replicationFactor = 1) {
49
+ const kafka = getKafka(config);
50
+ const admin = kafka.admin();
51
+ await admin.connect();
52
+ const existing = await admin.listTopics();
53
+ if (!existing.includes(topic)) {
54
+ await admin.createTopics({
55
+ topics: [
56
+ {
57
+ topic,
58
+ numPartitions: partitions,
59
+ replicationFactor
60
+ }
61
+ ]
62
+ });
63
+ }
64
+ await admin.disconnect();
65
+ }
48
66
  export async function disconnectProducer() {
49
67
  if (producerInstance) {
50
68
  await producerInstance.disconnect();
@@ -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.34",
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",