Closed jscancella closed 1 hour ago
since you mention the use of angular.js the browser based documentation is probably what you are looking for:
https://opentelemetry.io/docs/languages/js/getting-started/browser/
I don't think I want the browser one. This code is working, I just want it to be auto instrumented for metrics instead of having to manually do it myself
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);
but if you want to run it in nodejs why are you running it through webpack, and why are you mentioning angular?
but if you want to run it in nodejs why are you running it through webpack, and why are you mentioning angular?
because I am new to the javascript world and don't really know what I am doing 😄
Ok, that's not really something we (OpenTelemetry) can help with, but some pointers:
if your application is browser-based, you follow the instructions I showed above for the browser. An important difference is that not all exporters, especially grpc are supported: https://opentelemetry.io/docs/languages/js/exporters/#usage-in-the-browser
if your application is nodejs-based and therefore running outside of the browser, follow the Node.JS instructions.
I did ask in the javascript specific discussions but no one answered, and have been trying to follow the examples that the otel team has written, but I still can't get the metrics to auto instrument.
If you can point me to what I need to change to get auto instrumentation to work with the above code I would be really grateful!
Sorry, I am not really the right person to ask that, you may have more luck in the #otel-js slack channel (register via https://slack.cncf.io) or on stackoverflow
Ok i'll try there. thank you!
Hello,
While trying to follow https://opentelemetry.io/docs/languages/js/getting-started/nodejs/#setup in my own angular application I am getting compile errors. When I then try and install the missing dependencies, it continues to have the same compiler errors.
NPM version: 10.5.0 Node version: v18.20.0
Expected results: I include the same code as the example and get traces and metrics auto instrumented Actual results: