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 otel metrics exporter #73

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ Title + Metadata:
* `log.counterD("counter-with-data", 2, {extra: "info"})`
* `log.gaugeD("gauge-with-data", 2, {extra: "info"})`

#### kayvee/logger opentelemetry metrics
```js
var kv = require("../kayvee-js");
var log = new kv.logger("test-logger");

log.setMetricsOutput(kv.Logger.METRICS_OUTPUT_OTEL);
log.counterD("counter-name", 1, {"foo": "bar"});
log.gaugeD("gauge-name", 1.02, {"foo": "bar"});

// required to sync metrics with otel collector
log.shutdown();
```

### Formatters

#### format
Expand Down
109 changes: 91 additions & 18 deletions lib/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var _ = require("underscore");
var kv = require("../kayvee");
var router = require("../router");

const { OTLPMetricExporter } = require("@opentelemetry/exporter-otlp-grpc");
const { MeterProvider } = require("@opentelemetry/sdk-metrics-base");
const { Resource } = require("@opentelemetry/resources");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");

var LEVELS = {
Trace: "trace",
Debug: "debug",
Expand All @@ -20,6 +25,16 @@ var LOG_LEVEL_ENUM = {
critical: 5,
};

const METRICS_OUTPUT = {
LOG: "log",
OTEL: "otel",
};

const METRICS_OUTPUT_ENUM = {
log: 0,
otel: 1,
};

const assign = Object.assign || _.assign; // Use the faster Object.assign if possible

let globalRouter;
Expand All @@ -40,6 +55,8 @@ class Logger {
globals = null;
logWriter = null;
logRouter = null;
metricsProvider = null;
metrics = null;

constructor(
source,
Expand All @@ -52,13 +69,18 @@ class Logger {
this.globals = {};
this.globals.source = source;
this.logWriter = output;
this.metricsProvider = null;
this.metrics = {};

if (process.env._TEAM_OWNER) {
this.globals.team = process.env._TEAM_OWNER;
}
if (process.env._DEPLOY_ENV) {
this.globals.deploy_env = process.env._DEPLOY_ENV;
}
if (process.env._BUILD_ID) {
this.globals.build_id = process.env._BUILD_ID;
}
if (process.env._EXECUTION_NAME) {
this.globals.wf_id = process.env._EXECUTION_NAME;
}
Expand Down Expand Up @@ -118,6 +140,30 @@ class Logger {
return this.logWriter;
}

setMetricsOutput(output) {
if (output in METRICS_OUTPUT_ENUM && output === METRICS_OUTPUT.OTEL) {
var metricExporter = new OTLPMetricExporter({
concurrencyLimit: 1,
});

this.metricsProvider = new MeterProvider({
exporter: metricExporter,
interval: 1000,
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: this.globals.source,
[SemanticResourceAttributes.SERVICE_VERSION]: this.globals.build_id,
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: this.globals.deploy_env,
}),
}).getMeter(this.globals.source);
} else {
this.errorD("set-metrics-ouput", {
err: "invalid metrics output specified, falling back to log output",
});
this.metricsProvider = null;
}
return this.metricsProvider;
}

trace(title) {
this.traceD(title, {});
}
Expand Down Expand Up @@ -211,27 +257,52 @@ class Logger {
}

counterD(title, value, data) {
this._logWithLevel(
LEVELS.Info,
{
title,
value,
type: "counter",
},
data,
);
if (this.metricsProvider == null) {
this._logWithLevel(
LEVELS.Info,
{
title,
value,
type: "counter",
},
data,
);
} else {
if (this.metrics[title] === undefined) {
this.metrics[title] = this.metricsProvider.createUpDownCounter(title);
}
this.metrics[title].add(value, data);
}
}

gaugeD(title, value, data) {
this._logWithLevel(
LEVELS.Info,
{
title,
value,
type: "gauge",
},
data,
);
if (this.metricsProvider == null) {
this._logWithLevel(
LEVELS.Info,
{
title,
value,
type: "gauge",
},
data,
);
} else {
// use a histogram post v0.26.0 of metrics sdk
if (this.metrics[title] === undefined) {
this.metrics[title] = this.metricsProvider.createValueRecorder(title);
}
this.metrics[title].record(value, data);
}
}

shutdown() {
if (this.metricsProvider != null) {
return this.metricsProvider.shutdown().catch((err) => {
console.error(err);
return err;
});
}
return null;
}

_logWithLevel(logLvl, metadata, userdata) {
Expand All @@ -251,6 +322,8 @@ class Logger {
module.exports = Logger;
module.exports.setGlobalRouting = setGlobalRouting;
module.exports.getGlobalRouter = getGlobalRouter;
module.exports.METRICS_OUTPUT_OTEL = METRICS_OUTPUT.OTEL;
module.exports.METRICS_OUTPUT_LOG = METRICS_OUTPUT.LOG;
module.exports.mockRouting = (cb) => {
const _logWithLevel: any = Logger.prototype._logWithLevel;

Expand Down
Loading