Skip to content

Commit

Permalink
Provide MeterProvider to replace MeterFactory to be in sync with spec. (
Browse files Browse the repository at this point in the history
#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
cijothomas and reyang authored Jul 24, 2020
1 parent e416063 commit 920b0ed
Show file tree
Hide file tree
Showing 47 changed files with 316 additions and 169 deletions.
5 changes: 3 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Update the `Program.cs` file with the following code:

```csharp
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;

class Program
Expand All @@ -38,7 +39,7 @@ class Program

static void Main()
{
using var otel = OpenTelemetrySdk.CreateTracerProvider(b => b
using var otel = Sdk.CreateTracerProvider(b => b
.AddActivitySource("MyCompany.MyProduct.MyLibrary")
.UseConsoleExporter());

Expand Down Expand Up @@ -74,7 +75,7 @@ Tracer](https://github.com/open-telemetry/opentelemetry-specification/blob/maste
The activitysource instance is used to start an `Activity` which represent
[OpenTelemetry
Span](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#span).
`OpenTelemetrySdk.CreateTracerProvider` sets up the OpenTelemetry Sdk, and
`Sdk.CreateTracerProvider` sets up the OpenTelemetry Sdk, and
configures it to subscribe to the activities from the source
`MyCompany.MyProduct.MyLibrary`, and export it to `ConsoleExporter`, which
simply displays it on the console.
3 changes: 2 additions & 1 deletion examples/AspNet/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using OpenTelemetry;
using OpenTelemetry.Trace;

namespace Examples.AspNet
Expand All @@ -13,7 +14,7 @@ public class WebApiApplication : HttpApplication

protected void Application_Start()
{
this.openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
this.openTelemetry = Sdk.CreateTracerProvider(
(builder) => builder
.AddHttpInstrumentation()
.AddAspNetInstrumentation()
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand All @@ -29,7 +30,7 @@ internal static object Run(ConsoleOptions options)
{
// Enable TracerProvider for the source "MyCompany.MyProduct.MyWebServer"
// and use a single pipeline with a custom MyProcessor, and Console exporter.
using var tracerProvider = OpenTelemetrySdk.CreateTracerProvider(
using var tracerProvider = Sdk.CreateTracerProvider(
(builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.UseConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson,
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>
using System.Diagnostics;
using System.Net.Http;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand All @@ -26,7 +27,7 @@ internal static object Run()
{
System.Console.WriteLine("Hello World!");

using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
(builder) => builder.AddHttpClientInstrumentation()
.SetResource(Resources.CreateServiceResource("http-service-example"))
.AddActivitySource("http-client-test")
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestJaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>
using System;

using OpenTelemetry;
using OpenTelemetry.Trace;

namespace Examples.Console
Expand All @@ -30,7 +31,7 @@ internal static object RunWithActivity(string host, int port)
{
// Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
// and use the Jaeger exporter.
using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
builder => builder
.AddActivitySource("Samples.SampleServer")
.AddActivitySource("Samples.SampleClient")
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestOTelShimWithConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand All @@ -25,7 +26,7 @@ internal static object Run(OpenTelemetryShimOptions options)
{
// Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer"
// and use a single pipeline with a custom MyProcessor, and Console exporter.
using var tracerProvider = OpenTelemetrySdk.CreateTracerProvider(
using var tracerProvider = Sdk.CreateTracerProvider(
(builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.UseConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson));
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestOpenTracingWithConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Resources;
using OpenTelemetry.Shims.OpenTracing;
Expand All @@ -28,7 +29,7 @@ internal static object Run(OpenTracingShimOptions options)
{
// Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer"
// and use Console exporter.
using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
(builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.UseConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson));
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestOtlpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using OpenTelemetry;
using OpenTelemetry.Trace;

namespace Examples.Console
Expand All @@ -29,7 +30,7 @@ private static object RunWithActivitySource(string endpoint)
{
// Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
// and use OTLP exporter.
using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
builder => builder
.AddActivitySource("Samples.SampleServer")
.AddActivitySource("Samples.SampleClient")
Expand Down
28 changes: 17 additions & 11 deletions examples/Console/TestPrometheusExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Exporter.Prometheus;
using OpenTelemetry.Metrics;
using OpenTelemetry.Metrics.Export;
Expand Down Expand Up @@ -54,17 +55,22 @@ internal static async Task<object> RunAsync(int port, int pushIntervalInSecs, in
// Create Processor (called Batcher in Metric spec, this is still not decided)
var processor = new UngroupedBatcher();

// MeterFactory is from where one can obtain Meters.
// All meters from this factory will be configured with the common processor.
var meterFactory = MeterFactory.Create(mb =>
{
mb.SetMetricProcessor(processor);
mb.SetMetricExporter(promExporter);
mb.SetMetricPushInterval(TimeSpan.FromSeconds(pushIntervalInSecs));
});

// Obtain a Meter. Libraries would pass their name as argument.
var meter = meterFactory.GetMeter("MyMeter");
// Application which decides to enable OpenTelemetry metrics
// would setup a MeterProvider and make it default.
// All meters from this factory will be configured with the common processing pipeline.
MeterProvider.SetDefault(Sdk.CreateMeterProvider(mb =>
{
mb.SetMetricProcessor(processor);
mb.SetMetricExporter(promExporter);
mb.SetMetricPushInterval(TimeSpan.FromSeconds(pushIntervalInSecs));
}));

// The following shows how libraries would obtain a MeterProvider.
// MeterProvider is the entry point, which provides Meter.
// If user did not set the Default MeterProvider (shown in earlier lines),
// all metric operations become no-ops.
var meterProvider = MeterProvider.Default;
var meter = meterProvider.GetMeter("MyMeter");

// the rest is purely from Metrics API.
var testCounter = meter.CreateInt64Counter("MyCounter");
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using OpenTelemetry;
using OpenTelemetry.Trace;
using StackExchange.Redis;

Expand All @@ -41,7 +42,7 @@ internal static object Run(string zipkinUri)
var connection = ConnectionMultiplexer.Connect("localhost");

// Configure exporter to export traces to Zipkin
using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
builder => builder
.UseZipkinExporter(o =>
{
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestZPagesExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Diagnostics;
using System.Threading;
using OpenTelemetry;
using OpenTelemetry.Exporter.ZPages;
using OpenTelemetry.Trace;

Expand All @@ -33,7 +34,7 @@ internal static object Run()
// Start the server
httpServer.Start();

using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
builder => builder
.AddActivitySource("zpages-test")
.UseZPagesExporter(
Expand Down
3 changes: 2 additions & 1 deletion examples/Console/TestZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using OpenTelemetry;
using OpenTelemetry.Trace;

namespace Examples.Console
Expand All @@ -32,7 +33,7 @@ internal static object Run(string zipkinUri)

// Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
// and use the Zipkin exporter.
using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(
using var openTelemetry = Sdk.CreateTracerProvider(
builder => builder
.AddActivitySource("Samples.SampleServer")
.AddActivitySource("Samples.SampleClient")
Expand Down
98 changes: 98 additions & 0 deletions src/OpenTelemetry.Api/Metrics/MeterProvider.cs
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using OpenTelemetry;
using OpenTelemetry.Extensions.Hosting.Implementation;
using OpenTelemetry.Trace;

Expand Down Expand Up @@ -46,7 +47,7 @@ public static IServiceCollection AddOpenTelemetry(this IServiceCollection servic
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services, Action<TracerProviderBuilder> configure)
{
services.AddOpenTelemetry(() => OpenTelemetrySdk.CreateTracerProvider(configure));
services.AddOpenTelemetry(() => Sdk.CreateTracerProvider(configure));
return services;
}

Expand All @@ -58,7 +59,7 @@ public static IServiceCollection AddOpenTelemetry(this IServiceCollection servic
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services, Action<IServiceProvider, TracerProviderBuilder> configure)
{
services.AddOpenTelemetry(s => OpenTelemetrySdk.CreateTracerProvider(builder => configure(s, builder)));
services.AddOpenTelemetry(s => Sdk.CreateTracerProvider(builder => configure(s, builder)));
return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dotnet add package OpenTelemetry.Instrumentation.StackExchangeRedis
using var connection = ConnectionMultiplexer.Connect("localhost:6379");

// Pass the connection to AddRedisInstrumentation.
using var openTelemetry = OpenTelemetrySdk.CreateTracerProvider(b => b
using var openTelemetry = Sdk.CreateTracerProvider(b => b
.AddRedisInstrumentation(connection)
.UseZipkinExporter()
.SetResource(Resources.CreateServiceResource("my-service"));
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/Export/PushMetricController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Internal;
using static OpenTelemetry.Metrics.MeterFactory;
using static OpenTelemetry.Metrics.MeterProviderSdk;

namespace OpenTelemetry.Metrics.Export
{
Expand Down
Loading

0 comments on commit 920b0ed

Please sign in to comment.