-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetrics.ts
More file actions
130 lines (106 loc) · 3.58 KB
/
metrics.ts
File metadata and controls
130 lines (106 loc) · 3.58 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const DOGSTATSD_HOST = process.env.DD_AGENT_HOST || "127.0.0.1";
const DOGSTATSD_PORT = Number(process.env.DD_DOGSTATSD_PORT || 8125);
const METRIC_PREFIX = process.env.DD_METRICS_PREFIX || "x402-facilitator";
const METRICS_ENABLED = process.env.DD_METRICS_ENABLED !== "false";
const baseTags = [
process.env.DD_SERVICE ? `service:${process.env.DD_SERVICE}` : null,
process.env.DD_ENV ? `env:${process.env.DD_ENV}` : null,
process.env.DD_VERSION ? `version:${process.env.DD_VERSION}` : null,
].filter((tag): tag is string => Boolean(tag));
type StatsDLike = {
increment: (name: string, value?: number, sampleRate?: number, tags?: string[]) => void;
gauge: (name: string, value: number, sampleRate?: number, tags?: string[]) => void;
histogram: (name: string, value: number, sampleRate?: number, tags?: string[]) => void;
};
let client: StatsDLike | null = null;
let initErrorLogged = false;
let sendErrorLogged = false;
function formatMetricName(name: string): string {
return `${METRIC_PREFIX}.${name}`;
}
function getClient(): StatsDLike | null {
if (!METRICS_ENABLED) {
return null;
}
if (client) {
return client;
}
try {
const hotShotsModule = require("hot-shots") as
| (new (options?: Record<string, unknown>) => StatsDLike)
| { default?: new (options?: Record<string, unknown>) => StatsDLike; StatsD?: new (options?: Record<string, unknown>) => StatsDLike };
const StatsDClass =
(typeof hotShotsModule === "function" ? hotShotsModule : hotShotsModule.StatsD || hotShotsModule.default);
if (!StatsDClass) {
throw new Error("hot-shots export not found");
}
client = new StatsDClass({
host: DOGSTATSD_HOST,
port: DOGSTATSD_PORT,
globalTags: baseTags,
errorHandler: (error: Error) => {
if (!sendErrorLogged) {
sendErrorLogged = true;
console.error("[metrics] hot-shots send error:", error.message);
}
},
});
return client;
} catch (error) {
if (!initErrorLogged) {
initErrorLogged = true;
console.error("[metrics] Failed to initialize hot-shots client (metrics disabled):", error);
}
return null;
}
}
function mergedTags(tags?: string[]): string[] | undefined {
if (!tags || tags.length === 0) {
return undefined;
}
return tags;
}
export function incrementMetric(name: string, tags?: string[], value = 1): void {
if (!Number.isFinite(value)) {
return;
}
const statsd = getClient();
if (!statsd) {
return;
}
statsd.increment(formatMetricName(name), value, 1, mergedTags(tags));
}
export function gaugeMetric(name: string, value: number, tags?: string[]): void {
if (!Number.isFinite(value)) {
return;
}
const statsd = getClient();
if (!statsd) {
return;
}
statsd.gauge(formatMetricName(name), value, 1, mergedTags(tags));
}
export function histogramMetric(name: string, value: number, tags?: string[]): void {
if (!Number.isFinite(value)) {
return;
}
const statsd = getClient();
if (!statsd) {
return;
}
statsd.histogram(formatMetricName(name), value, 1, mergedTags(tags));
}
export function timeAsync<T>(name: string, tags: string[], operation: () => Promise<T>): Promise<T> {
const startedAt = Date.now();
return operation()
.then(result => {
histogramMetric(name, Date.now() - startedAt, [...tags, "outcome:success"]);
return result;
})
.catch(error => {
histogramMetric(name, Date.now() - startedAt, [...tags, "outcome:failure"]);
throw error;
});
}