-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose metrics endpoint in nodejs functions (#17917)
- Loading branch information
Showing
7 changed files
with
112 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const opentelemetry = require('@opentelemetry/api'); | ||
const { MeterProvider } = require('@opentelemetry/sdk-metrics'); | ||
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); | ||
const { Resource } = require( '@opentelemetry/resources'); | ||
const { SemanticResourceAttributes } = require( '@opentelemetry/semantic-conventions'); | ||
|
||
let exporter; | ||
|
||
function setupMetrics(serviceName){ | ||
|
||
const resource = new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: serviceName, | ||
}); | ||
|
||
|
||
const myServiceMeterProvider = new MeterProvider({ | ||
resource, | ||
}); | ||
|
||
exporter = new PrometheusExporter({ preventServerStart: true}) | ||
|
||
myServiceMeterProvider.addMetricReader(exporter); | ||
|
||
opentelemetry.metrics.setGlobalMeterProvider(myServiceMeterProvider); | ||
|
||
} | ||
|
||
function createFunctionCallsTotalCounter(name){ | ||
const meter = opentelemetry.metrics.getMeter(name) | ||
return meter.createCounter('function_calls_total',{ | ||
description: 'Number of calls to user function', | ||
}); | ||
} | ||
|
||
|
||
function createFunctionFailuresTotalCounter(name){ | ||
const meter = opentelemetry.metrics.getMeter(name) | ||
return meter.createCounter('function_failures_total',{ | ||
description: 'Number of exceptions in user function', | ||
}); | ||
} | ||
|
||
function createFunctionDurationHistogram(name){ | ||
const meter = opentelemetry.metrics.getMeter(name) | ||
return meter.createHistogram("function_duration_miliseconds",{ | ||
description: 'Duration of user function in miliseconds', | ||
}); | ||
} | ||
|
||
const getMetrics = (req, res) => { | ||
exporter.getMetricsRequestHandler(req, res); | ||
}; | ||
|
||
module.exports = { | ||
setupMetrics, | ||
createFunctionCallsTotalCounter, | ||
createFunctionFailuresTotalCounter, | ||
createFunctionDurationHistogram, | ||
getMetrics, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters