-
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.
Provide MeterProvider to replace MeterFactory to be in sync with spec. (
#919) * Provide MeterProvider to replace MeterFactory to be in sync with spec. * move OpenTelemetrymetrics to metrics folder * Update examples/Console/TestPrometheusExporter.cs Co-authored-by: Reiley Yang <[email protected]> * Sdk.CreateTracerProvider CreateMeterProvider * move Sdk to root * makr Sdk static Co-authored-by: Reiley Yang <[email protected]>
- Loading branch information
1 parent
e416063
commit 920b0ed
Showing
47 changed files
with
316 additions
and
169 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
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
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
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
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,98 @@ | ||
// <copyright file="MeterProvider.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; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// MeterProvider is the entry point of the OpenTelemetry Metrics API. It provides access to Meters. | ||
/// </summary> | ||
public class MeterProvider : IDisposable | ||
{ | ||
private static ProxyMeter proxyMeter = new ProxyMeter(); | ||
private static bool isInitialized; | ||
private static MeterProvider defaultProvider = new MeterProvider(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MeterProvider"/> class. | ||
/// </summary> | ||
protected MeterProvider() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the dafult instance of a <see cref="MeterProvider"/>. | ||
/// </summary> | ||
public static MeterProvider Default | ||
{ | ||
get => defaultProvider; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the default instance of <see cref="MeterProvider"/>. | ||
/// </summary> | ||
/// <param name="meterProvider">Instance of <see cref="MeterProvider"/>.</param> | ||
/// <remarks> | ||
/// This method can only be called once. Calling it multiple times will throw an <see cref="System.InvalidOperationException"/>. | ||
/// </remarks> | ||
/// <exception cref="InvalidOperationException">Thrown when called multiple times.</exception> | ||
public static void SetDefault(MeterProvider meterProvider) | ||
{ | ||
if (isInitialized) | ||
{ | ||
throw new InvalidOperationException("Default factory is already set"); | ||
} | ||
|
||
defaultProvider = meterProvider ?? throw new ArgumentNullException(nameof(meterProvider)); | ||
|
||
// some libraries might have already used and cached ProxyMeter. | ||
// let's update it to real one and forward all calls. | ||
|
||
// TODO: | ||
// resource assignment is not possible for libraries that cache meter before SDK is initialized. | ||
// SDK (Meter) must be at least partially initialized before any collection starts to capture resources. | ||
// we might be able to work this around in future. | ||
proxyMeter.UpdateMeter(defaultProvider.GetMeter(null)); | ||
|
||
isInitialized = true; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual void Dispose() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Returns a Meter for a given name and version. | ||
/// </summary> | ||
/// <param name="name">Name of the instrumentation library.</param> | ||
/// <param name="version">Version of the instrumentation library (optional).</param> | ||
/// <returns>Meter for the given name and version information.</returns> | ||
public virtual Meter GetMeter(string name, string version = null) | ||
{ | ||
return isInitialized ? defaultProvider.GetMeter(name, version) : proxyMeter; | ||
} | ||
|
||
// for tests | ||
internal void Reset() | ||
{ | ||
proxyMeter = new ProxyMeter(); | ||
isInitialized = false; | ||
defaultProvider = new MeterProvider(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.