Skip to content

Commit d7c17af

Browse files
committed
docs(metrics): document advisory attributes parameter with example
Add documentation and example for the new advisory attributes parameter: - Update README with usage instructions and examples - Create standalone example demonstrating the feature - Include notes about experimental status and performance benefits - Provide clear guidance on when and how to use advisory attributes
1 parent 82948e1 commit d7c17af

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

packages/sdk-metrics/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ const counter = opentelemetry.metrics.getMeter('default').createCounter('foo');
3636
counter.add(1, { attributeKey: 'attribute-value' });
3737
```
3838

39+
### Advisory Attributes Parameter (Experimental)
40+
41+
The Metrics API supports an optional `attributes` advisory parameter in the `advice` configuration when creating instruments. This parameter acts as an allow-list for attribute keys, filtering out all attributes that are not in the specified list:
42+
43+
```js
44+
const counter = opentelemetry.metrics.getMeter('default').createCounter('filtered-counter', {
45+
description: 'A counter with attribute filtering',
46+
advice: {
47+
attributes: ['service', 'version'], // @experimental: Only these keys will be kept
48+
},
49+
});
50+
51+
// Only 'service' and 'version' attributes will be recorded, others are filtered out
52+
counter.add(1, {
53+
service: 'api-gateway',
54+
version: '1.0.0',
55+
region: 'us-west', // This will be filtered out
56+
method: 'GET' // This will be filtered out
57+
});
58+
```
59+
60+
**Note:** This feature is experimental and may change in future versions. The advisory attributes parameter only applies when no Views are configured for the instrument. If Views are present, they take precedence over the instrument's advisory attributes.
61+
3962
In conditions, we may need to setup an async instrument to observe costly events:
4063

4164
```js
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
3+
/*
4+
* This example demonstrates the new advisory attributes parameter for metrics instruments.
5+
* The attributes parameter acts as an allow-list for the instrument, filtering out
6+
* all attribute keys that are not in the specified list.
7+
*/
8+
9+
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
10+
const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
11+
const { PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
12+
13+
// Create a meter provider with a console exporter
14+
const meterProvider = new MeterProvider({
15+
readers: [
16+
new PeriodicExportingMetricReader({
17+
exporter: new ConsoleMetricExporter(),
18+
exportIntervalMillis: 1000,
19+
}),
20+
],
21+
});
22+
23+
const meter = meterProvider.getMeter('advisory-attributes-example');
24+
25+
// Create a counter with advisory attributes - only 'service' and 'version' keys will be kept
26+
const requestCounter = meter.createCounter('http_requests_total', {
27+
description: 'Total number of HTTP requests',
28+
advice: {
29+
attributes: ['service', 'version'], // @experimental: Only these keys will be allowed
30+
},
31+
});
32+
33+
// Create a histogram without advisory attributes - all keys will be kept
34+
const responseTimeHistogram = meter.createHistogram('http_response_time', {
35+
description: 'HTTP response time in milliseconds',
36+
});
37+
38+
// Record some measurements
39+
console.log('Recording metrics with advisory attributes filtering...\n');
40+
41+
// This will only keep 'service' and 'version' attributes, filtering out 'method' and 'endpoint'
42+
requestCounter.add(1, {
43+
service: 'api-gateway',
44+
version: '1.2.3',
45+
method: 'GET', // This will be filtered out
46+
endpoint: '/users', // This will be filtered out
47+
});
48+
49+
requestCounter.add(1, {
50+
service: 'user-service',
51+
version: '2.1.0',
52+
method: 'POST', // This will be filtered out
53+
endpoint: '/auth', // This will be filtered out
54+
region: 'us-west-2', // This will be filtered out
55+
});
56+
57+
// This will keep all attributes since no advisory attributes are specified
58+
responseTimeHistogram.record(150, {
59+
service: 'api-gateway',
60+
method: 'GET',
61+
endpoint: '/users',
62+
status_code: '200',
63+
});
64+
65+
responseTimeHistogram.record(89, {
66+
service: 'user-service',
67+
method: 'POST',
68+
endpoint: '/auth',
69+
status_code: '201',
70+
region: 'us-west-2',
71+
});
72+
73+
console.log('Check the console output above to see the filtered attributes!');
74+
console.log('The http_requests_total metric should only have service and version attributes.');
75+
console.log('The http_response_time metric should have all attributes.');
76+
77+
// Keep the process running for a bit to see the output
78+
setTimeout(() => {
79+
console.log('\nShutting down...');
80+
meterProvider.shutdown();
81+
}, 2000);

0 commit comments

Comments
 (0)