Skip to content

Commit

Permalink
fix: add service dimension to metrics (#386)
Browse files Browse the repository at this point in the history
* fix: add service dimension to metrics

* docs: fix section about service name
  • Loading branch information
seemk authored Nov 11, 2021
1 parent a2d5b94 commit 89233ca
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 23 additions & 7 deletions src/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand All @@ -296,6 +311,7 @@ export function _setDefaultOptions(

return {
enabled,
serviceName: serviceName,
accessToken,
endpoint,
exportInterval:
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 11 additions & 1 deletion test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,39 @@ 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,
});
});

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,
});
});
});

Expand Down

0 comments on commit 89233ca

Please sign in to comment.