Skip to content

Commit

Permalink
Merge branch 'main' into aspnet-telemetrycorrelation-otelintegration
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Aug 25, 2021
2 parents f697c52 + c81e628 commit fd3752f
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 41 deletions.
24 changes: 23 additions & 1 deletion examples/Console/TestPrometheusExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Threading;
Expand All @@ -27,7 +28,9 @@ namespace Examples.Console
internal class TestPrometheusExporter
{
private static readonly Meter MyMeter = new Meter("TestMeter", "0.0.1");
private static readonly Counter<long> Counter = MyMeter.CreateCounter<long>("counter");
private static readonly Counter<long> Counter = MyMeter.CreateCounter<long>("myCounter");
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("myHistogram");
private static readonly Random RandomGenerator = new Random();

internal static object Run(int port, int totalDurationInMins)
{
Expand All @@ -49,6 +52,19 @@ internal static object Run(int port, int totalDurationInMins)
.AddPrometheusExporter(opt => opt.Url = $"http://localhost:{port}/metrics/")
.Build();

ObservableGauge<long> gauge = MyMeter.CreateObservableGauge<long>(
"Gauge",
() =>
{
var tag1 = new KeyValuePair<string, object>("tag1", "value1");
var tag2 = new KeyValuePair<string, object>("tag2", "value2");
return new List<Measurement<long>>()
{
new Measurement<long>(RandomGenerator.Next(1, 1000), tag1, tag2),
};
});

using var token = new CancellationTokenSource();
Task writeMetricTask = new Task(() =>
{
Expand All @@ -63,6 +79,12 @@ internal static object Run(int port, int totalDurationInMins)
100,
new KeyValuePair<string, object>("tag1", "anothervalue"),
new KeyValuePair<string, object>("tag2", "somethingelse"));
MyHistogram.Record(
RandomGenerator.Next(1, 1500),
new KeyValuePair<string, object>("tag1", "value1"),
new KeyValuePair<string, object>("tag2", "value2"));
Task.Delay(10).Wait();
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

## 1.2.0-alpha1

Released 2021-Jul-23
Expand Down
7 changes: 7 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

* Add Histogram Metrics support.
* Changed default temporality to be cumulative.

## 1.2.0-alpha1

Released 2021-Jul-23
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

* Add Metrics
support.([#2192](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2192))

Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

## 1.2.0-alpha1

Released 2021-Jul-23
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

* The `OtlpExporterOptions` defaults can be overridden using
`OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS` and `OTEL_EXPORTER_OTLP_TIMEOUT`
envionmental variables as defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md).
([#2188](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2188))

* Changed default temporality for Metrics to be cumulative.

## 1.2.0-alpha1

Released 2021-Jul-23
Expand Down
7 changes: 7 additions & 0 deletions src/OpenTelemetry.Exporter.Prometheus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

* Revamped to support the new Metrics API/SDK.
Supports Counter, Gauge and Histogram.

## 1.0.0-rc1.1

Released 2020-Nov-17
Expand Down
132 changes: 92 additions & 40 deletions src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using OpenTelemetry.Exporter.Prometheus.Implementation;
Expand All @@ -28,12 +29,13 @@ namespace OpenTelemetry.Exporter
public static class PrometheusExporterExtensions
{
private const string PrometheusCounterType = "counter";
private const string PrometheusSummaryType = "summary";
private const string PrometheusSummarySumPostFix = "_sum";
private const string PrometheusSummaryCountPostFix = "_count";
private const string PrometheusSummaryQuantileLabelName = "quantile";
private const string PrometheusSummaryQuantileLabelValueForMin = "0";
private const string PrometheusSummaryQuantileLabelValueForMax = "1";
private const string PrometheusGaugeType = "gauge";
private const string PrometheusHistogramType = "histogram";
private const string PrometheusHistogramSumPostFix = "_sum";
private const string PrometheusHistogramCountPostFix = "_count";
private const string PrometheusHistogramBucketPostFix = "_bucket";
private const string PrometheusHistogramBucketLabelPositiveInfinity = "+Inf";
private const string PrometheusHistogramBucketLabelLessThan = "le";

/// <summary>
/// Serialize to Prometheus Format.
Expand All @@ -48,7 +50,7 @@ public static void WriteMetricsCollection(this PrometheusExporter exporter, Stre
{
var builder = new PrometheusMetricBuilder()
.WithName(metric.Name)
.WithDescription(metric.Name);
.WithDescription(metric.Description);

// TODO: Use switch case for higher perf.
if (metric.MetricType == MetricType.LongSum)
Expand All @@ -59,6 +61,25 @@ public static void WriteMetricsCollection(this PrometheusExporter exporter, Stre
{
WriteSum(writer, builder, metric.Attributes, (metric as ISumMetricDouble).DoubleSum);
}
else if (metric.MetricType == MetricType.DoubleGauge)
{
var gaugeMetric = metric as IGaugeMetric;
var doubleValue = (double)gaugeMetric.LastValue.Value;
WriteGauge(writer, builder, metric.Attributes, doubleValue);
}
else if (metric.MetricType == MetricType.LongGauge)
{
var gaugeMetric = metric as IGaugeMetric;
var longValue = (long)gaugeMetric.LastValue.Value;

// TODO: Prometheus only supports float/double
WriteGauge(writer, builder, metric.Attributes, longValue);
}
else if (metric.MetricType == MetricType.Histogram)
{
var histogramMetric = metric as IHistogramMetric;
WriteHistogram(writer, builder, metric.Attributes, metric.Name, histogramMetric.PopulationSum, histogramMetric.PopulationCount, histogramMetric.Buckets);
}
}
}
}
Expand Down Expand Up @@ -93,48 +114,79 @@ private static void WriteSum(StreamWriter writer, PrometheusMetricBuilder builde
builder.Write(writer);
}

private static void WriteSummary(
private static void WriteGauge(StreamWriter writer, PrometheusMetricBuilder builder, IEnumerable<KeyValuePair<string, object>> labels, double doubleValue)
{
builder = builder.WithType(PrometheusGaugeType);

var metricValueBuilder = builder.AddValue();
metricValueBuilder = metricValueBuilder.WithValue(doubleValue);

foreach (var label in labels)
{
metricValueBuilder.WithLabel(label.Key, label.Value.ToString());
}

builder.Write(writer);
}

private static void WriteHistogram(
StreamWriter writer,
PrometheusMetricBuilder builder,
IEnumerable<KeyValuePair<string, string>> labels,
IEnumerable<KeyValuePair<string, object>> labels,
string metricName,
double sum,
long count,
double min,
double max)
IEnumerable<HistogramBucket> buckets)
{
builder = builder.WithType(PrometheusSummaryType);
/* For Histogram we emit one row for Sum, Count and as
* many rows as number of buckets.
* myHistogram_sum{tag1="value1",tag2="value2"} 258330 1629860660991
* myHistogram_count{tag1="value1",tag2="value2"} 355 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="0"} 0 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="5"} 2 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="10"} 4 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="25"} 6 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="50"} 12 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="75"} 19 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="100"} 26 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="250"} 65 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="500"} 128 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="1000"} 241 1629860660991
* myHistogram_bucket{tag1="value1",tag2="value2",le="+Inf"} 355 1629860660991
*/

builder = builder.WithType(PrometheusHistogramType);
var metricValueBuilderSum = builder.AddValue();
metricValueBuilderSum.WithName(metricName + PrometheusHistogramSumPostFix);
metricValueBuilderSum = metricValueBuilderSum.WithValue(sum);
foreach (var label in labels)
{
/* For Summary we emit one row for Sum, Count, Min, Max.
Min,Max exports as quantile 0 and 1.
In future, when OpenTelemetry implements more aggregation
algorithms, this section will need to be revisited.
Sample output:
MyMeasure_sum{dim1="value1"} 750 1587013352982
MyMeasure_count{dim1="value1"} 5 1587013352982
MyMeasure{dim1="value2",quantile="0"} 150 1587013352982
MyMeasure{dim1="value2",quantile="1"} 150 1587013352982
*/
builder.AddValue()
.WithName(metricName + PrometheusSummarySumPostFix)
.WithLabel(label.Key, label.Value)
.WithValue(sum);
builder.AddValue()
.WithName(metricName + PrometheusSummaryCountPostFix)
.WithLabel(label.Key, label.Value)
.WithValue(count);
builder.AddValue()
.WithName(metricName)
.WithLabel(label.Key, label.Value)
.WithLabel(PrometheusSummaryQuantileLabelName, PrometheusSummaryQuantileLabelValueForMin)
.WithValue(min);
builder.AddValue()
.WithName(metricName)
.WithLabel(label.Key, label.Value)
.WithLabel(PrometheusSummaryQuantileLabelName, PrometheusSummaryQuantileLabelValueForMax)
.WithValue(max);
metricValueBuilderSum.WithLabel(label.Key, label.Value.ToString());
}

var metricValueBuilderCount = builder.AddValue();
metricValueBuilderCount.WithName(metricName + PrometheusHistogramCountPostFix);
metricValueBuilderCount = metricValueBuilderCount.WithValue(count);
foreach (var label in labels)
{
metricValueBuilderCount.WithLabel(label.Key, label.Value.ToString());
}

long totalCount = 0;
foreach (var bucket in buckets)
{
totalCount += bucket.Count;
var metricValueBuilderBuckets = builder.AddValue();
metricValueBuilderBuckets.WithName(metricName + PrometheusHistogramBucketPostFix);
metricValueBuilderBuckets = metricValueBuilderBuckets.WithValue(totalCount);
foreach (var label in labels)
{
metricValueBuilderBuckets.WithLabel(label.Key, label.Value.ToString());
}

var bucketName = double.IsPositiveInfinity(bucket.HighBoundary) ?
PrometheusHistogramBucketLabelPositiveInfinity : bucket.HighBoundary.ToString(CultureInfo.InvariantCulture);
metricValueBuilderBuckets.WithLabel(PrometheusHistogramBucketLabelLessThan, bucketName);
}

builder.Write(writer);
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

* Enabling endpoint configuration in ZipkinExporterOptions via
`OTEL_EXPORTER_ZIPKIN_ENDPOINT` environment variable.
([#1453](https://github.com/open-telemetry/opentelemetry-dotnet/issues/1453))
Expand Down
7 changes: 7 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

## 1.2.0-alpha2

Released 2021-Aug-24

* More Metrics features. All instrument types, push/pull
exporters, Delta/Cumulative temporality supported.

* `ResourceBuilder.CreateDefault` has detectors for
`OTEL_RESOURCE_ATTRIBUTES`, `OTEL_SERVICE_NAME` environment variables
so that explicit `AddEnvironmentVariableDetector` call is not needed. ([#2247](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2247))
Expand Down

0 comments on commit fd3752f

Please sign in to comment.