Skip to content

Commit

Permalink
move warnings to the same place and add a helper function for creatin…
Browse files Browse the repository at this point in the history
…g log messages
  • Loading branch information
pkanal committed Feb 15, 2024
1 parent 8cf66e7 commit 8479a49
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
9 changes: 7 additions & 2 deletions src/honeycomb-debug.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { HoneycombOptions } from './types';
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
import {
createHoneycombSDKLogMessage,
defaultOptions,
getSampleRate,
getTracesApiKey,
getTracesEndpoint,
} from './util';
import {
MISSING_API_KEY_ERROR,
MISSING_SERVICE_NAME_ERROR,
} from './util';
} from './validate-options';

/**
* Configures the Honeycomb Web SDK to log debug information to the console.
Expand All @@ -21,7 +24,9 @@ export function configureDebug(options?: HoneycombOptions): void {
return;
}
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
diag.debug('🐝 Honeycomb Web SDK Debug Mode Enabled 🐝');
diag.debug(
createHoneycombSDKLogMessage('🐝 Honeycomb Web SDK Debug Mode Enabled 🐝'),
);

// traces endpoint must be computed from provided options
const tracesEndpoint = getTracesEndpoint(options);
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const defaultOptions: HoneycombOptions = {
// localVisualizations: false,
};

export const MISSING_API_KEY_ERROR = `❌ @honeycombio/opentelemetry-web: Missing API Key. Set \`apiKey\` in HoneycombOptions. Telemetry will not be exported.`;
export const MISSING_SERVICE_NAME_ERROR = `❌ @honeycombio/opentelemetry-web: Missing Service Name. Set \`serviceName\` in HoneycombOptions. Defaulting to '${defaultOptions.serviceName}'`;
export const createHoneycombSDKLogMessage = (message: string) =>
`@honeycombio/opentelemetry-web: ${message}`;

/**
* Determines whether the passed in apikey is classic (32 chars) or not.
Expand Down
30 changes: 20 additions & 10 deletions src/validate-options.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { HoneycombOptions } from './types';
import {
createHoneycombSDKLogMessage,
defaultOptions,
isClassic,
MISSING_API_KEY_ERROR,
MISSING_SERVICE_NAME_ERROR,
} from './util';

export const IGNORED_DATASET_ERROR =
'🔕 WARN: Dataset is ignored in favor of service name.';
export const MISSING_DATASET_ERROR =
'❌ WARN: Missing dataset. Specify either HONEYCOMB_DATASET environment variable or dataset in the options parameter.';
export const SKIPPING_OPTIONS_VALIDATION_MSG =
'⏭️ DEBUG: Skipping options validation. To re-enable, set skipOptionsValidation option or HONEYCOMB_SKIP_OPTIONS_VALIDATION to false.';
export const SAMPLER_OVERRIDE_WARNING =
'🔨 WARN: Default deterministic sampler has been overridden. Honeycomb requires a resource attribute called SampleRate to properly show weighted values. Non-deterministic sampleRate could lead to missing spans in Honeycomb. See our docs for more details. https://docs.honeycomb.io/getting-data-in/opentelemetry/node-distro/#sampling-without-the-honeycomb-sdk';
export const MISSING_API_KEY_ERROR = createHoneycombSDKLogMessage(
'❌ Missing API Key. Set `apiKey` in HoneycombOptions. Telemetry will not be exported.',
);
export const MISSING_SERVICE_NAME_ERROR = createHoneycombSDKLogMessage(
`❌ Missing Service Name. Set \`serviceName\` in HoneycombOptions. Defaulting to '${defaultOptions.serviceName}'`,
);
export const IGNORED_DATASET_ERROR = createHoneycombSDKLogMessage(
'🔕 Dataset is ignored in favor of service name.',
);
export const MISSING_DATASET_ERROR = createHoneycombSDKLogMessage(
'❌ Missing dataset. Specify either HONEYCOMB_DATASET environment variable or dataset in the options parameter.',
);
export const SKIPPING_OPTIONS_VALIDATION_MSG = createHoneycombSDKLogMessage(
'⏭️ Skipping options validation. To re-enable, set skipOptionsValidation option or HONEYCOMB_SKIP_OPTIONS_VALIDATION to false.',
);
export const SAMPLER_OVERRIDE_WARNING = createHoneycombSDKLogMessage(
'🔨 Default deterministic sampler has been overridden. Honeycomb requires a resource attribute called SampleRate to properly show weighted values. Non-deterministic sampleRate could lead to missing spans in Honeycomb. See our docs for more details. https://docs.honeycomb.io/getting-data-in/opentelemetry/node-distro/#sampling-without-the-honeycomb-sdk',
);

export const validateOptionsWarnings = (options?: HoneycombOptions) => {
if (options?.skipOptionsValidation) {
Expand Down
9 changes: 4 additions & 5 deletions test/honeycomb-debug.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { HoneycombWebSDK } from '../src/honeycomb-otel-sdk';
import { defaultOptions, TRACES_PATH } from '../src/util';
import {
defaultOptions,
MISSING_API_KEY_ERROR,
MISSING_SERVICE_NAME_ERROR,
TRACES_PATH,
} from '../src/util';
} from '../src/validate-options';

const consoleSpy = jest
.spyOn(console, 'debug')
Expand All @@ -26,7 +25,7 @@ describe('when debug is set to true', () => {
});
expect(consoleSpy).toHaveBeenNthCalledWith(
2,
'🐝 Honeycomb Web SDK Debug Mode Enabled 🐝',
'@honeycombio/opentelemetry-web: 🐝 Honeycomb Web SDK Debug Mode Enabled 🐝',
);
expect(consoleSpy).toHaveBeenNthCalledWith(3, MISSING_API_KEY_ERROR);
expect(consoleSpy).toHaveBeenNthCalledWith(4, MISSING_SERVICE_NAME_ERROR);
Expand All @@ -51,7 +50,7 @@ describe('when debug is set to true', () => {
new HoneycombWebSDK(testConfig);
expect(consoleSpy).toHaveBeenNthCalledWith(
2,
'🐝 Honeycomb Web SDK Debug Mode Enabled 🐝',
'@honeycombio/opentelemetry-web: 🐝 Honeycomb Web SDK Debug Mode Enabled 🐝',
);
expect(consoleSpy).toHaveBeenNthCalledWith(
3,
Expand Down
3 changes: 2 additions & 1 deletion test/validate-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { HoneycombWebSDK } from '../src/honeycomb-otel-sdk';
import {
IGNORED_DATASET_ERROR,
MISSING_API_KEY_ERROR,
MISSING_DATASET_ERROR,
MISSING_SERVICE_NAME_ERROR,
SAMPLER_OVERRIDE_WARNING,
SKIPPING_OPTIONS_VALIDATION_MSG,
} from '../src/validate-options';
import { MISSING_API_KEY_ERROR, MISSING_SERVICE_NAME_ERROR } from '../src/util';
import { AlwaysOnSampler } from '@opentelemetry/sdk-trace-base';
const debugSpy = jest
.spyOn(console, 'debug')
Expand Down

0 comments on commit 8479a49

Please sign in to comment.