Views allow you to customize how metrics are collected and exported. You can use them to:
- Change aggregation: Apply specific aggregation logic (like
ExponentialBucketHistogram) to instruments matching a name pattern. - Drop instruments: Suppress noisy or low-value instrumentation by using
OpenTelemetry::SDK::Metrics::Aggregation::Drop. - Restrict attribute keys: Use an allowlist to keep only specific attribute keys and drop all others.
add_view supports wildcard matching (* and ?) for instrument names.
# Change aggregation for a specific instrument using wildcards
OpenTelemetry.meter_provider.add_view(
'*exponential*',
aggregation: OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(
aggregation_temporality: :cumulative,
max_scale: 20
),
type: :histogram,
unit: 'ms'
)
# Drop all metrics from a specific meter
OpenTelemetry.meter_provider.add_view(
'*',
aggregation: OpenTelemetry::SDK::Metrics::Aggregation::Drop.new,
meter_name: 'noisy_library'
)
# Restrict which attribute keys are retained
OpenTelemetry.meter_provider.add_view(
'http.request.duration',
attribute_keys: { 'http.method' => nil, 'http.status_code' => nil }
)