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 prefix option for metric namespace #81

Merged
merged 1 commit into from
Feb 13, 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
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

module.exports = {
extends: "@krakenjs/eslint-config-grumbler/eslintrc-browser",
globals: {
$Shape: true,
},
};
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ logger.metricCounter({
})
```

#### Using a namespace prefix

```
const logger = new Logger({...options, metricNamespacePrefix: "company.team.app"})

logger.metricCounter({
namespace: "product.feature",
event: "button_click",
})

// creates metric with namespace of
// company.team.app.product.feature
```

### `$logger.metricGauge(<event>, <payload>);`

Queues a gauge metric, helper wrapping `logger.metric`
Expand All @@ -71,6 +85,21 @@ logger.metricGauge({
})
```

#### Using a namespace prefix

```
const logger = new Logger({...options, metricNamespacePrefix: "company.team.app"})

logger.metricGauge({
namespace: "product.feature",
event: "request_latency",
value: 100
})

// creates metric with namespace of
// company.team.app.product.feature
```

### Deprecated - `$logger.metric(<event>, <payload>);`

Queues a metric. We suggest using the `metricCount` or `metricGauge` interface for better type safety and clearer intention in your code.
Expand Down
8 changes: 7 additions & 1 deletion src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import type {
MetricPayloadGauge,
} from "./types";

type LoggerOptions = {|
export type LoggerOptions = {|
url?: string,
prefix?: string,
metricNamespacePrefix?: string,
logLevel?: $Values<typeof LOG_LEVEL>,
transport?: Transport,
flushInterval?: number,
Expand Down Expand Up @@ -80,6 +81,7 @@ export type LoggerType = {|
export function Logger({
url,
prefix,
metricNamespacePrefix,
logLevel = DEFAULT_LOG_LEVEL,
transport = getHTTPTransport(),
flushInterval = FLUSH_INTERVAL,
Expand Down Expand Up @@ -283,6 +285,10 @@ export function Logger({
return logger; // eslint-disable-line no-use-before-define
}

if (metricNamespacePrefix) {
metricPayload.metricNamespace = `${metricNamespacePrefix}.${metricPayload.metricNamespace}`;
}

if (metricDimensionBuilders.length > 0 && !metricPayload.dimensions) {
metricPayload.dimensions = {};
}
Expand Down
53 changes: 46 additions & 7 deletions src/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { beforeEach, describe, expect, test, vi } from "vitest";

import { Logger } from ".";
import { Logger, type LoggerOptions } from ".";

const XMLHttpRequestMock = vi.fn(() => ({
open: vi.fn(),
Expand All @@ -16,9 +16,13 @@ vi.stubGlobal("XMLHttpRequest", XMLHttpRequestMock);
let logger;
let logBuf;

const initLogger = ({ url = "/test/api/log" } = {}) =>
const initLogger = ({
url = "/test/api/log",
metricNamespacePrefix,
}: $Shape<LoggerOptions> = {}) =>
Logger({
url,
metricNamespacePrefix,
});
const getLoggerBuffer = (testLogger) => testLogger.__buffer__;

Expand Down Expand Up @@ -266,7 +270,7 @@ describe("beaver logger provides flushing methods that send events to the server
});

describe("metricCounter", () => {
test("should add metrics of counter type", () => {
test("adds metrics of counter type", () => {
const testLogger = initLogger();

testLogger.metricCounter({
Expand Down Expand Up @@ -306,10 +310,27 @@ describe("metricCounter", () => {
},
});
});

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

testLogger.metricCounter({
namespace: "namespace",
event: "no_value",
});

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

describe("metricGauge", () => {
test("should add metrics of gauge type", () => {
test("adds metrics of gauge type", () => {
const testLogger = initLogger();

testLogger.metricGauge({
Expand All @@ -331,10 +352,28 @@ describe("metricGauge", () => {
},
});
});

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

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

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

describe("addMetricDimensionBuilder", () => {
test("should add dimensions from builder", () => {
test("adds dimensions from builder", () => {
const testLogger = initLogger();

testLogger.addMetricDimensionBuilder(() => ({
Expand All @@ -356,7 +395,7 @@ describe("addMetricDimensionBuilder", () => {
);
});

test("should overwrite existing dimensions", () => {
test("overwrites existing dimensions", () => {
const testLogger = initLogger();

testLogger.addMetricDimensionBuilder(() => ({
Expand Down Expand Up @@ -385,7 +424,7 @@ describe("addMetricDimensionBuilder", () => {
);
});

test("should merge dimensions", () => {
test("merges dimensions", () => {
const testLogger = initLogger();

testLogger.addMetricDimensionBuilder(() => ({
Expand Down
Loading