-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmetrics.ts
More file actions
80 lines (65 loc) · 2.38 KB
/
metrics.ts
File metadata and controls
80 lines (65 loc) · 2.38 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
import { createSingletonPromise } from '@antfu/utils'
import { metrics } from '@opentelemetry/api'
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
import { MeterProvider } from '@opentelemetry/sdk-metrics'
import { env } from './env'
export const getMetrics = createSingletonPromise(async () => {
if (!env.METRICS_ENABLED) return null
const exporter = new PrometheusExporter({ preventServerStart: true })
const meterProvider = new MeterProvider({
readers: [exporter],
})
metrics.setGlobalMeterProvider(meterProvider)
const meter = meterProvider.getMeter('github-actions-cache-server')
// HTTP Metrics
const httpRequestDuration = meter.createHistogram('http_request_duration_seconds', {
description: 'HTTP request latency in seconds',
unit: 's',
})
const httpRequestsTotal = meter.createCounter('http_requests_total', {
description: 'Total number of HTTP requests',
})
// Cache Metrics
const cacheOperationsTotal = meter.createCounter('cache_operations_total', {
description: 'Total cache operations (hit/miss/create)',
})
// Storage Metrics
const storageOperationsTotal = meter.createCounter('storage_operations_total', {
description: 'Total storage adapter operations',
})
const storageOperationDuration = meter.createHistogram('storage_operation_duration_seconds', {
description: 'Storage operation latency in seconds',
unit: 's',
})
// Database Metrics
const dbQueryDuration = meter.createHistogram('db_query_duration_seconds', {
description: 'Database query latency in seconds',
unit: 's',
})
const dbQueriesTotal = meter.createCounter('db_queries_total', {
description: 'Total database queries',
})
// Byte Transfer Metrics
const cacheBytesUploadedTotal = meter.createCounter('cache_bytes_uploaded_total', {
description: 'Total cache data uploaded in bytes',
unit: 'By',
})
const cacheBytesDownloadedTotal = meter.createCounter('cache_bytes_downloaded_total', {
description: 'Total cache data downloaded in bytes',
unit: 'By',
})
return {
exporter,
meter,
httpRequestDuration,
httpRequestsTotal,
cacheOperationsTotal,
storageOperationsTotal,
storageOperationDuration,
dbQueryDuration,
dbQueriesTotal,
cacheBytesUploadedTotal,
cacheBytesDownloadedTotal,
}
})
export type Metrics = NonNullable<Awaited<ReturnType<typeof getMetrics>>>