-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add OpenTelemetry metrics support
This adds in support for OpenTelementry metrics.
- Loading branch information
1 parent
85bed10
commit f1bac27
Showing
16 changed files
with
518 additions
and
64 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 { | ||
OTLPMetricExporter | ||
} = require('@opentelemetry/exporter-metrics-otlp-proto'); | ||
const { CompressionAlgorithm } = require('@opentelemetry/otlp-exporter-base'); | ||
const { PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); | ||
const logger = require('@dotcom-reliability-kit/logger'); | ||
const { METRICS_USER_AGENT } = require('./user-agents'); | ||
|
||
/** | ||
* @typedef {object} MetricsOptions | ||
* @property {string} [apiGatewayKey] | ||
* The API key to send to the metrics collector if you're using the FT's official metrics collector endpoint. | ||
* @property {string} [endpoint] | ||
* The URL to send OpenTelemetry metrics to, for example http://localhost:4318/v1/metrics. | ||
*/ | ||
|
||
/** | ||
* Create an OpenTelemetry metrics configuration. | ||
* | ||
* @param {MetricsOptions} options | ||
* @returns {Partial<import('@opentelemetry/sdk-node').NodeSDKConfiguration>} | ||
*/ | ||
exports.createMetricsConfig = function createMetricsConfig(options) { | ||
/** @type {Partial<import('@opentelemetry/sdk-node').NodeSDKConfiguration>} */ | ||
const config = {}; | ||
|
||
// If we have an OpenTelemetry metrics endpoint then set it up | ||
if (options?.endpoint) { | ||
const headers = { | ||
'user-agent': METRICS_USER_AGENT | ||
}; | ||
if (options.apiGatewayKey) { | ||
headers['X-OTel-Key'] = options.apiGatewayKey; | ||
} | ||
config.metricReader = new PeriodicExportingMetricReader({ | ||
exporter: new OTLPMetricExporter({ | ||
url: options.endpoint, | ||
compression: CompressionAlgorithm.GZIP, | ||
headers | ||
}) | ||
}); | ||
|
||
logger.info({ | ||
event: 'OTEL_METRICS_STATUS', | ||
message: `OpenTelemetry metrics are enabled and exporting to endpoint ${options.endpoint}`, | ||
enabled: true, | ||
endpoint: options.endpoint | ||
}); | ||
} else { | ||
logger.warn({ | ||
event: 'OTEL_METRICS_STATUS', | ||
message: | ||
'OpenTelemetry metrics are disabled because no metrics endpoint was set', | ||
enabled: false, | ||
endpoint: null | ||
}); | ||
} | ||
|
||
return config; | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const appInfo = require('@dotcom-reliability-kit/app-info'); | ||
const packageJson = require('../../package.json'); | ||
const metricExporterPackageJson = require('@opentelemetry/exporter-metrics-otlp-proto/package.json'); | ||
const traceExporterPackageJson = require('@opentelemetry/exporter-trace-otlp-proto/package.json'); | ||
|
||
const BASE_USER_AGENT = `FTSystem/${appInfo.systemCode} (${packageJson.name}/${packageJson.version})`; | ||
|
||
exports.METRICS_USER_AGENT = `${BASE_USER_AGENT} (${metricExporterPackageJson.name}/${metricExporterPackageJson.version})`; | ||
exports.TRACING_USER_AGENT = `${BASE_USER_AGENT} (${traceExporterPackageJson.name}/${traceExporterPackageJson.version})`; |
Oops, something went wrong.