Skip to content

Commit

Permalink
feat: Add log level config option to SDK (#315)
Browse files Browse the repository at this point in the history
<!--
Thank you for contributing to the project! 💜
Please see our [OSS process
document](https://github.com/honeycombio/home/blob/main/honeycomb-oss-lifecycle-and-practices.md#)
to get an idea of how we operate.
-->

## Which problem is this PR solving?

- Closes #279

## Short description of the changes

Users will now have the option to set a log level when initializing the
SDK. This log level controls the verbosity of logs outputted. The
current log levels includes DEBUG, INFO, WARN, and ERROR.

## How to verify that this has the expected result
- Unit tests have been added
- I tested locally by reproducing the issue and seeing if my changes
fixed the issue
  • Loading branch information
jairo-mendoza authored Oct 15, 2024
1 parent c77fb9d commit 06d05ad
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Pass these options to the HoneycombWebSDK:
| traceExporters | optional | SpanExporter[] | | Array of [span exporters](https://opentelemetry.io/docs/languages/js/exporters) | optional |
| webVitalsInstrumentationConfig|optional|WebVitalsInstrumentationConfig| `{ enabled: true }` | See [WebVitalsInstrumentationConfig](####WebVitalsInstrumentationConfig). |
| globalErrorsInstrumentationConfig |optional| GlobalErrorsInstrumentationConfig| `{ enabled: true }` | See [GlobalErrorsInstrumentationConfig](####GlobalErrorsInstrumentationConfig).
| logLevel | optional | DiagLogLevel | DiagLogLevel.DEBUG | Controls the verbosity of logs printed to the console. |

`*` Note: the `apiKey` field is required because this SDK really wants to help you send data directly to Honeycomb.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FAILED_AUTH_FOR_LOCAL_VISUALIZATIONS,
MISSING_FIELDS_FOR_LOCAL_VISUALIZATIONS,
} from './validate-options';
import { DiagLogLevel } from '@opentelemetry/api';

/**
* Builds and returns a {@link SpanExporter} that logs Honeycomb URLs for completed traces
Expand All @@ -22,7 +23,11 @@ export function configureConsoleTraceLinkExporter(
options?: HoneycombOptions,
): SpanExporter {
const apiKey = getTracesApiKey(options);
return new ConsoleTraceLinkExporter(options?.serviceName, apiKey);
return new ConsoleTraceLinkExporter(
options?.serviceName,
apiKey,
options?.logLevel,
);
}

/**
Expand All @@ -32,10 +37,17 @@ export function configureConsoleTraceLinkExporter(
*/
class ConsoleTraceLinkExporter implements SpanExporter {
private _traceUrl = '';
private _logLevel: DiagLogLevel = DiagLogLevel.DEBUG;

constructor(serviceName?: string, apikey?: string, logLevel?: DiagLogLevel) {
if (logLevel) {
this._logLevel = logLevel;
}

constructor(serviceName?: string, apikey?: string) {
if (!serviceName || !apikey) {
console.debug(MISSING_FIELDS_FOR_LOCAL_VISUALIZATIONS);
if (this._logLevel >= DiagLogLevel.DEBUG) {
console.debug(MISSING_FIELDS_FOR_LOCAL_VISUALIZATIONS);
}
return;
}

Expand Down Expand Up @@ -65,7 +77,9 @@ class ConsoleTraceLinkExporter implements SpanExporter {
}
})
.catch(() => {
console.log(FAILED_AUTH_FOR_LOCAL_VISUALIZATIONS);
if (this._logLevel >= DiagLogLevel.INFO) {
console.log(FAILED_AUTH_FOR_LOCAL_VISUALIZATIONS);
}
});
}

Expand All @@ -76,7 +90,7 @@ class ConsoleTraceLinkExporter implements SpanExporter {
if (this._traceUrl) {
spans.forEach((span) => {
// only log root spans (ones without a parent span)
if (!span.parentSpanId) {
if (!span.parentSpanId && this._logLevel >= DiagLogLevel.INFO) {
console.log(
createHoneycombSDKLogMessage(
`Honeycomb link: ${this._traceUrl}=${span.spanContext().traceId}`,
Expand Down
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 @@ -102,7 +102,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 @@ -141,6 +141,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;
}

/* 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
29 changes: 29 additions & 0 deletions packages/honeycomb-opentelemetry-web/test/validate-options.test.ts
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();
});
});
});

0 comments on commit 06d05ad

Please sign in to comment.