@lazyapps/observability 0.1.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/config.js ADDED
@@ -0,0 +1,48 @@
1
+ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
2
+ import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
3
+ import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
4
+ import { AmqplibInstrumentation } from '@opentelemetry/instrumentation-amqplib';
5
+ import { SocketIoInstrumentation } from '@opentelemetry/instrumentation-socket.io';
6
+
7
+ const defaults = {
8
+ serviceName: 'unknown-service',
9
+ serviceVersion: undefined,
10
+ environment: undefined,
11
+ otlp: {
12
+ endpoint: 'http://localhost:4317',
13
+ protocol: 'grpc',
14
+ insecure: true,
15
+ },
16
+ traces: true,
17
+ metrics: true,
18
+ logs: true,
19
+ sampler: {
20
+ type: 'always_on',
21
+ ratio: 1.0,
22
+ },
23
+ diagnosticLogLevel: 'WARN',
24
+ };
25
+
26
+ const createInstrumentations = () => [
27
+ new HttpInstrumentation(),
28
+ new ExpressInstrumentation(),
29
+ new MongoDBInstrumentation(),
30
+ new AmqplibInstrumentation(),
31
+ new SocketIoInstrumentation(),
32
+ ];
33
+
34
+ export const createConfig = (userConfig = {}) => ({
35
+ ...defaults,
36
+ ...userConfig,
37
+ otlp: {
38
+ ...defaults.otlp,
39
+ ...(userConfig.otlp || {}),
40
+ },
41
+ sampler: {
42
+ ...defaults.sampler,
43
+ ...(userConfig.sampler || {}),
44
+ },
45
+ instrumentations: userConfig.instrumentations || createInstrumentations(),
46
+ });
47
+
48
+ export const __testing__ = { defaults, createInstrumentations };
package/exporters.js ADDED
@@ -0,0 +1,26 @@
1
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
2
+ import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
3
+ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc';
4
+ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
5
+
6
+ const createOtlpConfig = (config) => ({
7
+ url: config.otlp.endpoint,
8
+ timeoutMillis: 5000,
9
+ });
10
+
11
+ export const createExporters = (config) => {
12
+ const otlpConfig = createOtlpConfig(config);
13
+
14
+ return {
15
+ trace: config.traces ? new OTLPTraceExporter(otlpConfig) : undefined,
16
+ metrics: config.metrics
17
+ ? new PeriodicExportingMetricReader({
18
+ exporter: new OTLPMetricExporter(otlpConfig),
19
+ exportIntervalMillis: 15000,
20
+ })
21
+ : undefined,
22
+ logs: config.logs ? new OTLPLogExporter(otlpConfig) : undefined,
23
+ };
24
+ };
25
+
26
+ export const __testing__ = { createOtlpConfig };
package/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import { NodeSDK } from '@opentelemetry/sdk-node';
2
+ import { createConfig } from './config.js';
3
+ import { createResource } from './resource.js';
4
+ import { createExporters } from './exporters.js';
5
+
6
+ export { createConfig } from './config.js';
7
+ export { createResource } from './resource.js';
8
+ export { createExporters } from './exporters.js';
9
+
10
+ let initialized = false;
11
+
12
+ export const isInitialized = () => initialized;
13
+
14
+ export const __resetForTesting = () => {
15
+ initialized = false;
16
+ };
17
+
18
+ export const initialize = (userConfig) => {
19
+ if (initialized) {
20
+ return;
21
+ }
22
+ initialized = true;
23
+
24
+ const config = createConfig(userConfig);
25
+ const resource = createResource(config);
26
+ const exporters = createExporters(config);
27
+
28
+ const sdk = new NodeSDK({
29
+ resource,
30
+ traceExporter: exporters.trace,
31
+ metricReader: exporters.metrics,
32
+ logRecordExporter: exporters.logs,
33
+ instrumentations: config.instrumentations,
34
+ });
35
+
36
+ sdk.start();
37
+
38
+ return sdk;
39
+ };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@lazyapps/observability",
3
+ "version": "0.1.0",
4
+ "description": "OpenTelemetry SDK initialization and configuration for the LazyApps framework",
5
+ "main": "index.js",
6
+ "files": [
7
+ "*.js"
8
+ ],
9
+ "scripts": {
10
+ "test": "vitest"
11
+ },
12
+ "keywords": [
13
+ "event-sourcing",
14
+ "cqrs",
15
+ "lazyapps",
16
+ "opentelemetry",
17
+ "observability",
18
+ "tracing",
19
+ "metrics"
20
+ ],
21
+ "author": "Oliver Sturm",
22
+ "license": "ISC",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/oliversturm/lazyapps-libs.git",
26
+ "directory": "packages/observability"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/oliversturm/lazyapps-libs/issues"
30
+ },
31
+ "homepage": "https://github.com/oliversturm/lazyapps-libs/tree/main/packages/observability#readme",
32
+ "engines": {
33
+ "node": ">=18.20.3 || >=20.18.0"
34
+ },
35
+ "devDependencies": {
36
+ "eslint": "^8.46.0",
37
+ "vitest": "^4.0.18"
38
+ },
39
+ "dependencies": {
40
+ "@opentelemetry/api": "^1.9.0",
41
+ "@opentelemetry/sdk-node": "^0.211.0",
42
+ "@opentelemetry/sdk-metrics": "^2.5.0",
43
+ "@opentelemetry/sdk-logs": "^0.211.0",
44
+ "@opentelemetry/resources": "^2.5.0",
45
+ "@opentelemetry/semantic-conventions": "^1.39.0",
46
+ "@opentelemetry/exporter-trace-otlp-grpc": "^0.211.0",
47
+ "@opentelemetry/exporter-metrics-otlp-grpc": "^0.211.0",
48
+ "@opentelemetry/exporter-logs-otlp-grpc": "^0.211.0",
49
+ "@opentelemetry/instrumentation-http": "^0.211.0",
50
+ "@opentelemetry/instrumentation-express": "^0.59.0",
51
+ "@opentelemetry/instrumentation-mongodb": "^0.64.0",
52
+ "@opentelemetry/instrumentation-amqplib": "^0.58.0",
53
+ "@opentelemetry/instrumentation-socket.io": "^0.57.0"
54
+ },
55
+ "type": "module"
56
+ }
package/resource.js ADDED
@@ -0,0 +1,16 @@
1
+ import { resourceFromAttributes } from '@opentelemetry/resources';
2
+ import {
3
+ ATTR_SERVICE_NAME,
4
+ ATTR_SERVICE_VERSION,
5
+ } from '@opentelemetry/semantic-conventions';
6
+
7
+ export const createResource = (config) =>
8
+ resourceFromAttributes({
9
+ [ATTR_SERVICE_NAME]: config.serviceName,
10
+ ...(config.serviceVersion && {
11
+ [ATTR_SERVICE_VERSION]: config.serviceVersion,
12
+ }),
13
+ ...(config.environment && {
14
+ 'deployment.environment.name': config.environment,
15
+ }),
16
+ });