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 debug logger, fix raw otel totaltime measurement #34

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/goodmetrics/metricsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export enum TotaltimeType {
None = 'none',
}

export type LogLevel = 'none' | 'debug' | 'info' | 'error';

interface Props {
metricsSink: MetricsSink;
totalTimeType: TotaltimeType;
logLevel?: LogLevel;
}

interface RecordOptions {
Expand All @@ -42,12 +45,57 @@ interface RecordWithBehaviorOptions {
behavior: MetricsBehavior;
}

const NONE_LEVEL = 0;
const DEBUG_LEVEL = 1;
const INFO_LEVEL = 2;
const ERROR_LEVEL = 3;

class Logger {
private readonly level: number;
constructor(level: LogLevel) {
switch (level) {
case 'none':
this.level = NONE_LEVEL;
break;
case 'debug':
this.level = DEBUG_LEVEL;
break;
case 'info':
this.level = INFO_LEVEL;
break;
case 'error':
this.level = ERROR_LEVEL;
break;
}
}

debug(message: string): void {
if (this.level >= DEBUG_LEVEL) {
console.debug(message);
}
}

info(message: string): void {
if (this.level >= INFO_LEVEL) {
console.info(message);
}
}

error(message: string): void {
if (this.level >= ERROR_LEVEL) {
console.error(message);
}
}
}

export class MetricsFactory {
protected readonly metricsSink: MetricsSink;
private readonly totalTimeType: TotaltimeType;
private readonly logger: Logger;
constructor(props: Props) {
this.metricsSink = props.metricsSink;
this.totalTimeType = props.totalTimeType;
this.logger = new Logger(props.logLevel ?? 'none');
}

/**
Expand Down Expand Up @@ -109,26 +157,33 @@ export class MetricsFactory {
*/
private async emit(metrics: _Metrics) {
this.finalizeMetrics(metrics);
this.logger.debug('metrics finalized');
console.log('metrics', metrics);
await this.metricsSink.emit(metrics);
}

private finalizeMetrics(metrics: _Metrics) {
this.logger.debug('finalizing metrics');
if (metrics.timestampMillis < 1) {
metrics.timestampMillis = Date.now();
}
if (metrics.metricsBehavior === MetricsBehavior.NO_TOTALTIME) {
this.logger.debug('no total time being recorded');
return;
}

const duration = metrics.getDurationMillis();
switch (this.totalTimeType) {
case TotaltimeType.DistributionMilliseconds:
this.logger.debug(`distribution milliseconds, duration: ${duration}`);
metrics.distribution('totaltime', duration);
break;
case TotaltimeType.MeasurementMilliseconds:
this.logger.debug(`measurement milliseconds, duration: ${duration}`);
metrics.measure('totaltime', duration);
break;
case TotaltimeType.None:
this.logger.debug(`totaltime.none, duration: ${duration}`);
break;
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/goodmetrics/metricsSetups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MetricsFactory, TotaltimeType} from './metricsFactory';
import {LogLevel, MetricsFactory, TotaltimeType} from './metricsFactory';
import {_Metrics, Dimension, Metrics} from './_Metrics';
import {
OpenTelemetryClient,
Expand Down Expand Up @@ -47,6 +47,7 @@ interface LightstepNativeLambdaOtlpProps {
*/
doLogSuccess?: boolean;
onSendUnary?: (metrics: Metrics[]) => void;
logLevel?: LogLevel;
}

interface RawNativeLambdaOtlpForLambdaProps {
Expand Down Expand Up @@ -82,6 +83,7 @@ interface RawNativeLambdaOtlpForLambdaProps {
*/
doLogSuccess?: boolean;
onSendUnary?: (metrics: Metrics[]) => void;
logLevel?: LogLevel;
}

interface ConfigureBatchedUnaryLightstepSinkProps {
Expand Down Expand Up @@ -125,6 +127,7 @@ interface LightstepNativeOtlpProps {
preaggregatedBatchMaxAgeSeconds?: number;
onSendUnary?: (metrics: Metrics[]) => void;
onSendPreaggregated?: (aggregatedBatch: AggregatedBatch[]) => void;
logLevel?: LogLevel;
}

interface GoodmetricsSetupProps {
Expand Down Expand Up @@ -186,11 +189,13 @@ export class MetricsSetups {
const unaryMetricsFactory = new MetricsFactory({
metricsSink: unarySink,
totalTimeType: TotaltimeType.DistributionMilliseconds,
logLevel: props.logLevel,
});

const preaggregatedMetricsFactory = new MetricsFactory({
metricsSink: preaggregatedSink,
totalTimeType: TotaltimeType.DistributionMilliseconds,
logLevel: props.logLevel,
});

return {
Expand Down Expand Up @@ -237,6 +242,7 @@ export class MetricsSetups {
return new MetricsFactory({
metricsSink: unarySink,
totalTimeType: TotaltimeType.DistributionMilliseconds,
logLevel: props.logLevel,
});
}

Expand Down Expand Up @@ -275,7 +281,8 @@ export class MetricsSetups {

return new MetricsFactory({
metricsSink: unarySink,
totalTimeType: TotaltimeType.DistributionMilliseconds,
totalTimeType: TotaltimeType.MeasurementMilliseconds,
logLevel: props.logLevel,
});
}

Expand Down
Loading