Skip to content

Commit

Permalink
feat!: updates to latest metric sdk and drop node-sdk dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pragmaticivan committed Nov 27, 2022
1 parent a9b4c6d commit 46c2df3
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 679 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class BookService {

```ts
import { MetricService } from 'nestjs-otel';
import { Counter } from '@opentelemetry/api-metrics';
import { Counter } from '@opentelemetry/api';

@Injectable()
export class BookService {
Expand Down Expand Up @@ -247,7 +247,7 @@ Example of usage:

```ts
import { OtelCounter } from 'nestjs-otel';
import { Counter } from '@opentelemetry/api-metrics';
import { Counter } from '@opentelemetry/api';

@Controller()
export class AppController {
Expand Down
675 changes: 78 additions & 597 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
},
"homepage": "https://github.com/pragmaticivan/nestjs-otel#readme",
"dependencies": {
"@opentelemetry/api": "^1.2.0",
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/api-metrics": "^0.33.0",
"@opentelemetry/host-metrics": "^0.30.1",
"@opentelemetry/host-metrics": "^0.31.0",
"response-time": "^2.3.2"
},
"devDependencies": {
Expand All @@ -49,9 +49,8 @@
"@nestjs/platform-express": "^9.2.0",
"@nestjs/platform-fastify": "^9.2.0",
"@nestjs/testing": "^9.2.0",
"@opentelemetry/exporter-prometheus": "^0.33.0",
"@opentelemetry/sdk-metrics": "^0.33.0",
"@opentelemetry/sdk-node": "^0.33.0",
"@opentelemetry/exporter-prometheus": "^0.34.0",
"@opentelemetry/sdk-metrics": "^1.8.0",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/jest": "^29.2.3",
"@types/node": "^18.11.9",
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/opentelemetry-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ModuleMetadata, Type, Abstract } from '@nestjs/common';
import { MetricAttributes } from '@opentelemetry/api-metrics';
import { MetricAttributes } from '@opentelemetry/api';
import { RouteInfo } from '@nestjs/common/interfaces';

export type OpenTelemetryModuleOptions = {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/decorators/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Counter, MetricOptions } from '@opentelemetry/api-metrics';
import { Counter, MetricOptions } from '@opentelemetry/api';
import { copyMetadataFromFunctionToFunction } from '../../opentelemetry.utils';
import { getOrCreateCounter } from '../metric-data';

Expand Down
2 changes: 1 addition & 1 deletion src/metrics/decorators/counter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createParamDecorator } from '@nestjs/common';
import { MetricOptions } from '@opentelemetry/api-metrics';
import { MetricOptions } from '@opentelemetry/api';
import { getOrCreateCounter, MetricType } from '../metric-data';

