Skip to content

Commit c51d078

Browse files
authored
feat: add debug logger, fix raw otel totaltime measurement (#34)
1 parent 5728a49 commit c51d078

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/goodmetrics/metricsFactory.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ export enum TotaltimeType {
2626
None = 'none',
2727
}
2828

29+
export type LogLevel = 'none' | 'debug' | 'info' | 'error';
30+
2931
interface Props {
3032
metricsSink: MetricsSink;
3133
totalTimeType: TotaltimeType;
34+
logLevel?: LogLevel;
3235
}
3336

3437
interface RecordOptions {
@@ -42,12 +45,57 @@ interface RecordWithBehaviorOptions {
4245
behavior: MetricsBehavior;
4346
}
4447

48+
const NONE_LEVEL = 0;
49+
const DEBUG_LEVEL = 1;
50+
const INFO_LEVEL = 2;
51+
const ERROR_LEVEL = 3;
52+
53+
class Logger {
54+
private readonly level: number;
55+
constructor(level: LogLevel) {
56+
switch (level) {
57+
case 'none':
58+
this.level = NONE_LEVEL;
59+
break;
60+
case 'debug':
61+
this.level = DEBUG_LEVEL;
62+
break;
63+
case 'info':
64+
this.level = INFO_LEVEL;
65+
break;
66+
case 'error':
67+
this.level = ERROR_LEVEL;
68+
break;
69+
}
70+
}
71+
72+
debug(message: string): void {
73+
if (this.level >= DEBUG_LEVEL) {
74+
console.debug(message);
75+
}
76+
}
77+
78+
info(message: string): void {
79+
if (this.level >= INFO_LEVEL) {
80+
console.info(message);
81+
}
82+
}
83+
84+
error(message: string): void {
85+
if (this.level >= ERROR_LEVEL) {
86+
console.error(message);
87+
}
88+
}
89+
}
90+
4591
export class MetricsFactory {
4692
protected readonly metricsSink: MetricsSink;
4793
private readonly totalTimeType: TotaltimeType;
94+
private readonly logger: Logger;
4895
constructor(props: Props) {
4996
this.metricsSink = props.metricsSink;
5097
this.totalTimeType = props.totalTimeType;
98+
this.logger = new Logger(props.logLevel ?? 'none');
5199
}
52100

53101
/**
@@ -109,26 +157,33 @@ export class MetricsFactory {
109157
*/
110158
private async emit(metrics: _Metrics) {
111159
this.finalizeMetrics(metrics);
160+
this.logger.debug('metrics finalized');
161+
console.log('metrics', metrics);
112162
await this.metricsSink.emit(metrics);
113163
}
114164

115165
private finalizeMetrics(metrics: _Metrics) {
166+
this.logger.debug('finalizing metrics');
116167
if (metrics.timestampMillis < 1) {
117168
metrics.timestampMillis = Date.now();
118169
}
119170
if (metrics.metricsBehavior === MetricsBehavior.NO_TOTALTIME) {
171+
this.logger.debug('no total time being recorded');
120172
return;
121173
}
122174

123175
const duration = metrics.getDurationMillis();
124176
switch (this.totalTimeType) {
125177
case TotaltimeType.DistributionMilliseconds:
178+
this.logger.debug(`distribution milliseconds, duration: ${duration}`);
126179
metrics.distribution('totaltime', duration);
127180
break;
128181
case TotaltimeType.MeasurementMilliseconds:
182+
this.logger.debug(`measurement milliseconds, duration: ${duration}`);
129183
metrics.measure('totaltime', duration);
130184
break;
131185
case TotaltimeType.None:
186+
this.logger.debug(`totaltime.none, duration: ${duration}`);
132187
break;
133188
}
134189
}

src/goodmetrics/metricsSetups.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {MetricsFactory, TotaltimeType} from './metricsFactory';
1+
import {LogLevel, MetricsFactory, TotaltimeType} from './metricsFactory';
22
import {_Metrics, Dimension, Metrics} from './_Metrics';
33
import {
44
OpenTelemetryClient,
@@ -47,6 +47,7 @@ interface LightstepNativeLambdaOtlpProps {
4747
*/
4848
doLogSuccess?: boolean;
4949
onSendUnary?: (metrics: Metrics[]) => void;
50+
logLevel?: LogLevel;
5051
}
5152

5253
interface RawNativeLambdaOtlpForLambdaProps {
@@ -82,6 +83,7 @@ interface RawNativeLambdaOtlpForLambdaProps {
8283
*/
8384
doLogSuccess?: boolean;
8485
onSendUnary?: (metrics: Metrics[]) => void;
86+
logLevel?: LogLevel;
8587
}
8688

8789
interface ConfigureBatchedUnaryLightstepSinkProps {
@@ -125,6 +127,7 @@ interface LightstepNativeOtlpProps {
125127
preaggregatedBatchMaxAgeSeconds?: number;
126128
onSendUnary?: (metrics: Metrics[]) => void;
127129
onSendPreaggregated?: (aggregatedBatch: AggregatedBatch[]) => void;
130+
logLevel?: LogLevel;
128131
}
129132

130133
interface GoodmetricsSetupProps {
@@ -186,11 +189,13 @@ export class MetricsSetups {
186189
const unaryMetricsFactory = new MetricsFactory({
187190
metricsSink: unarySink,
188191
totalTimeType: TotaltimeType.DistributionMilliseconds,
192+
logLevel: props.logLevel,
189193
});
190194

191195
const preaggregatedMetricsFactory = new MetricsFactory({
192196
metricsSink: preaggregatedSink,
193197
totalTimeType: TotaltimeType.DistributionMilliseconds,
198+
logLevel: props.logLevel,
194199
});
195200

196201
return {
@@ -237,6 +242,7 @@ export class MetricsSetups {
237242
return new MetricsFactory({
238243
metricsSink: unarySink,
239244
totalTimeType: TotaltimeType.DistributionMilliseconds,
245+
logLevel: props.logLevel,
240246
});
241247
}
242248

@@ -275,7 +281,8 @@ export class MetricsSetups {
275281

276282
return new MetricsFactory({
277283
metricsSink: unarySink,
278-
totalTimeType: TotaltimeType.DistributionMilliseconds,
284+
totalTimeType: TotaltimeType.MeasurementMilliseconds,
285+
logLevel: props.logLevel,
279286
});
280287
}
281288

0 commit comments

Comments
 (0)