Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Try exporter with latest version of OTel .NET SDK #37

Merged
merged 7 commits into from
Jan 17, 2023
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
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ directly to [Dynatrace](https://www.dynatrace.com).
More information on exporting OpenTelemetry metrics to Dynatrace can be found in the
[Dynatrace documentation](https://www.dynatrace.com/support/help/extend-dynatrace/opentelemetry/opentelemetry-metrics).

This exporter is built against the OpenTelemetry .NET
SDK [v1.2.0](https://github.com/open-telemetry/opentelemetry-dotnet/releases/tag/core-1.2.0) which is the first stable
release for the metrics SDK and should be compatible with this and later versions.
This exporter is built against the OpenTelemetry .NET SDK
[v1.2.0](https://github.com/open-telemetry/opentelemetry-dotnet/releases/tag/core-1.2.0).
It should be compatible with applications targeting OpenTelemetry .NET SDK version 1.2.0 and higher.

> It is highly recommended to update the OpenTelemetry .NET SDK in your applications to version `1.3.1` or later
> to avoid being impacted by this issue:
> [Possible app crash for OpenTelemetry .NET versions 1.3.0 and prior](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3629)

## Getting started

The general setup of OpenTelemetry .NET is explained in the official [Getting Started Guide](https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.2.0/docs/metrics/getting-started/README.md).
The general setup of OpenTelemetry .NET is explained in the official [Getting Started Guide](https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.3.2/docs/metrics/getting-started/README.md).

To add the exporter to your project, install the [Dynatrace.OpenTelemetry.Exporter.Metrics](https://www.nuget.org/packages/Dynatrace.OpenTelemetry.Exporter.Metrics) package to your project.
This can be done through the NuGet package manager in Visual Studio or by running the following command in your project folder:
Expand All @@ -21,13 +25,19 @@ This can be done through the NuGet package manager in Visual Studio or by runnin
dotnet add package Dynatrace.OpenTelemetry.Exporter.Metrics
```

This exporter package targets [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) and can therefore be included on .NET Core 2.0 and above, as well as .NET Framework 4.6.1 and above.
This exporter package targets [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) and can therefore be included on .NET Core 2.0 and above, as well as .NET Framework 4.6.2 and above.
joaopgrassi marked this conversation as resolved.
Show resolved Hide resolved

### Setup

To set up a Dynatrace metrics exporter, add the following code to your project:

```csharp
using System.Diagnostics;
joaopgrassi marked this conversation as resolved.
Show resolved Hide resolved
using System.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using Dynatrace.OpenTelemetry.Exporter.Metrics;

// A Meter instance is obtained via the System.Diagnostics.DiagnosticSource package
var meter = new Meter("my_meter", "0.0.1");

Expand Down Expand Up @@ -59,6 +69,13 @@ and it is recommended to restrict the token access to that scope.
More information about the token setup can be found [here](#dynatrace-api-token).

```csharp
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using Dynatrace.OpenTelemetry.Exporter.Metrics;

// Not required, but potentially helpful.
// The exporter logs information about preparing and exporting metrics.
var loggerFactory = LoggerFactory.Create(builder =>
Expand Down Expand Up @@ -96,6 +113,13 @@ configured in the `DynatraceExporterOptions`.
Read the [Configuration section](#configuration) to learn more about each of them.

```csharp
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using Dynatrace.OpenTelemetry.Exporter.Metrics;

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddDynatraceExporter(cfg =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.2.0" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.2.0-rc5" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.3.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -728,6 +729,67 @@ public async Task Export_Histogram_ShouldSetMinAndMaxCorrectly(MinMaxEstimationD
AssertExportRequest(actualRequestMessage);
}

[Theory]
[InlineData("en-US")]
[InlineData("en-GB")]
[InlineData("pt-BR")]
[InlineData("de-AT")]
[InlineData("fi-FI")]
public async Task TestAllInstrumentsWithCulture(string locale)
{
var originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);

// Arrange
HttpRequestMessage actualRequestMessage = null!;
var mockMessageHandler = SetupHttpMock(r => actualRequestMessage = r);

var sut = new DynatraceMetricsExporter(null, null, new HttpClient(mockMessageHandler.Object));

var longCounter = _meter.CreateCounter<long>("counter");
longCounter.Add(10);

var doubleCounter = _meter.CreateCounter<double>("double_counter");
doubleCounter.Add(10.3);

_meter.CreateObservableCounter("obs_counter",
() => new List<Measurement<long>> { new(1 * 10) });

_meter.CreateObservableCounter("double_obs_counter",
() => new List<Measurement<double>> { new(1 * 10.3, _attributes) });

var longHistogram = _meter.CreateHistogram<long>("histogram");
longHistogram.Record(1);
longHistogram.Record(6);
longHistogram.Record(11);
longHistogram.Record(21);

var doubleHistogram = _meter.CreateHistogram<double>("double_histogram");
doubleHistogram.Record(1.1);
doubleHistogram.Record(6.2);
doubleHistogram.Record(11.3);
doubleHistogram.Record(21.23);

// Act
_meterProvider.ForceFlush();
sut.Export(new Batch<Metric>(_exportedMetrics.ToArray(), _exportedMetrics.Count));

// Assert
var point = MetricTest.FromMetricPoints(_exportedMetrics.First().GetMetricPoints()).First();

var expected = @$"counter,dt.metrics.source=opentelemetry count,delta=10 {point.TimeStamp}
double_counter,dt.metrics.source=opentelemetry count,delta=10.3 {point.TimeStamp}
obs_counter,dt.metrics.source=opentelemetry count,delta=10 {point.TimeStamp}
double_obs_counter,attr1=v1,attr2=v2,dt.metrics.source=opentelemetry count,delta=10.3 {point.TimeStamp}
histogram,dt.metrics.source=opentelemetry gauge,min=0,max=25,sum=39,count=4 {point.TimeStamp}
double_histogram,dt.metrics.source=opentelemetry gauge,min=0,max=25,sum=39.83,count=4 {point.TimeStamp}";

var actualMetricString = await actualRequestMessage.Content!.ReadAsStringAsync();
Assert.Equal(expected, actualMetricString);

Thread.CurrentThread.CurrentCulture = originalCulture;
}

private static Mock<HttpMessageHandler> SetupHttpMock(
Action<HttpRequestMessage>? setter = null,
HttpStatusCode? statusCode = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Company>Dynatrace</Company>
<Product>Dynatrace OpenTelemetry Metrics Exporter for .NET</Product>
<PackageId>Dynatrace.OpenTelemetry.Exporter.Metrics</PackageId>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Description>See https://github.com/dynatrace-oss/opentelemetry-metric-dotnet to learn more.</Description>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Copyright>Copyright 2020 Dynatrace LLC; Licensed under the Apache License, Version 2.0</Copyright>
Expand All @@ -19,7 +19,7 @@

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.2.0" />
<PackageReference Include="Dynatrace.MetricUtils" Version="0.3.0" />
<PackageReference Include="Dynatrace.MetricUtils" Version="0.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ private static MeterProviderBuilder AddDynatraceExporter(
}

var metricExporter = new DynatraceMetricsExporter(options, logger);
var metricReader = new PeriodicExportingMetricReader(metricExporter, options.MetricExportIntervalMilliseconds);
metricReader.TemporalityPreference = MetricReaderTemporalityPreference.Delta;
var metricReader = new PeriodicExportingMetricReader(metricExporter, options.MetricExportIntervalMilliseconds)
{
TemporalityPreference = MetricReaderTemporalityPreference.Delta
};
return builder.AddReader(metricReader);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Examples.Console/Examples.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.3.2" />
joaopgrassi marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="CommandLineParser" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.9" />
</ItemGroup>
Expand Down