Skip to content
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
6 changes: 3 additions & 3 deletions src/goodmetrics/metricsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ export class MetricsFactory {
const res = await block(metrics);
return res;
} finally {
this.emit(metrics);
await this.emit(metrics);
}
}

/**
* Complete and release a Metrics to the configured downstream sink.
* If you don't emit() the metrics it will never show up downstream.
*/
private emit(metrics: _Metrics) {
private async emit(metrics: _Metrics) {
this.finalizeMetrics(metrics);
this.metricsSink.emit(metrics);
await this.metricsSink.emit(metrics);
}

private finalizeMetrics(metrics: _Metrics) {
Expand Down
18 changes: 12 additions & 6 deletions src/goodmetrics/metricsSetups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ interface LightstepNativeLambdaOtlpProps {
*/
lightstepPort?: number;
logError: (message: string, error: unknown) => void;
/**
* Mostly for debugging purposes, logs after successfully sending metrics to the backend.
* Used to tell if the promise fully resolved
*/
doLogSuccess?: boolean;
onSendUnary?: (metrics: Metrics[]) => void;
}

Expand Down Expand Up @@ -181,13 +186,14 @@ export class MetricsSetups {
close(): void {
client.close();
},
emit(metrics: _Metrics): void {
async emit(metrics: _Metrics): Promise<void> {
props?.onSendUnary && props.onSendUnary([metrics]);
client
.sendMetricsBatch([metrics])
.catch(e =>
props.logError('error while sending blocking metrics', e)
);
try {
await client.sendMetricsBatch([metrics]);
props.doLogSuccess && console.log('metrics sent to backend');
} catch (e) {
props.logError('error while sending blocking metrics', e);
}
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/goodmetrics/pipeline/metricsSink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {_Metrics} from '../_Metrics';

export interface MetricsSink {
emit(metrics: _Metrics): void;
emit(metrics: _Metrics): void | Promise<void>;
close(): void;
}