You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am looking for examples of how to auto instrument metrics. There are plenty of examples for manually adding metrics, but they are all manual. Ideally it would be just as easy as the auto instrumentation for tracing examples.
Here I have working code for manually adding metrics, how do I change it to auto instrument the metrics?
//mostly taken from https://github.com/open-telemetry/opentelemetry-js/blob/9cd2021119a2a189c53883d328c08f32140e075c/examples/otlp-exporter-node/metrics.js
import { environment } from 'src/environments/environment';
import status from 'src/environments/status.json';
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
//other optional exporters
//const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
//const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
import { ConsoleMetricExporter } from '@opentelemetry/sdk-metrics' ;
import {
ExponentialHistogramAggregation,
MeterProvider,
PeriodicExportingMetricReader,
View
} from '@opentelemetry/sdk-metrics';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
// Optional and only needed to see the internal diagnostic logging (during development)
//diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
const collectorOptions = {
url: environment.OTEL_COLLECTOR_URL + '/v1/metrics', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
headers: {}, // an optional object containing custom headers to be sent with each request
concurrencyLimit: 1, // an optional limit on pending requests
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
// Create an instance of the metric provider
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'TM-EADM',
[SemanticResourceAttributes.SERVICE_VERSION]: status.version,
}),
views: [],
});
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: metricExporter,
//exporter: new ConsoleMetricExporter(), //used for testing - print the metrics to the console
exportIntervalMillis: 5000 //export data to otel collector every 5 seconds
})
);
//fake some data, every 5 seconds add data to the metrics
const meter = meterProvider.getMeter('example-exporter-collector');
const requestCounter = meter.createCounter('requests', {
description: 'Example of a Counter',
});
const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {
description: 'Example of a UpDownCounter',
});
const histogram = meter.createHistogram('test_histogram', {
description: 'Example of a Histogram',
});
const exponentialHistogram = meter.createHistogram('test_exponential_histogram', {
description: 'Example of an ExponentialHistogram',
});
const attributes = { env: 'local' };
setInterval(() => {
requestCounter.add(1, attributes);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
histogram.record(Math.random(), attributes);
//exponentialHistogram.record(Math.random(), attributes);
}, 5000);
The text was updated successfully, but these errors were encountered:
I am looking for examples of how to auto instrument metrics. There are plenty of examples for manually adding metrics, but they are all manual. Ideally it would be just as easy as the auto instrumentation for tracing examples.
Here I have working code for manually adding metrics, how do I change it to auto instrument the metrics?
The text was updated successfully, but these errors were encountered: