Skip to content

Commit

Permalink
feat: add SPLUNK_REALM for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Aug 25, 2022
1 parent 7c66c95 commit 1965158
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
26 changes: 22 additions & 4 deletions src/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as signalfx from 'signalfx';

export interface MetricsOptions {
accessToken: string;
realm?: string;
endpoint: string;
serviceName: string;
// Metrics-specific configuration options:
Expand Down Expand Up @@ -83,6 +84,7 @@ export type StartMetricsOptions = Partial<MetricsOptions> & {

export const allowedMetricsOptions = [
'accessToken',
'realm',
'endpoint',
'exportInterval',
'serviceName',
Expand Down Expand Up @@ -284,10 +286,26 @@ export function _setDefaultOptions(
): MetricsOptions & { sfxClient: signalfx.SignalClient } {
const accessToken =
options.accessToken || process.env.SPLUNK_ACCESS_TOKEN || '';
const endpoint =
options.endpoint ||
process.env.SPLUNK_METRICS_ENDPOINT ||
'http://localhost:9943';

let endpoint = options.endpoint || process.env.SPLUNK_METRICS_ENDPOINT;

const realm = options.realm || process.env.SPLUNK_REALM || '';

if (realm) {
if (!accessToken) {
throw new Error(
'Splunk realm is set, but access token is unset. To send metrics to the Observability Cloud, both need to be set'
);
}

if (!endpoint) {
endpoint = `https://ingest.${realm}.signalfx.com/v2/datapoint`;
}
}

if (!endpoint) {
endpoint = 'http://localhost:9943';
}

const resource = detectResource();

Expand Down
4 changes: 2 additions & 2 deletions src/tracing/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function _setDefaultOptions(options: Partial<Options> = {}): Options {
if (options.realm) {
if (!options.accessToken) {
throw new Error(
'Splunk realm is set, but access token is unset. To send data to the Observability Cloud, both need to be set'
'Splunk realm is set, but access token is unset. To send traces to the Observability Cloud, both need to be set'
);
}

Expand Down Expand Up @@ -272,7 +272,7 @@ function resolveExporterType(options: Partial<Options>): ExporterType {
if (tracesExporter) {
if (!isSupportedRealmExporter(tracesExporter)) {
throw new Error(
'Setting the Splunk realm with an explicit OTEL_TRACES_EXPORTER requires OTEL_TRACES_EXPORTER to be either otlp-http jaeger-thrift-splunk'
'Setting the Splunk realm with an explicit OTEL_TRACES_EXPORTER requires OTEL_TRACES_EXPORTER to be either otlp-http or jaeger-thrift-splunk'
);
}
} else {
Expand Down
26 changes: 26 additions & 0 deletions test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ describe('metrics', () => {
node_version: process.versions.node,
});
});

it('throws when realm is set without an access token', () => {
process.env.SPLUNK_REALM = 'eu0';
assert.throws(
_setDefaultOptions,
/To send metrics to the Observability Cloud/
);
});

it('chooses the correct endpoint when realm is set', () => {
process.env.SPLUNK_REALM = 'eu0';
process.env.SPLUNK_ACCESS_TOKEN = 'abc';
const options = _setDefaultOptions();
assert.deepStrictEqual(
options.endpoint,
'https://ingest.eu0.signalfx.com/v2/datapoint'
);
});

it('prefers user endpoint when realm is set', () => {
process.env.SPLUNK_REALM = 'eu0';
process.env.SPLUNK_ACCESS_TOKEN = 'abc';
process.env.SPLUNK_METRICS_ENDPOINT = 'http://localhost:9999';
const options = _setDefaultOptions();
assert.deepStrictEqual(options.endpoint, 'http://localhost:9999');
});
});

describe('startMetrics', () => {
Expand Down
36 changes: 31 additions & 5 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,25 @@ describe('options', () => {

it('throws when setting SPLUNK_REALM without an access token', () => {
process.env.SPLUNK_REALM = 'us0';
assert.throws(_setDefaultOptions, /Splunk realm is set, but access token is unset/);
assert.throws(
_setDefaultOptions,
/Splunk realm is set, but access token is unset/
);
});

it('chooses the correct OTLP endpoint when realm is set', () => {
process.env.SPLUNK_REALM = 'us0';
process.env.SPLUNK_ACCESS_TOKEN = 'abc';

const options = _setDefaultOptions();
assert.deepStrictEqual(options.endpoint, 'https://ingest.us0.signalfx.com/v2/trace/otlp');
assert.deepStrictEqual(options.spanExporterFactory, otlpHttpSpanExporterFactory);
assert.deepStrictEqual(
options.endpoint,
'https://ingest.us0.signalfx.com/v2/trace/otlp'
);
assert.deepStrictEqual(
options.spanExporterFactory,
otlpHttpSpanExporterFactory
);
});

it('chooses the correct Jaeger Thrift endpoint when realm is set', () => {
Expand All @@ -244,8 +253,25 @@ describe('options', () => {
process.env.OTEL_TRACES_EXPORTER = 'jaeger-thrift-http';

const options = _setDefaultOptions();
assert.deepStrictEqual(options.endpoint, 'https://ingest.us0.signalfx.com/v2/trace/jaegerthrift');
assert.deepStrictEqual(options.spanExporterFactory, jaegerSpanExporterFactory);
assert.deepStrictEqual(
options.endpoint,
'https://ingest.us0.signalfx.com/v2/trace/jaegerthrift'
);
assert.deepStrictEqual(
options.spanExporterFactory,
jaegerSpanExporterFactory
);
});

it('throws when setting the realm with an incompatible SPLUNK_TRACES_EXPORTER', () => {
process.env.SPLUNK_REALM = 'us0';
process.env.SPLUNK_ACCESS_TOKEN = 'abc';
process.env.OTEL_TRACES_EXPORTER = 'otlp-grpc';

assert.throws(
_setDefaultOptions,
/either otlp-http or jaeger-thrift-splunk/
);
});
});
});
Expand Down

0 comments on commit 1965158

Please sign in to comment.