From 89233ca6880bb0cea97b727d00706e299e7a911e Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Thu, 11 Nov 2021 17:54:46 +0200 Subject: [PATCH] fix: add service dimension to metrics (#386) * fix: add service dimension to metrics * docs: fix section about service name --- docs/advanced-config.md | 2 +- package-lock.json | 2 +- src/metrics/index.ts | 30 +++++++++++++++++++++++------- src/options.ts | 2 +- test/metrics.test.ts | 12 +++++++++++- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/docs/advanced-config.md b/docs/advanced-config.md index 18a6b5d3..6c3529c6 100644 --- a/docs/advanced-config.md +++ b/docs/advanced-config.md @@ -69,7 +69,7 @@ The following config options can be set by passing them as arguments to `startTr - `signalfx`: A JS object with optional `client` and `dimensions` fields. If you have already setup a [SignalFx client](https://github.com/signalfx/signalfx-nodejs) with custom configuration, you can use this for sending instead of creating, configuring a new one. `dimensions` object adds a pre-defined dimension for each datapoint. The format for `dimensions` is `{key: value, ...}`. The following is a list of dimensions added by default: - - `host`: `os.hostname()` + - `service`: see [`serviceName`](#tracing) from the tracing section - `metric_source`: `splunk-otel-js` - `node_version`: `process.versions.node`, e.g. `16.10.0` diff --git a/package-lock.json b/package-lock.json index 23aaf9d2..21e403c9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -68,7 +68,7 @@ "winston": "3.3.3" }, "engines": { - "node": ">=8.5.0 <17" + "node": ">=8.5.0 <18" }, "peerDependencies": { "@opentelemetry/instrumentation-bunyan": ">=0.25.0 <1", diff --git a/src/metrics/index.ts b/src/metrics/index.ts index fab2b4af..ade8d8e3 100644 --- a/src/metrics/index.ts +++ b/src/metrics/index.ts @@ -17,12 +17,14 @@ import { context, diag } from '@opentelemetry/api'; import { suppressTracing } from '@opentelemetry/core'; import { collectMemoryInfo, MemoryInfo } from './memory'; -import { getEnvBoolean, getEnvNumber } from '../options'; -import * as os from 'os'; +import { defaultServiceName, getEnvBoolean, getEnvNumber } from '../options'; +import { EnvResourceDetector } from '../resource'; +import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import * as signalfx from 'signalfx'; interface MetricsOptions { enabled: boolean; + serviceName: string; accessToken: string; endpoint: string; exportInterval: number; @@ -281,11 +283,24 @@ export function _setDefaultOptions( options.endpoint || process.env.SPLUNK_METRICS_ENDPOINT || 'http://localhost:9943'; - const dimensions = Object.assign(options.signalfx?.dimensions || {}, { - host: os.hostname(), - metric_source: 'splunk-otel-js', - node_version: process.versions.node, - }); + + const resource = new EnvResourceDetector().detect(); + + const serviceName = String( + options.serviceName || + process.env.OTEL_SERVICE_NAME || + resource.attributes[SemanticResourceAttributes.SERVICE_NAME] || + defaultServiceName + ); + + const dimensions = Object.assign( + { + service: serviceName, + metric_source: 'splunk-otel-js', + node_version: process.versions.node, + }, + options.signalfx?.dimensions || {} + ); const sfxClient = options.signalfx?.client || @@ -296,6 +311,7 @@ export function _setDefaultOptions( return { enabled, + serviceName: serviceName, accessToken, endpoint, exportInterval: diff --git a/src/options.ts b/src/options.ts index d2b6a083..082a485e 100644 --- a/src/options.ts +++ b/src/options.ts @@ -37,7 +37,7 @@ import { import { SplunkBatchSpanProcessor } from './SplunkBatchSpanProcessor'; import { Resource } from '@opentelemetry/resources'; -const defaultServiceName = 'unnamed-node-service'; +export const defaultServiceName = 'unnamed-node-service'; type SpanExporterFactory = (options: Options) => SpanExporter; diff --git a/test/metrics.test.ts b/test/metrics.test.ts index 5f9d4a04..b9948bb0 100644 --- a/test/metrics.test.ts +++ b/test/metrics.test.ts @@ -95,13 +95,14 @@ describe('metrics', () => { it('has expected defaults', () => { const options = _setDefaultOptions(); assert.deepEqual(options.enabled, false); + assert.deepEqual(options.serviceName, 'unnamed-node-service'); assert.deepEqual(options.accessToken, ''); assert.deepEqual(options.endpoint, 'http://localhost:9943'); assert.deepEqual(options.exportInterval, 5000); const sfxClient = options.sfxClient; assert.deepStrictEqual(sfxClient['globalDimensions'], { - host: os.hostname(), + service: 'unnamed-node-service', metric_source: 'splunk-otel-js', node_version: process.versions.node, }); @@ -109,15 +110,24 @@ describe('metrics', () => { it('is possible to set options via env vars', () => { process.env.SPLUNK_ACCESS_TOKEN = 'foo'; + process.env.OTEL_SERVICE_NAME = 'bigmetric'; process.env.SPLUNK_METRICS_ENABLED = 'true'; process.env.SPLUNK_METRICS_ENDPOINT = 'http://localhost:9999'; process.env.SPLUNK_METRICS_EXPORT_INTERVAL = '1000'; const options = _setDefaultOptions(); assert.deepEqual(options.enabled, true); + assert.deepEqual(options.serviceName, 'bigmetric'); assert.deepEqual(options.accessToken, 'foo'); assert.deepEqual(options.endpoint, 'http://localhost:9999'); assert.deepEqual(options.exportInterval, 1000); + + const sfxClient = options.sfxClient; + assert.deepStrictEqual(sfxClient['globalDimensions'], { + service: 'bigmetric', + metric_source: 'splunk-otel-js', + node_version: process.versions.node, + }); }); });