Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add log level config option to SDK #315

Merged
merged 14 commits into from
Oct 15, 2024
Merged
10 changes: 8 additions & 2 deletions packages/honeycomb-opentelemetry-web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import type { ContextManager } from '@opentelemetry/api';
import type { ContextManager, DiagLogLevel } from '@opentelemetry/api';
import { TextMapPropagator } from '@opentelemetry/api';
import { Instrumentation } from '@opentelemetry/instrumentation';
import {
Expand Down Expand Up @@ -95,7 +95,7 @@ export interface HoneycombOptions extends Partial<WebSDKConfiguration> {
*/
sampleRate?: number;

/** The debug flag enables additional logging that us useful when debugging your application. Do not use in production.
/** The debug flag enables additional logging that is useful when debugging your application. Do not use in production.
* Defaults to 'false'.
*/
debug?: boolean;
Expand Down Expand Up @@ -134,6 +134,12 @@ export interface HoneycombOptions extends Partial<WebSDKConfiguration> {
/** Config options for web vitals instrumentation. Enabled by default. */
webVitalsInstrumentationConfig?: WebVitalsInstrumentationConfig;
globalErrorsInstrumentationConfig?: GlobalErrorsInstrumentationConfig;

/**
* Controls the verbosity of logs. Utilizes OpenTelemetry's `DiagLogLevel` enum. Defaults to 'DEBUG'.
* Current options include 'NONE', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'VERBOSE', and 'ALL'.
*/
logLevel?: DiagLogLevel;
jairo-mendoza marked this conversation as resolved.
Show resolved Hide resolved
}

/* Configure which fields to include in the `entry_page` resource attributes. By default,
Expand Down
29 changes: 23 additions & 6 deletions packages/honeycomb-opentelemetry-web/src/validate-options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DiagLogLevel } from '@opentelemetry/api';
import { HoneycombOptions } from './types';
import {
createHoneycombSDKLogMessage,
Expand Down Expand Up @@ -33,32 +34,48 @@ export const FAILED_AUTH_FOR_LOCAL_VISUALIZATIONS =
);

export const validateOptionsWarnings = (options?: HoneycombOptions) => {
const logLevel: DiagLogLevel = options?.logLevel
? options.logLevel
: DiagLogLevel.DEBUG;

if (options?.skipOptionsValidation) {
console.debug(SKIPPING_OPTIONS_VALIDATION_MSG);
if (logLevel === DiagLogLevel.DEBUG) {
console.debug(SKIPPING_OPTIONS_VALIDATION_MSG);
}
return;
}
// warn if api key is missing
if (!options?.apiKey) {
if (!options?.apiKey && logLevel >= DiagLogLevel.WARN) {
console.warn(MISSING_API_KEY_ERROR);
}

// warn if service name is missing
if (!options?.serviceName) {
if (!options?.serviceName && logLevel >= DiagLogLevel.WARN) {
console.warn(MISSING_SERVICE_NAME_ERROR);
}

// warn if dataset is set while using an environment-aware key
if (options?.apiKey && !isClassic(options?.apiKey) && options?.dataset) {
if (
options?.apiKey &&
!isClassic(options?.apiKey) &&
options?.dataset &&
logLevel >= DiagLogLevel.WARN
) {
console.warn(IGNORED_DATASET_ERROR);
}

// warn if dataset is missing if using classic key
if (options?.apiKey && isClassic(options?.apiKey) && !options?.dataset) {
if (
options?.apiKey &&
isClassic(options?.apiKey) &&
!options?.dataset &&
logLevel >= DiagLogLevel.WARN
) {
console.warn(MISSING_DATASET_ERROR);
}

// warn if custom sampler provided
if (options?.sampler) {
if (options?.sampler && logLevel === DiagLogLevel.DEBUG) {
console.debug(SAMPLER_OVERRIDE_WARNING);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DiagLogLevel } from '@opentelemetry/api';
import { HoneycombWebSDK } from '../src/honeycomb-otel-sdk';
import {
IGNORED_DATASET_ERROR,
Expand Down Expand Up @@ -42,7 +43,27 @@ describe('console warnings', () => {
SKIPPING_OPTIONS_VALIDATION_MSG,
);
});

it('should not show any warnings or debug logs if log level is lower than DEBUG level', () => {
new HoneycombWebSDK({
skipOptionsValidation: true,
logLevel: DiagLogLevel.INFO,
});
expect(debugSpy).not.toHaveBeenCalled();
});

it("should show debug logs if log level is 'DEBUG'", () => {
new HoneycombWebSDK({
skipOptionsValidation: true,
logLevel: DiagLogLevel.DEBUG,
});
expect(debugSpy).toHaveBeenNthCalledWith(
1,
SKIPPING_OPTIONS_VALIDATION_MSG,
);
});
});

describe('when skipOptionsValidation is false', () => {
it('should show the API key missing warning', () => {
new HoneycombWebSDK({
Expand Down Expand Up @@ -83,5 +104,13 @@ describe('console warnings', () => {

expect(debugSpy).toHaveBeenLastCalledWith(SAMPLER_OVERRIDE_WARNING);
});

it("should not show any warnings if log level is lower than 'WARN'", () => {
new HoneycombWebSDK({
logLevel: DiagLogLevel.ERROR,
});

expect(warningSpy).not.toHaveBeenCalled();
});
});
});
Loading