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

add histogram to metrics for logger #83

Merged
merged 1 commit into from
Nov 12, 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
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 },
|};
Loading