export const OtelCounter = createParamDecorator((name: string, options?: MetricOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/decorators/histogram.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createParamDecorator } from '@nestjs/common';
import { MetricOptions } from '@opentelemetry/api-metrics';
import { MetricOptions } from '@opentelemetry/api';
import { getOrCreateHistogram } from '../metric-data';

export const OtelHistogram = createParamDecorator((name: string, options?: MetricOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/decorators/observable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createParamDecorator } from '@nestjs/common';
import { MetricOptions } from '@opentelemetry/api-metrics';
import { MetricOptions } from '@opentelemetry/api';
import { getOrCreateObservableCounter, getOrCreateObservableGauge, getOrCreateObservableUpDownCounter } from '../metric-data';

export const OtelObservableGauge = createParamDecorator((name: string, options?: MetricOptions) => {
Expand Down
7 changes: 4 additions & 3 deletions src/metrics/metric-data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
Counter, MetricOptions, metrics, UpDownCounter,
Histogram, ObservableGauge, ObservableCounter, ObservableUpDownCounter, ObservableResult,
} from '@opentelemetry/api-metrics';
Counter, MetricOptions, UpDownCounter,
Histogram, ObservableGauge, ObservableCounter, ObservableUpDownCounter,
metrics,
} from '@opentelemetry/api';
import { OTEL_METER_NAME } from '../opentelemetry.constants';

export type GenericMetric =
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/metric.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import {
MetricOptions, ObservableResult,
} from '@opentelemetry/api-metrics';
MetricOptions,
} from '@opentelemetry/api';
import {
getOrCreateCounter, getOrCreateHistogram,
getOrCreateObservableCounter, getOrCreateObservableGauge,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/api-metrics.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as responseTime from 'response-time';
import * as urlParser from 'url';
import {
Counter, MetricAttributes, Histogram, UpDownCounter,
} from '@opentelemetry/api-metrics';
} from '@opentelemetry/api';
import { OpenTelemetryModuleOptions } from '../interfaces';
import { MetricService } from '../metrics/metric.service';
import { OPENTELEMETRY_MODULE_OPTIONS } from '../opentelemetry.constants';
Expand Down
2 changes: 1 addition & 1 deletion src/opentelemetry-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Provider, Type,
} from '@nestjs/common';
import { HostMetrics } from '@opentelemetry/host-metrics';
import { metrics } from '@opentelemetry/api-metrics';
import { metrics } from '@opentelemetry/api';
import { OpenTelemetryModuleAsyncOptions, OpenTelemetryModuleOptions, OpenTelemetryOptionsFactory } from './interfaces';
import { MetricService } from './metrics/metric.service';
import { ApiMetricsMiddleware } from './middleware';
Expand Down
11 changes: 5 additions & 6 deletions src/tracing/decorators/span.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'reflect-metadata';
import { SpanKind, SpanStatusCode } from '@opentelemetry/api';
import { tracing } from '@opentelemetry/sdk-node';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { InMemorySpanExporter, NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
import { SetMetadata } from '@nestjs/common';
import { Span } from './span';

Expand Down Expand Up @@ -31,14 +30,14 @@ class TestSpan {

describe('Span', () => {
let instance: TestSpan;
let traceExporter: tracing.InMemorySpanExporter;
let spanProcessor: tracing.SimpleSpanProcessor;
let traceExporter: InMemorySpanExporter;
let spanProcessor: SimpleSpanProcessor;
let provider: NodeTracerProvider;

beforeAll(async () => {
instance = new TestSpan();
traceExporter = new tracing.InMemorySpanExporter();
spanProcessor = new tracing.SimpleSpanProcessor(traceExporter);
traceExporter = new InMemorySpanExporter();
spanProcessor = new SimpleSpanProcessor(traceExporter);

provider = new NodeTracerProvider();
// TODO: figure out why that's failing with new version
Expand Down
51 changes: 21 additions & 30 deletions tests/e2e/metrics/decorators/common.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { metrics } from '@opentelemetry/api-metrics';
import { metrics } from '@opentelemetry/api';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { NodeSDK } from '@opentelemetry/sdk-node';
import * as request from 'supertest';
import { OpenTelemetryModule } from '../../../../src';
import { meterData } from '../../../../src/metrics/metric-data';
Expand All @@ -12,19 +11,27 @@ import { AppController } from '../../../fixture-app/app.controller';
describe('Common Decorators', () => {
let app: INestApplication;
let exporter: PrometheusExporter;
let otelSDK: NodeSDK;
let meterProvider: MeterProvider;

beforeEach(async () => {
exporter = new PrometheusExporter({
preventServerStart: true,
});

otelSDK = new NodeSDK({
metricReader: exporter,
beforeEach(done => {
exporter = new PrometheusExporter({}, () => {
meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
metrics.setGlobalMeterProvider(meterProvider);
done();
});
});

await otelSDK.start();
await exporter.startServer();
afterEach(async () => {
metrics.disable();
meterData.clear();
if (exporter) {
await exporter.stopServer();
await exporter.shutdown();
}
if (app) {
await app.close();
}
});

describe('Instance counter & Method counter', () => {
Expand All @@ -46,31 +53,15 @@ describe('Common Decorators', () => {
const agent = request(app.getHttpServer());
await agent.get('/example/4?foo=bar');

// Workaround for delay of metrics going to prometheus
await new Promise(resolve => setTimeout(resolve, 200));

// TODO: OpenTelemetry exporter does not expose server in a public function.
// @ts-ignore
// eslint-disable-next-line no-underscore-dangle
const { text } = await request(exporter._server)
.get('/metrics')
.expect(200);

expect(/app_AppController_instances_total 1/.test(text)).toBeTruthy();
expect(/app_AppController_example_calls_total 1/.test(text)).toBeTruthy();
});
});

afterEach(async () => {
metrics.disable();
meterData.clear();
if (exporter) {
await exporter.stopServer();
}
if (otelSDK) {
await otelSDK.shutdown();
}
if (app) {
await app.close();
}
});
});
48 changes: 22 additions & 26 deletions tests/e2e/metrics/metric.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { metrics } from '@opentelemetry/api-metrics';
import { metrics } from '@opentelemetry/api';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { OpenTelemetryModule } from '../../../src/opentelemetry.module';
import { meterData } from '../../../src/metrics/metric-data';
import { MetricService } from '../../../src/metrics/metric.service';
Expand All @@ -11,19 +11,27 @@ describe('MetricService', () => {
let metricService: MetricService;
let app: INestApplication;
let exporter: PrometheusExporter;
let otelSDK: NodeSDK;

beforeEach(async () => {
exporter = new PrometheusExporter({
preventServerStart: true,
});

otelSDK = new NodeSDK({
metricReader: exporter,
let meterProvider: MeterProvider;

beforeEach(done => {
exporter = new PrometheusExporter({}, () => {
meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
metrics.setGlobalMeterProvider(meterProvider);
done();
});
});

await otelSDK.start();
afterEach(async () => {
metrics.disable();
meterData.clear();
if (exporter) {
await exporter.stopServer();
await exporter.shutdown();
}
if (app) {
await app.close();
}
});

describe('instance', () => {
Expand Down Expand Up @@ -182,8 +190,7 @@ describe('MetricService', () => {
// Starts empty
expect(meterData.size).toBe(0);

const counter = metricService.getHistogram('test1');
// counter.clear();
metricService.getHistogram('test1');

// Has new key record
const data = meterData;
Expand All @@ -206,8 +213,7 @@ describe('MetricService', () => {

metricService = moduleRef.get<MetricService>(MetricService);

const counter = metricService.getHistogram('test1', { description: 'test1 description' });
// counter.clear();
metricService.getHistogram('test1', { description: 'test1 description' });

const existingCounter = metricService.getHistogram('test1');
expect(meterData.has('test1')).toBeTruthy();
Expand All @@ -218,14 +224,4 @@ describe('MetricService', () => {
expect(existingCounter._descriptor.description).toBe('test1 description');
});
});

afterEach(async () => {
metrics.disable();
if (otelSDK) {
await otelSDK.shutdown();
}
if (app) {
await app.close();
}
});
});
2 changes: 1 addition & 1 deletion tests/e2e/middleware/api-metrics.middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { metrics } from '@opentelemetry/api-metrics';
import { metrics } from '@opentelemetry/api';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { MetricService, OpenTelemetryModule } from '../../../src';
Expand Down

0 comments on commit 46c2df3

Please sign in to comment.