-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmetrics.ts
More file actions
95 lines (81 loc) · 2.6 KB
/
Copy pathmetrics.ts
File metadata and controls
95 lines (81 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {
ExplicitBucketHistogramAggregation,
MeterProvider,
OTLPMetricExporter,
PeriodicExportingMetricReader,
View,
} from "../../deps.ts";
import { OTEL_IS_ENABLED, resource } from "./config.ts";
export const OTEL_ENABLE_EXTRA_METRICS: boolean = Deno.env.has(
"OTEL_ENABLE_EXTRA_METRICS",
);
// 2 minutes. We don't need frequent updates here.
export const OTEL_EXPORT_INTERVAL: number = parseInt(
Deno.env.get("OTEL_EXPORT_INTERVAL") ?? "60000",
10,
);
// a=b,c=d => {a:b, c:d}
const headersStringToObject = (headersString: string | undefined | null) => {
if (!headersString) {
return {};
}
const splitByComma = headersString.split(",").map((keyVal) =>
keyVal.split("=") as [string, string]
);
return Object.fromEntries(splitByComma);
};
// Add views with different boundaries for each unit.
const msBoundaries = [10, 100, 500, 1000, 5000, 10000, 15000];
const sBoundaries = [1, 5, 10, 50];
type IMeter = ReturnType<MeterProvider["getMeter"]>;
const meterProvider: MeterProvider = new MeterProvider({
resource,
views: [
new View({
instrumentUnit: "ms",
aggregation: new ExplicitBucketHistogramAggregation(msBoundaries),
}),
new View({
instrumentUnit: "s",
aggregation: new ExplicitBucketHistogramAggregation(sBoundaries),
}),
],
});
if (OTEL_IS_ENABLED) {
const metricExporter = new OTLPMetricExporter({
url: Deno.env.get("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT") ??
`${Deno.env.get("OTEL_EXPORTER_OTLP_ENDPOINT")}/v1/metrics`,
headers: headersStringToObject(Deno.env.get("OTEL_EXPORTER_OTLP_HEADERS")),
});
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: OTEL_EXPORT_INTERVAL,
}),
);
}
export const meter: IMeter = meterProvider.getMeter("deco");
// Outgoing fetch metrics — disabled by default. Enable with OTEL_METRICS_OUTGOING_FETCH=true.
import { ValueType } from "../../deps.ts";
import { onFetch, parseUrlParts } from "../../utils/patched_fetch.ts";
if (Deno.env.get("OTEL_METRICS_OUTGOING_FETCH") === "true") {
const fetchCount = meter.createCounter("outgoing_fetch", {
unit: "1",
valueType: ValueType.INT,
});
const fetchDuration = meter.createHistogram("outgoing_fetch_duration", {
unit: "ms",
valueType: ValueType.DOUBLE,
});
onFetch((event) => {
const { host } = parseUrlParts(event.url);
const attrs = {
app: event.app ?? "unknown",
host: host ?? "unknown",
method: event.method,
status: event.status,
};
fetchCount.add(1, attrs);
fetchDuration.record(event.durationMs, attrs);
});
} // OTEL_METRICS_OUTGOING_FETCH