Implement the metrics dispose/shutdown logic (#2404)

This commit is contained in:
Reiley Yang 2021-09-23 08:02:09 -07:00 committed by GitHub
parent 256fc2d2fe
commit 78baf7c261
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 41 additions and 51 deletions

View File

@ -218,7 +218,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-observable-
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-observable-counter", "docs\metrics\getting-started-observable-counter\getting-started-observable-counter.csproj", "{43005998-0247-4620-AF88-27DACD48712E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-counter", "docs\metrics\getting-started-counter\getting-started-counter.csproj", "{EA60B549-F712-4ABE-8E44-FCA83B78C06E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started", "docs\metrics\getting-started\getting-started.csproj", "{EA60B549-F712-4ABE-8E44-FCA83B78C06E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "extending-the-sdk", "docs\metrics\extending-the-sdk\extending-the-sdk.csproj", "{1F9D7748-D099-4E25-97F5-9C969D6FF969}"
EndProject

View File

@ -24,7 +24,7 @@ Any exceptions to this are noted in the individual `README.md` files.
If you are new here, please read the getting started docs:
* [logs](./docs/logs/getting-started/README.md)
* [metrics](./docs/metrics/README.md) (experimental)
* [metrics](./docs/metrics/getting-started/README.md) (experimental)
* [trace](./docs/trace/getting-started/README.md)
This repository includes multiple installable components, available on

View File

@ -1,6 +0,0 @@
# Getting Started with OpenTelemetry .NET Metrics in 5 Minutes
* [Getting started with Counter](./getting-started-counter/README.md)
* [Getting started with Observable Counter](./getting-started-observable-counter/README.md)
* [Getting started with Observable Gauge](./getting-started-gauge/README.md)
* [Getting started with Histogram](./getting-started-histogram/README.md)

View File

@ -36,11 +36,12 @@ internal class MyReader : MetricReader
protected override bool OnShutdown(int timeoutMilliseconds)
{
Console.WriteLine($"{this.name}.OnShutdown(timeoutMilliseconds={timeoutMilliseconds})");
return true;
return base.OnShutdown(timeoutMilliseconds);
}
protected override void Dispose(bool disposing)
{
Console.WriteLine($"{this.name}.Dispose({disposing})");
base.Dispose(disposing);
}
}

View File

@ -29,11 +29,13 @@ public class Program
.AddConsoleExporter()
.Build();
var counter = MyMeter.CreateCounter<long>("MyCounter");
var counter = MyMeter.CreateCounter<long>("MyFruitCounter");
for (int i = 0; i < 20000000; i++)
{
counter.Add(1, new("tag1", "value1"), new("tag2", "value2"));
}
counter.Add(1, new("name", "apple"), new("color", "red"));
counter.Add(2, new("name", "lemon"), new("color", "yellow"));
counter.Add(1, new("name", "lemon"), new("color", "yellow"));
counter.Add(2, new("name", "apple"), new("color", "green"));
counter.Add(5, new("name", "apple"), new("color", "red"));
counter.Add(4, new("name", "lemon"), new("color", "yellow"));
}
}

View File

@ -6,8 +6,8 @@ SDK](https://dotnet.microsoft.com/download) on your computer.
Create a new console application and run it:
```sh
dotnet new console --output getting-started-counter
cd getting-started-counter
dotnet new console --output getting-started
cd getting-started
dotnet run
```
@ -32,29 +32,13 @@ output from the console, similar to shown below:
<!-- markdownlint-disable MD013 -->
```text
Export MyCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-03T04:29:37.1096398Z, 2021-09-03T04:29:38.0649233Z] tag1:value1tag2:value2 LongSum
Value: 460
Export MyCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-03T04:29:38.0649233Z, 2021-09-03T04:29:39.1483639Z] tag1:value1tag2:value2 LongSum
Value: 640
Export MyCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-03T04:29:39.1483639Z, 2021-09-03T04:29:40.1679696Z] tag1:value1tag2:value2 LongSum
Value: 420
Export MyCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-03T04:29:40.1679696Z, 2021-09-03T04:29:41.1774527Z] tag1:value1tag2:value2 LongSum
Value: 560
Export MyCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-03T04:29:41.1774527Z, 2021-09-03T04:29:42.1791523Z] tag1:value1tag2:value2 LongSum
Value: 650
Export MyCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-03T04:29:42.1791523Z, 2021-09-03T04:29:43.1875033Z] tag1:value1tag2:value2 LongSum
Value: 620
Export MyFruitCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
2021-09-23T03:17:30.6198292Z, 2021-09-23T03:17:30.6356517Z] color:redname:apple LongSum
Value: 6
2021-09-23T03:17:30.6198292Z, 2021-09-23T03:17:30.6356517Z] color:yellowname:lemon LongSum
Value: 7
2021-09-23T03:17:30.6198292Z, 2021-09-23T03:17:30.6356517Z] color:greenname:apple LongSum
Value: 2
```
<!-- markdownlint-enable MD013 -->
@ -66,12 +50,10 @@ The program creates a
[Meter](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#meter)
instance named "MyCompany.MyProduct.MyLibrary" and then creates a
[Counter](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#counter)
instrument from it. This counter is used to repeatedly report metric
measurements until it reaches a certain number of loops.
instrument from it. This counter is used to report several metric measurements.
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
`MyCompany.MyProduct.MyLibrary`, 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.
pre-aggregated metrics are exported to a `ConsoleExporter`.

View File

@ -15,6 +15,8 @@
// </copyright>
using System;
using System.Diagnostics;
using System.Threading;
namespace OpenTelemetry.Metrics
{
@ -97,7 +99,22 @@ namespace OpenTelemetry.Metrics
/// <inheritdoc />
protected override bool OnShutdown(int timeoutMilliseconds)
{
return this.exporter.Shutdown(timeoutMilliseconds);
var result = true;
if (timeoutMilliseconds == Timeout.Infinite)
{
result = this.Collect(Timeout.Infinite) && result;
result = this.exporter.Shutdown(Timeout.Infinite) && result;
}
else
{
var sw = Stopwatch.StartNew();
result = this.Collect(timeoutMilliseconds) && result;
var timeout = timeoutMilliseconds - sw.ElapsedMilliseconds;
result = this.exporter.Shutdown((int)Math.Max(timeout, 0)) && result;
}
return result;
}
/// <inheritdoc/>

View File

@ -211,7 +211,7 @@ namespace OpenTelemetry.Metrics
/// </remarks>
protected virtual bool OnShutdown(int timeoutMilliseconds)
{
return true;
return this.Collect(timeoutMilliseconds);
}
/// <summary>

View File

@ -84,9 +84,6 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
// giving some breezing room for the End callback to complete
await Task.Delay(TimeSpan.FromSeconds(1));
// Invokes the TestExporter which will invoke ProcessExport
metricReader.Collect();
this.meterProvider.Dispose();
var requestMetrics = metricItems

View File

@ -112,9 +112,6 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
}
}
// Invokes the TestExporter which will invoke ProcessExport
metricReader.Collect();
meterProvider.Dispose();
var requestMetrics = metricItems