@cran/gql.koa
Version:
Cran/GraphQL Koa Server
112 lines (111 loc) • 4.19 kB
JavaScript
;
/* eslint-disable max-lines */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricSpanExporter = void 0;
const api_1 = require("@opentelemetry/api");
const tty_1 = require("tty");
const lodash_1 = require("lodash");
const prom_client_1 = require("prom-client");
const core_1 = require("@opentelemetry/core");
class MetricSpanExporter {
metrics = {};
config;
constructor(config) {
(0, prom_client_1.collectDefaultMetrics)({ prefix: config.prefix, });
this.config = {
prefix: "",
trace: MetricSpanExporter.defaultTracer,
...config,
relabel: {
...MetricSpanExporter.defaultRelabel,
...config.relabel,
},
};
}
export(spans, done) {
return void this.send(spans, done);
}
async shutdown() {
this.send([]);
}
format(span) {
return {
ok: span.status.code !== api_1.SpanStatusCode.ERROR,
name: span.name,
timestamp: (0, core_1.hrTimeToMilliseconds)(span.startTime),
duration: (0, core_1.hrTimeToMilliseconds)(span.duration),
attributes: span.attributes,
};
}
send(spans, done) {
for (const span of spans) {
const result = this.format(span);
this.config.trace(result);
this.metric(result.name, result.duration, span);
}
if (done) {
return void done({ code: core_1.ExportResultCode.SUCCESS, });
}
}
metric(name, value, span) {
const config = { library: span.instrumentationLibrary.name, name, };
const relabel = this.config.relabel[config.name] ||
this.config.relabel[config.library];
if (!relabel) {
return;
}
const labels = relabel(span.attributes, config);
if (!labels) {
return;
}
const metric = this.metrics[name] || (this.metrics[name] = this.newMetric(config, Object.keys(labels)));
metric.counter.inc(labels);
metric.histogram.observe(labels, value);
metric.summary.observe(labels, value);
}
newMetric({ library, name, }, labelNames) {
const metricName = this.config.prefix + (0, lodash_1.snakeCase)(name);
const help = `${name} (${library})`;
return {
counter: new prom_client_1.Counter({ name: `${metricName}_counter`, help, labelNames, }),
summary: new prom_client_1.Summary({ name: `${metricName}_summary`, help, labelNames, }),
histogram: new prom_client_1.Histogram({
name: `${metricName}_histogram`,
help,
labelNames,
buckets: [0.1, 0.25, 0.5, 1, 2.5, 5, 7,],
}),
};
}
}
exports.MetricSpanExporter = MetricSpanExporter;
(function (MetricSpanExporter) {
MetricSpanExporter.ttyPrint = (0, tty_1.isatty)(process.stdout.fd)
? function identity(value) { return value; }
: JSON.stringify.bind(JSON);
function defaultTracer(value) {
// eslint-disable-next-line no-console
console.log(MetricSpanExporter.ttyPrint(value));
}
MetricSpanExporter.defaultTracer = defaultTracer;
function relabelAll(attributes) {
return Object.entries(attributes).reduce(function reduceLabel(acc, [key, val,]) {
acc[(0, lodash_1.snakeCase)(key)] = String(val);
return acc;
}, {});
}
MetricSpanExporter.relabelAll = relabelAll;
MetricSpanExporter.defaultRelabel = {
"@opentelemetry/instrumentation-koa": relabelAll,
"middleware - allowedMethods"() { return null; },
"middleware - "() { return null; },
"@opentelemetry/instrumentation-http"(attributes) {
return {
route: attributes["http.route"],
status_code: attributes["http.status_code"],
};
},
"@opentelemetry/instrumentation-graphql"() { return {}; },
"graphql.resolve"() { return null; },
};
})(MetricSpanExporter = exports.MetricSpanExporter || (exports.MetricSpanExporter = {}));