Skip to content

Commit

Permalink
feat: Improve histogram metric (#1033)
Browse files Browse the repository at this point in the history
* Use exponential histogram for automatic bucket boundaries that make
  sense. Previously, the top bucket was 1000 ms, which is not enough.
* Rename metric because it’s not a counter.
* Support `OTEL_METRIC_EXPORT_INTERVAL` env var because 
  opentelemetry-js itself apparently doesn’t.
* Disable OTEL exporter in test to solve #1030.
  • Loading branch information
ddeboer committed Jul 21, 2023
1 parent 83de4b3 commit 097ae8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default {
],
coverageThreshold: {
global: {
lines: 91.12,
statements: 91.12,
branches: 94.13,
lines: 90.84,
statements: 90.84,
branches: 93.81,
functions: 89.79,
},
},
Expand Down
29 changes: 23 additions & 6 deletions packages/network-of-terms-query/src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import {
ExponentialHistogramAggregation,
MeterProvider,
PeriodicExportingMetricReader,
View,
} from '@opentelemetry/sdk-metrics';
import {Resource} from '@opentelemetry/resources';
import {OTLPMetricExporter} from '@opentelemetry/exporter-metrics-otlp-proto';
import {SemanticResourceAttributes} from '@opentelemetry/semantic-conventions';
import {metrics, ValueType} from '@opentelemetry/api';

const sourceQueriesHistogramName = 'queries.source';

const meterProvider = new MeterProvider({
resource: Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'network-of-terms',
})
),
views: [
new View({
aggregation: new ExponentialHistogramAggregation(10),
instrumentName: sourceQueriesHistogramName,
}),
],
});
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
})
);

if ('test' !== process.env.NODE_ENV) {
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis:
(process.env.OTEL_METRIC_EXPORT_INTERVAL as unknown as number) ?? 60000,
})
);
}
metrics.setGlobalMeterProvider(meterProvider);

const meter = metrics.getMeter('default');
Expand All @@ -32,8 +47,10 @@ export const clientQueriesCounter = meter.createCounter(
);

export const sourceQueriesHistogram = meter.createHistogram(
'queries.source.counter',
sourceQueriesHistogramName,
{
description: 'Queries to terminology sources and their response times',
valueType: ValueType.INT,
unit: 'ms',
}
);

0 comments on commit 097ae8c

Please sign in to comment.