Skip to content

Commit

Permalink
feat(metrics): add histogram to metrics for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrunson committed Nov 12, 2024
1 parent b66703f commit 10b9e92
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
Payload,
MetricPayloadCounter,
MetricPayloadGauge,
MetricPayloadHistogram,
} from "./types";

export type LoggerOptions = {|
Expand Down Expand Up @@ -58,6 +59,7 @@ export type LoggerType = {|
metric: LogMetric,
metricCounter: (payload: MetricPayloadCounter) => LoggerType,
metricGauge: (payload: MetricPayloadGauge) => LoggerType,
metricHistogram: (payload: MetricPayloadHistogram) => LoggerType,

flush: () => ZalgoPromise<void>,
immediateFlush: () => ZalgoPromise<void>,
Expand Down Expand Up @@ -331,6 +333,16 @@ export function Logger({
});
}

function metricHistogram(metricPayload: MetricPayloadHistogram): LoggerType {
return metric({
metricNamespace: metricPayload.namespace,
metricEventName: metricPayload.event,
metricValue: metricPayload.value,
metricType: "histogram",
dimensions: metricPayload.dimensions,
});
}

function setTransport(newTransport: Transport): LoggerType {
transport = newTransport;
return logger; // eslint-disable-line no-use-before-define
Expand Down Expand Up @@ -391,6 +403,7 @@ export function Logger({
metric,
metricCounter,
metricGauge,
metricHistogram,
flush,
immediateFlush,
addPayloadBuilder,
Expand Down
43 changes: 43 additions & 0 deletions src/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,49 @@ describe("metricGauge", () => {
});
});

describe("metricHistogram", () => {
test("adds metrics of histogram type", () => {
const testLogger = initLogger();

testLogger.metricHistogram({
namespace: "namespace",
event: "load",
value: 100,
dimensions: {
one: "1",
},
});

expect(getLoggerBuffer(testLogger).metrics[0]).toEqual({
metricNamespace: "namespace",
metricEventName: "load",
metricValue: 100,
metricType: "histogram",
dimensions: {
one: "1",
},
});
});

test("uses metric namespace prefix", () => {
const testLogger = initLogger({
metricNamespacePrefix: "prefix",
});

testLogger.metricHistogram({
namespace: "namespace",
event: "load",
value: 100,
});

expect(getLoggerBuffer(testLogger).metrics[0]).toEqual(
expect.objectContaining({
metricNamespace: "prefix.namespace",
})
);
});
});

describe("addMetricDimensionBuilder", () => {
test("adds dimensions from builder", () => {
const testLogger = initLogger();
Expand Down
14 changes: 13 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type MetricPayload = {|
metricNamespace: string, // the name of the metric that's used for charting / finding in signalFx
metricEventName: string, // assigned to event_name dimension in signalFx
metricValue?: number, // in most cases this will be 1 if we want to count 1 instance of an event happening.
metricType?: "counter" | "gauge", // We support these types of metrics. Defaults to counter.
metricType?: "counter" | "gauge" | "histogram", // We support these types of metrics. Defaults to counter.
/**
* For proper usage & best practices guidance around dimensions please read: -------------------->
* - https://engineering.paypalcorp.com/confluence/pages/viewpage.action?pageId=981633893
Expand Down Expand Up @@ -38,3 +38,15 @@ export type MetricPayloadGauge = {|
*/
dimensions?: { [string]: mixed },
|};

export type MetricPayloadHistogram = {|
namespace: string, // the name of the metric that's used for charting / finding in signalFx
event: string, // assigned to event_name dimension in signalFx
value: number, // in most cases this will be 1 if we want to count 1 instance of an event happening.
/**
* For proper usage & best practices guidance around dimensions please read: -------------------->
* - https://engineering.paypalcorp.com/confluence/pages/viewpage.action?pageId=981633893
* - https://engineering.paypalcorp.com/confluence/display/Checkout/Checkout+Observability+Overview
*/
dimensions?: { [string]: mixed },
|};

0 comments on commit 10b9e92

Please sign in to comment.