-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor MeterProvider to be similar to TracerProvider (#2141)
- Loading branch information
1 parent
9a5d9fd
commit 63c28f0
Showing
26 changed files
with
372 additions
and
343 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
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 |
---|---|---|
@@ -1,5 +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
43 changes: 43 additions & 0 deletions
43
src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricHelperExtensions.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,43 @@ | ||
// <copyright file="ConsoleExporterMetricHelperExtensions.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 OpenTelemetry.Exporter; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
public static class ConsoleExporterMetricHelperExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Console exporter to the TracerProvider. | ||
/// </summary> | ||
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param> | ||
/// <param name="configure">Exporter configuration options.</param> | ||
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns> | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")] | ||
public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action<ConsoleExporterOptions> configure = null) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
var options = new ConsoleExporterOptions(); | ||
configure?.Invoke(options); | ||
return builder.AddMetricProcessor(new PushMetricProcessor(new ConsoleMetricExporter(options), options.MetricExportInterval)); | ||
} | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.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,85 @@ | ||
// <copyright file="ConsoleMetricExporter.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.Globalization; | ||
using System.Linq; | ||
using OpenTelemetry.Metrics; | ||
|
||
namespace OpenTelemetry.Exporter | ||
{ | ||
public class ConsoleMetricExporter : ConsoleExporter<MetricItem> | ||
{ | ||
public ConsoleMetricExporter(ConsoleExporterOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public override ExportResult Export(in Batch<MetricItem> batch) | ||
{ | ||
foreach (var metricItem in batch) | ||
{ | ||
foreach (var metric in metricItem.Metrics) | ||
{ | ||
var tags = metric.Attributes.ToArray().Select(k => $"{k.Key}={k.Value?.ToString()}"); | ||
|
||
string valueDisplay = string.Empty; | ||
if (metric is ISumMetric sumMetric) | ||
{ | ||
if (sumMetric.Sum.Value is double doubleSum) | ||
{ | ||
valueDisplay = ((double)doubleSum).ToString(CultureInfo.InvariantCulture); | ||
} | ||
else if (sumMetric.Sum.Value is long longSum) | ||
{ | ||
valueDisplay = ((long)longSum).ToString(); | ||
} | ||
} | ||
else if (metric is IGaugeMetric gaugeMetric) | ||
{ | ||
if (gaugeMetric.LastValue.Value is double doubleValue) | ||
{ | ||
valueDisplay = ((double)doubleValue).ToString(); | ||
} | ||
else if (gaugeMetric.LastValue.Value is long longValue) | ||
{ | ||
valueDisplay = ((long)longValue).ToString(); | ||
} | ||
|
||
// Qn: tags again ? gaugeMetric.LastValue.Tags | ||
} | ||
else if (metric is ISummaryMetric summaryMetric) | ||
{ | ||
valueDisplay = string.Format("Sum: {0} Count: {1}", summaryMetric.PopulationSum, summaryMetric.PopulationCount); | ||
} | ||
else if (metric is IHistogramMetric histogramMetric) | ||
{ | ||
valueDisplay = string.Format("Sum: {0} Count: {1}", histogramMetric.PopulationSum, histogramMetric.PopulationCount); | ||
} | ||
|
||
var kind = metric.GetType().Name; | ||
|
||
string time = $"{metric.StartTimeExclusive.ToLocalTime().ToString("HH:mm:ss.fff")} {metric.EndTimeInclusive.ToLocalTime().ToString("HH:mm:ss.fff")}"; | ||
|
||
var msg = $"Export {time} {metric.Name} [{string.Join(";", tags)}] {kind} Value: {valueDisplay}, Details: {metric.ToDisplayString()}"; | ||
Console.WriteLine(msg); | ||
} | ||
} | ||
|
||
return ExportResult.Success; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.