Skip to content
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

[Exporter.Geneva] Add support for UpDownCounter and ObservableUpDownCounter #685

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Add support for exporting `UpDownCounter` and `ObservableUpDownCounter`.
[#685](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/685)

## 1.4.0-beta.2

Released 2022-Oct-17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,25 @@ public override ExportResult Export(in Batch<Metric> batch)
break;
}

// The value here could be negative hence we have to use `MetricEventType.DoubleMetric`
case MetricType.LongSumNonMonotonic:
{
// potential for minor precision loss implicitly going from long->double
// see: https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/numeric-conversions#implicit-numeric-conversions
var doubleSum = Convert.ToDouble(metricPoint.GetSumLong());
var metricData = new MetricData { DoubleValue = doubleSum };
var bodyLength = this.SerializeMetric(
MetricEventType.DoubleMetric,
metric.Name,
metricPoint.EndTime.ToFileTime(),
metricPoint.Tags,
metricData);
this.metricDataTransport.Send(MetricEventType.DoubleMetric, this.bufferForNonHistogramMetrics, bodyLength);
break;
}

case MetricType.DoubleSum:
case MetricType.DoubleSumNonMonotonic:
{
var doubleSum = metricPoint.GetSumDouble();
var metricData = new MetricData { DoubleValue = doubleSum };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462;net47;net471;net472;net48</TargetFrameworks>
<NoWarn>$(NoWarn),SA1201,SA1202,SA1204,SA1311,SA1123</NoWarn>
</PropertyGroup>
Expand All @@ -11,7 +11,6 @@
<PackageReference Include="BenchmarkDotNet" Version="$(BenchmarkDotNetPkgVer)" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryPkgVer)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="[6.0.0,)" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="5.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462;net47;net471;net472;net48</TargetFrameworks>
<NoWarn>$(NoWarn),SA1308,SA1201</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void SuccessfulSerialization(bool testMaxLimits)
using var meter = new Meter("SuccessfulSerialization", "0.0.1");
var longCounter = meter.CreateCounter<long>("longCounter");
var doubleCounter = meter.CreateCounter<double>("doubleCounter");
var longUpDownCounter = meter.CreateUpDownCounter<long>("longUpDownCounter");
var doubleUpDownCounter = meter.CreateUpDownCounter<double>("doubleUpDownCounter");
var histogram = meter.CreateHistogram<long>("histogram");
var exportedItems = new List<Metric>();
using var inMemoryReader = new BaseExportingMetricReader(new InMemoryExporter<Metric>(exportedItems))
Expand Down Expand Up @@ -129,6 +131,12 @@ public void SuccessfulSerialization(bool testMaxLimits)
doubleCounter.Add(
doubleValue, new("tag1", "value1"), new("tag2", "value2"));

longUpDownCounter.Add(
longValue, new("tag1", "value1"), new("tag2", "value2"));

doubleUpDownCounter.Add(
longValue, new("tag1", "value1"), new("tag2", "value2"));

meter.CreateObservableCounter(
"observableLongCounter",
() => new List<Measurement<long>>()
Expand Down Expand Up @@ -157,6 +165,20 @@ public void SuccessfulSerialization(bool testMaxLimits)
new(doubleValue, new("tag1", "value1"), new("tag2", "value2")),
});

meter.CreateObservableUpDownCounter(
"observableUpDownLongCounter",
() => new List<Measurement<long>>()
{
new(longValue, new("tag1", "value1"), new("tag2", "value2")),
});

meter.CreateObservableUpDownCounter(
"observableUpDownDoubleCounter",
() => new List<Measurement<double>>()
{
new(doubleValue, new("tag1", "value1"), new("tag2", "value2")),
});

if (testMaxLimits)
{
// only testing the max value allowed for sum
Expand Down Expand Up @@ -221,28 +243,40 @@ public void SuccessfulSerialization(bool testMaxLimits)

inMemoryReader.Collect();

Assert.Equal(7, exportedItems.Count);
Assert.Equal(11, exportedItems.Count);

// check serialization for longCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[0], exporter, exporterOptions);

// check serialization for doubleCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[1], exporter, exporterOptions);

// check serialization for histogram
// check serialization for longUpDownCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[2], exporter, exporterOptions);

// check serialization for observableLongCounter
// check serialization for doubleUpDownCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[3], exporter, exporterOptions);

// check serialization for observableDoubleCounter
// check serialization for histogram
this.CheckSerializationForSingleMetricPoint(exportedItems[4], exporter, exporterOptions);

// check serialization for observableLongGauge
// check serialization for observableLongCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[5], exporter, exporterOptions);

// check serialization for observableDoubleGauge
// check serialization for observableDoubleCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[6], exporter, exporterOptions);

// check serialization for observableLongGauge
this.CheckSerializationForSingleMetricPoint(exportedItems[7], exporter, exporterOptions);

// check serialization for observableDoubleGauge
this.CheckSerializationForSingleMetricPoint(exportedItems[8], exporter, exporterOptions);

// check serialization for observableUpDownLongCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[9], exporter, exporterOptions);

// check serialization for observableUpDownDoubleCounter
this.CheckSerializationForSingleMetricPoint(exportedItems[10], exporter, exporterOptions);
}
finally
{
Expand Down Expand Up @@ -653,6 +687,27 @@ private void CheckSerializationForSingleMetricPoint(Metric metric, GenevaMetricE
Assert.Equal((ushort)MetricEventType.DoubleMetric, data.EventId);
Assert.Equal(bodyLength, data.LenBody);
}
else if (metricType == MetricType.LongSumNonMonotonic || metricType == MetricType.DoubleSumNonMonotonic)
{
var metricDataValue = metricType == MetricType.LongSumNonMonotonic ?
Convert.ToDouble(metricPoint.GetSumLong()) :
Convert.ToDouble(metricPoint.GetSumDouble());
var metricData = new MetricData { DoubleValue = metricDataValue };
var bodyLength = exporter.SerializeMetric(
MetricEventType.DoubleMetric,
metric.Name,
metricPoint.EndTime.ToFileTime(),
metricPoint.Tags,
metricData);
var buffer = typeof(GenevaMetricExporter).GetField("bufferForNonHistogramMetrics", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(exporter) as byte[];
var stream = new KaitaiStream(buffer);
data = new MetricsContract(stream);
var valueSection = data.Body.ValueSection as SingleDoubleValue;
Assert.Equal(metricDataValue, valueSection.Value);
Assert.Equal((ulong)metricPoint.EndTime.ToFileTime(), valueSection.Timestamp);
Assert.Equal((ushort)MetricEventType.DoubleMetric, data.EventId);
Assert.Equal(bodyLength, data.LenBody);
}
else if (metricType == MetricType.Histogram)
{
var sum = new MetricData { UInt64Value = Convert.ToUInt64(metricPoint.GetHistogramSum()) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Unit test project for Geneva Exporters for OpenTelemetry</Description>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462;net47;net471;net472;net48</TargetFrameworks>
<NoWarn>$(NoWarn),SA1311,SA1312,SA1313,SA1123,SA1202</NoWarn>
</PropertyGroup>
Expand Down Expand Up @@ -31,7 +31,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryPkgVer)" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.4.0-beta.2" />
</ItemGroup>

<ItemGroup>
Expand Down