Skip to content

Commit

Permalink
feat: add 'metric' logger method to accept otel metrics (#65)
Browse files Browse the repository at this point in the history
* feat: add 'metric' logger method to accept otel metrics

* feat: server support to recognize `metrics` from the frontend
  • Loading branch information
ukalbhor authored Feb 21, 2023
1 parent becce9f commit 3983c31
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
31 changes: 29 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type ExpressResponse = express$Response; // eslint-disable-line no-undef

type Payload = { [string]: string };

export type Metric = {|
name: string,
dimensions: Payload,
|};

type Logger = {|
log: (
req: ExpressRequest,
Expand All @@ -19,6 +24,7 @@ type Logger = {|
meta?: Payload
) => void,
track: (req: ExpressRequest, payload: Payload, meta?: Payload) => void,
metric: (req: ExpressRequest, payload: Metric, meta?: Payload) => void,
meta: (req: ExpressRequest, meta: Payload) => void,
|};

Expand Down Expand Up @@ -66,6 +72,18 @@ export const defaultLogger: Logger = {
);
},

metric(req, metric) {
console.log(
`[metric].${metric.name}\n`,
Object.keys(metric.dimensions)
.map((key) => {
return `\t${key}: ${metric.dimensions[key]}`;
})
.join("\n"),
"\n"
);
},

meta(req, meta) {
console.log(
"[meta]\n",
Expand All @@ -85,11 +103,13 @@ export function log(
logs: {|
events: $ReadOnlyArray<Event>,
tracking?: $ReadOnlyArray<Tracking>,
metrics?: $ReadOnlyArray<Metric>,
meta?: Meta,
|}
) {
const events = logs.events || [];
const tracking = logs.tracking || [];
const metrics = logs.metrics || [];
const meta = logs.meta || {};

if (logger.meta) {
Expand All @@ -115,6 +135,12 @@ export function log(
logger.track(req, track, meta);
});
}

if (logger.metric) {
metrics.forEach((metric) => {
logger.metric(req, metric);
});
}
}

type Query = {
Expand All @@ -126,6 +152,7 @@ type Query = {
type Body = {|
events: $ReadOnlyArray<Event>,
tracking?: $ReadOnlyArray<Tracking>,
metrics?: $ReadOnlyArray<Metric>,
meta: Meta,
|};

Expand All @@ -140,8 +167,8 @@ export function handleRequest(req: ExpressRequest, logger: Logger) {
const body: Body = req.body || {};

if (method.toLowerCase() === "post") {
const { events, tracking, meta } = body;
log(req, logger, { events, tracking, meta });
const { events, tracking, metrics, meta } = body;
log(req, logger, { events, tracking, metrics, meta });
} else {
const { event, level = LOG_LEVEL.INFO, ...payload } = query;

Expand Down
26 changes: 24 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { LOG_LEVEL, PROTOCOL } from "./constants";
import { extendIfDefined } from "./util";
import { type Transport, getHTTPTransport } from "./http";
import type { Payload } from "./types";
import type { Metric, Payload } from "./types";

type LoggerOptions = {|
url?: string,
Expand All @@ -34,6 +34,7 @@ type LoggerOptions = {|
type ClientPayload = Payload;
type Log = (name: string, payload?: ClientPayload) => LoggerType; // eslint-disable-line no-use-before-define
type Track = (payload: ClientPayload) => LoggerType; // eslint-disable-line no-use-before-define
type LogMetric = (payload: Metric) => LoggerType; // eslint-disable-line no-use-before-define

type Builder = (Payload) => ClientPayload;
type AddBuilder = (Builder) => LoggerType; // eslint-disable-line no-use-before-define
Expand All @@ -45,6 +46,7 @@ export type LoggerType = {|
error: Log,

track: Track,
metric: LogMetric,

flush: () => ZalgoPromise<void>,
immediateFlush: () => ZalgoPromise<void>,
Expand Down Expand Up @@ -73,6 +75,7 @@ export function Logger({
payload: Payload,
|}> = [];
let tracking: Array<Payload> = [];
let metrics: Array<Metric> = [];

const payloadBuilders: Array<Builder> = [];
const metaBuilders: Array<Builder> = [];
Expand Down Expand Up @@ -119,7 +122,7 @@ export function Logger({
return;
}

if (!events.length && !tracking.length) {
if (!events.length && !tracking.length && !metrics.length) {
return;
}

Expand All @@ -144,6 +147,7 @@ export function Logger({
events,
meta,
tracking,
metrics,
},
enableSendBeacon,
}).catch(noop);
Expand Down Expand Up @@ -171,6 +175,7 @@ export function Logger({

events = [];
tracking = [];
metrics = [];

return ZalgoPromise.resolve(res).then(noop);
});
Expand Down Expand Up @@ -276,6 +281,22 @@ export function Logger({
return logger; // eslint-disable-line no-use-before-define
}

function metric(metricPayload: Metric): LoggerType {
if (!isBrowser()) {
return logger; // eslint-disable-line no-use-before-define
}

print(
LOG_LEVEL.DEBUG,
`metric.${metricPayload.name}`,
metricPayload.dimensions
);

metrics.push(metricPayload);

return logger; // eslint-disable-line no-use-before-define
}

function setTransport(newTransport: Transport): LoggerType {
transport = newTransport;
return logger; // eslint-disable-line no-use-before-define
Expand Down Expand Up @@ -337,6 +358,7 @@ export function Logger({
warn,
error,
track,
metric,
flush,
immediateFlush,
addPayloadBuilder,
Expand Down
5 changes: 5 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* @flow */

export type Payload = { [string]: string | boolean | null | void };

export type Metric = {|
name: string,
dimensions: Payload,
|};

0 comments on commit 3983c31

Please sign in to comment.