Skip to content

Commit

Permalink
Implement the metrics dispose/shutdown logic (#2404)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Sep 23, 2021
1 parent 256fc2d commit 78baf7c
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 51 deletions.
2 changes: 1 addition & 1 deletion OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions docs/metrics/README.md

This file was deleted.

3 changes: 2 additions & 1 deletion docs/metrics/extending-the-sdk/MyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ protected override bool ProcessMetrics(Batch<Metric> metrics, int timeoutMillise
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public static void Main(string[] args)
.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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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 -->

Expand All @@ -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`.
19 changes: 18 additions & 1 deletion src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using System.Diagnostics;
using System.Threading;

namespace OpenTelemetry.Metrics
{
Expand Down Expand Up @@ -97,7 +99,22 @@ protected override bool OnCollect(int timeoutMilliseconds)
/// <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/>
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/MetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected virtual bool OnCollect(int timeoutMilliseconds)
/// </remarks>
protected virtual bool OnShutdown(int timeoutMilliseconds)
{
return true;
return this.Collect(timeoutMilliseconds);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ void ProcessExport(Batch<Metric> batch)
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ void ProcessExport(Batch<Metric> batch)
}
}

// Invokes the TestExporter which will invoke ProcessExport
metricReader.Collect();

meterProvider.Dispose();

var requestMetrics = metricItems
Expand Down

0 comments on commit 78baf7c

Please sign in to comment.