Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics and auto instrumentation examples #5088

Open
jscancella opened this issue Oct 24, 2024 · 0 comments
Open

Metrics and auto instrumentation examples #5088

jscancella opened this issue Oct 24, 2024 · 0 comments

Comments

@jscancella
Copy link

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant