The following declarations are essential for setting up custom instrumentation with the OTEL node.js agent extension:
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { metrics } = require('@opentelemetry/api');
@opentelemetry/sdk-metrics: Provides the MeterProvider, which is responsible for managing metric collection.
@opentelemetry/api: Contains the core OpenTelemetry API, including the metrics module to interact with the global metrics API.
metrics.setGlobalMeterProvider(meterProvider)
metrics.setGlobalMeterProvider(meterProvider);
This step registers the MeterProvider as the global provider. This ensures that any Meter retrieved using the OpenTelemetry API is created by this MeterProvider.
const meter = metrics.getMeter('@sap-cloud-alm/instrumentation-metrics:manual');
The getMeter method initializes the meter with scope “@sap-cloud-alm/instrumentation-metrics:manual“. This meter is used to collect and report custom metrics specific to the application.
These declarations ensure that your Node.js microservice can use OpenTelemetry to capture and export custom metrics effectively.
const successfulJobCounter = meter.createObservableGauge('custom.successful.jobs', {
description: 'successful jobs'
});
const failedJobCounter = meter.createObservableGauge('custom.failed.jobs', {
description:'failed jobs'
});
Create an ObservableGauge for successful and failed jobs.
let successJobs = 0;
let failedJobs = 0;
successfulJobCounter.addCallback(observableResult => {
// Pass the value of successfulJobs to the observable gauge
observableResult.observe(successJobs);
});
failedJobCounter.addCallback(observableResult => {
// Pass the value of successfulJobs to the observable gauge
observableResult.observe(failedJobs);
});
Set up the observation logic.
cds.on("bootstrap", (cdsApp) => {
cdsApp.use((req, res, next) => {
//Example: Increment counters based on request success or failure
successJobs += 1; // Update based on real logic
failedJobs += 1;
next();
});
// Use the Express app as middleware for the CDS server
cdsApp.use(app);
});
successJobs and failedJobs are updated in middleware or event listeners, ensuring metrics reflect real system behavior.
app.get('/testCounters', (req, res) => {
res.send(`Successful Jobs: ${successJobs}, Failed Jobs: ${ failedJobs }`);
});
Test endpoint to trigger requests to update counters.