-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs and fixes for histogram (#2269)
- Loading branch information
1 parent
95e6851
commit d649b3d
Showing
10 changed files
with
257 additions
and
67 deletions.
There are no files selected for viewing
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
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,4 @@ | ||
# Getting Started with OpenTelemetry .NET Metrics in 5 Minutes | ||
|
||
* [Getting started with Counter](.\getting-started\README.md) | ||
* [Getting started with Histogram](.\getting-started-histogram\README.md) |
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,55 @@ | ||
// <copyright file="Program.cs" company="OpenTelemetry Authors"> | ||
// 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 | ||
// | ||
// http://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. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
|
||
public class Program | ||
{ | ||
private static readonly Meter MyMeter = new Meter("TestMeter", "0.0.1"); | ||
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("histogram"); | ||
private static readonly Random RandomGenerator = new Random(); | ||
|
||
public static async Task Main(string[] args) | ||
{ | ||
using var meterProvider = Sdk.CreateMeterProviderBuilder() | ||
.AddSource("TestMeter") | ||
.AddConsoleExporter() | ||
.Build(); | ||
|
||
using var token = new CancellationTokenSource(); | ||
Task writeMetricTask = new Task(() => | ||
{ | ||
while (!token.IsCancellationRequested) | ||
{ | ||
MyHistogram.Record( | ||
RandomGenerator.Next(1, 1000), | ||
new KeyValuePair<string, object>("tag1", "value1"), | ||
new KeyValuePair<string, object>("tag2", "value2")); | ||
Task.Delay(10).Wait(); | ||
} | ||
}); | ||
writeMetricTask.Start(); | ||
|
||
token.CancelAfter(10000); | ||
await writeMetricTask; | ||
} | ||
} |
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,67 @@ | ||
# Getting Started with OpenTelemetry .NET in 5 Minutes | ||
|
||
First, download and install the [.NET Core | ||
SDK](https://dotnet.microsoft.com/download) on your computer. | ||
|
||
Create a new console application and run it: | ||
|
||
```sh | ||
dotnet new console --output getting-started-histogram | ||
cd getting-started | ||
dotnet run | ||
``` | ||
|
||
You should see the following output: | ||
|
||
```text | ||
Hello World! | ||
``` | ||
|
||
Install the | ||
[OpenTelemetry.Exporter.Console](../../../src/OpenTelemetry.Exporter.Console/README.md) | ||
package: | ||
|
||
```sh | ||
dotnet add package OpenTelemetry.Exporter.Console | ||
``` | ||
|
||
Update the `Program.cs` file with the code from [Program.cs](./Program.cs): | ||
|
||
Run the application again (using `dotnet run`) and you should see the metric | ||
output from the console, similar to shown below: | ||
|
||
<!-- markdownlint-disable MD013 --> | ||
```text | ||
Export 14:30:58.201 14:30:59.177 histogram [tag1=value1;tag2=value2] Histogram, Meter: TestMeter/0.0.1 | ||
Value: Sum: 33862 Count: 62 | ||
(-? - 0) : 0 | ||
(0 - 5) : 0 | ||
(5 - 10) : 0 | ||
(10 - 25) : 2 | ||
(25 - 50) : 0 | ||
(50 - 75) : 1 | ||
(75 - 100) : 1 | ||
(100 - 250) : 6 | ||
(250 - 500) : 18 | ||
(500 - 1000) : 34 | ||
(1000 - ?) : 0 | ||
``` | ||
<!-- markdownlint-enable MD013 --> | ||
|
||
Congratulations! You are now collecting histogram metrics using OpenTelemetry. | ||
|
||
What does the above program do? | ||
|
||
The program creates a | ||
[Meter](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#meter) | ||
instance named "TestMeter" and then creates a | ||
[Histogram](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#histogram) | ||
instrument from it. This histogram is used to repeatedly report random metric | ||
measurements until exited after 10 seconds. | ||
|
||
An OpenTelemetry | ||
[MeterProvider](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#meterprovider) | ||
is configured to subscribe to instruments from the Meter `TestMeter`, and | ||
aggregate the measurements in-memory. The pre-aggregated metrics are exported | ||
every 1 second to a `ConsoleExporter`. `ConsoleExporter` simply displays it on | ||
the console. |
6 changes: 6 additions & 0 deletions
6
docs/metrics/getting-started-histogram/getting-started-histogram.csproj
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,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" /> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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
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
70 changes: 70 additions & 0 deletions
70
src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetric.cs
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,70 @@ | ||
// <copyright file="HistogramMetric.cs" company="OpenTelemetry Authors"> | ||
// 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 | ||
// | ||
// http://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. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
internal class HistogramMetric : IHistogramMetric | ||
{ | ||
internal HistogramMetric(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, int bucketCount) | ||
{ | ||
this.Name = name; | ||
this.Description = description; | ||
this.Unit = unit; | ||
this.Meter = meter; | ||
this.StartTimeExclusive = startTimeExclusive; | ||
this.Attributes = attributes; | ||
this.MetricType = MetricType.Histogram; | ||
this.BucketsArray = new HistogramBucket[bucketCount]; | ||
} | ||
|
||
public string Name { get; private set; } | ||
|
||
public string Description { get; private set; } | ||
|
||
public string Unit { get; private set; } | ||
|
||
public Meter Meter { get; private set; } | ||
|
||
public DateTimeOffset StartTimeExclusive { get; internal set; } | ||
|
||
public DateTimeOffset EndTimeInclusive { get; internal set; } | ||
|
||
public KeyValuePair<string, object>[] Attributes { get; private set; } | ||
|
||
public bool IsDeltaTemporality { get; internal set; } | ||
|
||
public IEnumerable<IExemplar> Exemplars { get; private set; } = new List<IExemplar>(); | ||
|
||
public long PopulationCount { get; internal set; } | ||
|
||
public double PopulationSum { get; internal set; } | ||
|
||
public IEnumerable<HistogramBucket> Buckets => this.BucketsArray; | ||
|
||
public MetricType MetricType { get; private set; } | ||
|
||
internal HistogramBucket[] BucketsArray { get; set; } | ||
|
||
public string ToDisplayString() | ||
{ | ||
return $"Count={this.PopulationCount},Sum={this.PopulationSum}"; | ||
} | ||
} | ||
} |
Oops, something went wrong.