-
Notifications
You must be signed in to change notification settings - Fork 838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(exporter-metrics-otlp-*): configure histogram aggregation #5061
Closed
olabodeIdowu
wants to merge
1
commit into
open-telemetry:main
from
olabodeIdowu:olabode-contrib-otep-issues
+113
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLMetricExporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
// To implement default histogram aggregation for OTLP Metrics exporters in TypeScript, you can use the OpenTelemetry library. | ||
// Below is a sample code snippet that demonstrates how to allow for either Explicit Bucket histogram aggregation or Exponential histogram aggregation based on an environment variable. | ||
|
||
// ### Explanation: | ||
// 1. *Environment Variable*: The OTLP_HISTOGRAM_AGGREGATION environment variable is set to either 'explicit' or 'exponential'. | ||
// 2. *Histogram Aggregation Configuration*: | ||
// - For *Explicit Bucket*, boundaries are defined. | ||
// - For *Exponential*, a scale factor and bounds are defined. | ||
// 3. *Metric Reader*: A PeriodicExportingMetricReader is set up to export metrics at a specified interval. | ||
// 4. *Meter Provider*: The global meter provider is set to start collecting and exporting metrics. | ||
// 5. *Histogram Metric Creation*: A histogram metric is created based on the chosen aggregation type, and sample values are recorded. | ||
|
||
// ### Usage: | ||
// - Change the value of process.env.OTLP_HISTOGRAM_AGGREGATION before running the code to select the desired histogram aggregation method. | ||
// - Adjust the bucket boundaries and export interval according to your needs. | ||
|
||
|
||
//Here’s how you can set it up: | ||
|
||
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'; | ||
import { OtlpHttpExporter } from '@opentelemetry/exporter-otlp-http'; | ||
import { Histogram, Meter } from '@opentelemetry/api'; | ||
import { HistogramAggregation } from '@opentelemetry/sdk-metrics'; | ||
|
||
// Set the environment variable for histogram aggregation | ||
// Use 'explicit' for Explicit Bucket histogram or 'exponential' for Exponential histogram | ||
process.env.OTLP_HISTOGRAM_AGGREGATION = 'explicit'; // or 'exponential' | ||
|
||
// Function to configure histogram aggregation based on the environment variable | ||
function configureHistogramAggregation(): HistogramAggregation { | ||
const histogramType = process.env.OTLP_HISTOGRAM_AGGREGATION || 'explicit'; | ||
|
||
let histogramAggregation: HistogramAggregation; | ||
|
||
if (histogramType === 'explicit') { | ||
console.log("Configuring Explicit Bucket histogram aggregation..."); | ||
histogramAggregation = { | ||
type: 'explicit', | ||
boundaries: [0, 10, 20, 30, 40, 50], // Define your explicit bucket boundaries | ||
}; | ||
} else if (histogramType === 'exponential') { | ||
console.log("Configuring Exponential histogram aggregation..."); | ||
histogramAggregation = { | ||
type: 'exponential', | ||
scale: 2.0, // Scale factor for buckets | ||
bounds: [1, 2, 5, 10, 20, 50, 100], // Define your exponential bounds | ||
}; | ||
} else { | ||
console.log("Invalid histogram aggregation type specified. Defaulting to Explicit Bucket."); | ||
histogramAggregation = { | ||
type: 'explicit', | ||
boundaries: [0, 10, 20, 30, 40, 50], | ||
}; | ||
} | ||
|
||
return histogramAggregation; | ||
} | ||
|
||
// Initialize the MeterProvider and configure the exporter | ||
const meterProvider = new MeterProvider(); | ||
const otlpExporter = new OtlpHttpExporter({ | ||
url: 'http://localhost:4318/v1/metrics' // Replace with your OTLP endpoint | ||
}); | ||
|
||
// Create the metric reader with the chosen histogram aggregation | ||
const metricReader = new PeriodicExportingMetricReader({ | ||
exporter: otlpExporter, | ||
exportIntervalMillis: 5000, // Adjust export interval as needed | ||
}); | ||
|
||
// Register the metric reader with the MeterProvider | ||
meterProvider.addMetricReader(metricReader); | ||
|
||
// Set the global meter provider | ||
const meter = meterProvider.getMeter('example-meter'); | ||
|
||
// Call the function to get the histogram aggregation configuration | ||
const histogramAggregation = configureHistogramAggregation(); | ||
|
||
// Create a histogram metric with the chosen aggregation | ||
const histogram: Histogram = meter.createHistogram('example_histogram', { | ||
description: 'An example histogram', | ||
unit: '1', | ||
aggregation: histogramAggregation, | ||
}); | ||
|
||
// Record some values in the histogram | ||
histogram.record(5); | ||
histogram.record(15); | ||
histogram.record(25); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this change intentional? I don't see any usage of the
sdk-logs
package in the example above.