|
| 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