@@ -16,11 +16,18 @@ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
16
16
import { HostMetrics } from '@opentelemetry/host-metrics' ;
17
17
import { type IResource } from '@opentelemetry/resources' ;
18
18
import { type LoggerProvider } from '@opentelemetry/sdk-logs' ;
19
- import { MeterProvider , PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics' ;
19
+ import {
20
+ ExplicitBucketHistogramAggregation ,
21
+ InstrumentType ,
22
+ MeterProvider ,
23
+ PeriodicExportingMetricReader ,
24
+ View ,
25
+ } from '@opentelemetry/sdk-metrics' ;
20
26
import { BatchSpanProcessor , NodeTracerProvider } from '@opentelemetry/sdk-trace-node' ;
21
27
import { ATTR_SERVICE_NAME , ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions' ;
22
28
23
29
import { type TelemetryClientConfig } from './config.js' ;
30
+ import { linearBuckets } from './histogram_utils.js' ;
24
31
import { registerOtelLoggerProvider } from './otel_logger_provider.js' ;
25
32
import { getOtelResource } from './otel_resource.js' ;
26
33
import { type Gauge , type TelemetryClient } from './telemetry.js' ;
@@ -140,6 +147,43 @@ export class OpenTelemetryClient implements TelemetryClient {
140
147
exportTimeoutMillis : config . otelExportTimeoutMs ,
141
148
} ) ,
142
149
] ,
150
+ views : [
151
+ // Every histogram matching the selector (type + unit) gets these custom buckets assigned
152
+ new View ( {
153
+ instrumentType : InstrumentType . HISTOGRAM ,
154
+ instrumentUnit : 'ms' ,
155
+ aggregation : new ExplicitBucketHistogramAggregation (
156
+ // 181 buckets between 5ms and 1hr
157
+ [
158
+ ...linearBuckets ( 5 , 100 , 20 ) , // 20 buckets between 5 and 100ms
159
+ ...linearBuckets ( 100 , 1_000 , 20 ) . slice ( 1 ) , // another 20 buckets between 100ms and 1s. slice(1) to remove duplicate 100
160
+ ...linearBuckets ( 1_000 , 10_000 , 20 ) . slice ( 1 ) ,
161
+ ...linearBuckets ( 10_000 , 60_000 , 20 ) . slice ( 1 ) ,
162
+ ...linearBuckets ( 60_000 , 300_000 , 20 ) . slice ( 1 ) ,
163
+ ...linearBuckets ( 300_000 , 600_000 , 20 ) . slice ( 1 ) ,
164
+ ...linearBuckets ( 600_000 , 1_200_000 , 20 ) . slice ( 1 ) ,
165
+ ...linearBuckets ( 1_200_000 , 1_800_000 , 20 ) . slice ( 1 ) ,
166
+ ...linearBuckets ( 1_800_000 , 3_600_000 , 20 ) . slice ( 1 ) , // 1hr
167
+ ] ,
168
+ true ,
169
+ ) ,
170
+ } ) ,
171
+ new View ( {
172
+ instrumentType : InstrumentType . HISTOGRAM ,
173
+ instrumentUnit : 'By' ,
174
+ aggregation : new ExplicitBucketHistogramAggregation (
175
+ // 143 buckets between 32 bytes and 2MB
176
+ [
177
+ ...linearBuckets ( 2 ** 5 , 2 ** 10 , 31 ) , // 32 bytes to 1KB at a resolution of 32 bytes
178
+ ...linearBuckets ( 2 ** 10 , 2 ** 15 , 31 ) . slice ( 1 ) , // 1KB to 32KB at a resolution of 1KB
179
+ ...linearBuckets ( 2 ** 15 , 2 ** 18 , 32 ) . slice ( 1 ) , // 32KB to 256KB at a resolution of 7KB
180
+ ...linearBuckets ( 2 ** 18 , 2 ** 20 , 32 ) . slice ( 1 ) , // 256KB to 1MB at a resolution of 24KB
181
+ ...linearBuckets ( 2 ** 20 , 2 ** 21 , 16 ) . slice ( 1 ) , // 1MB to 2MB at a resolution of 64KB
182
+ ] ,
183
+ true ,
184
+ ) ,
185
+ } ) ,
186
+ ] ,
143
187
} ) ;
144
188
145
189
const loggerProvider = await registerOtelLoggerProvider ( resource , config . logsCollectorUrl ) ;
0 commit comments