|
| 1 | +/* Copyright (C) 2022 The Fluent Bit authors |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api'); |
| 19 | +const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); |
| 20 | +const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base'); |
| 21 | +const { Resource } = require('@opentelemetry/resources'); |
| 22 | +const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); |
| 23 | + |
| 24 | +// Optional and only needed to see the internal diagnostic logging (during development) |
| 25 | +diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); |
| 26 | + |
| 27 | +const metricExporter = new OTLPMetricExporter({'url':'http://fluentd:4318/v1/metrics'}); |
| 28 | + |
| 29 | +const meterProvider = new MeterProvider({ |
| 30 | + resource: new Resource({ |
| 31 | + [SemanticResourceAttributes.SERVICE_NAME]: 'basic-metric-service', |
| 32 | + }), |
| 33 | +}); |
| 34 | + |
| 35 | +meterProvider.addMetricReader(new PeriodicExportingMetricReader({ |
| 36 | + exporter: metricExporter, |
| 37 | + exportIntervalMillis: 1000, |
| 38 | +})); |
| 39 | + |
| 40 | +const meter = meterProvider.getMeter('example-exporter-collector'); |
| 41 | + |
| 42 | +const requestCounter = meter.createCounter('requests', { |
| 43 | + description: 'Example of a Counter', |
| 44 | +}); |
| 45 | + |
| 46 | +const upDownCounter = meter.createUpDownCounter('test_up_down_counter', { |
| 47 | + description: 'Example of a UpDownCounter', |
| 48 | +}); |
| 49 | + |
| 50 | +const attributes = { pid: process.pid, environment: 'staging' }; |
| 51 | + |
| 52 | +setInterval(() => { |
| 53 | + requestCounter.add(15, attributes); |
| 54 | + upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes); |
| 55 | +}, 1000); |
0 commit comments