可以使用不同的导出器同时使用多个 OpenTelemetry 指标提供者吗?

huangapple go评论101阅读模式
英文:

Possible to have multiple OpenTelemetry Metrics Providers with different exporters?

问题

在ASP.NET Core 7中,是否可以拥有多个OpenTelemetry Metrics Providers,其中每个提供不同的Meters并将它们的指标发送到不同的端点?

我想要实现的目标(伪代码):

services.AddOpenTelemetry()     
.WithMetrics(metricsBuilder =>
{
    metricsBuilder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("foobar"));
    metricsBuilder.AddMeter("Meters.Category1.*");
    metricsBuilder.AddGenevaMetricExporter(exporterOptions => { exporterOptions.ConnectionString = "myEndpoint1"; });
})
.WithMetrics(metricsBuilder =>
{
    metricsBuilder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("foobar"));
    metricsBuilder.AddMeter("Meters.Category2.*");
    metricsBuilder.AddGenevaMetricExporter(exporterOptions => { exporterOptions.ConnectionString = "myEndpoint2"; });
});

稍后使用它们:

Meter MyMeter1 = new("Meters.Category1.MyMeter1");
var foo = MyMeter1.CreateHistogram<double>("Foo");


Meter MyMeter2 = new("Meters.Category2.MyMeter2");
var bar = MyMeter2.CreateHistogram<double>("Bar");

这是否可行,或者我应该如何实现这一目标?

英文:

Is it possible (in ASP.NETcore 7) to have multiple OpenTelemetry Metrics Providers, where each provides different Meters and sends their respective metrics to different endpoints?

What I would like to get to (pseudo code):

services.AddOpenTelemetry()     
.WithMetrics(metricsBuilder =&gt;
{
	metricsBuilder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(&quot;foobar&quot;));
	metricsBuilder.AddMeter(&quot;Meters.Category1.*&quot;);
    metricsBuilder.AddGenevaMetricExporter(exporterOptions =&gt; { exporterOptions.ConnectionString = &quot;myEndpoint1&quot;; });
})
.WithMetrics(metricsBuilder =&gt;
{
	metricsBuilder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(&quot;foobar&quot;));
	metricsBuilder.AddMeter(&quot;Meters.Category2.*&quot;);
    metricsBuilder.AddGenevaMetricExporter(exporterOptions =&gt; { exporterOptions.ConnectionString = &quot;myEndpoint2&quot;; });
});

Using them later:

Meter MyMeter1 = new(&quot;Meters.Category1.MyMeter1&quot;);
var foo = MyMeter1.CreateHistogram&lt;double&gt;(&quot;Foo&quot;);


Meter MyMeter2 = new(&quot;Meters.Category2.MyMeter2&quot;);
var bar = MyMeter2.CreateHistogram&lt;double&gt;(&quot;Bar&quot;);

Is this possible or how could I achieve that?

答案1

得分: 2

这似乎无法使用WithMetrics扩展方法实现。有关该方法的文档注释如下:

注意:可以多次调用此方法,也可以由库作者调用。
每个注册的配置操作将按顺序应用。

相反,您需要手动实例化两个独立的MetricProvider,如下所示:

var meterProvider1 = Sdk.CreateMeterProviderBuilder()
    .AddMeter("Meters.Category1.*")
    .AddGenevaMetricExporter(exporterOptions => { exporterOptions.ConnectionString = "myEndpoint1"; })
    .Build();
var meterProvider2 = Sdk.CreateMeterProviderBuilder()
    .AddMeter("Meters.Category2.*")
    .AddGenevaMetricExporter(exporterOptions => { exporterOptions.ConnectionString = "myEndpoint2"; })
    .Build();

Meter MyMeter1 = new Meter("Meters.Category1.MyMeter1");
var foo = MyMeter1.CreateHistogram<double>("Foo");

Meter MyMeter2 = new Meter("Meters.Category2.MyMeter2");
var bar = MyMeter2.CreateHistogram<double>("Bar");

然而,这里存在一个潜在的问题。CreateMeterProviderBuilder上的文档注释如下所示:

创建一个用于构建MeterProviderMeterProviderBuilder。在典型的应用程序中,应该在应用程序启动时创建一个MeterProvider,并在应用程序关闭时释放它。确保不要过早释放提供程序非常重要。

如果手动创建两个MeterProvider实例,您需要解决如何管理它们的生命周期的问题。WithMetrics扩展方法通过与.NET依赖注入框架进行连接来完成此操作。

一个快速的解决方案是将两个MeterProvider实例都注册为单例,如下所示:

services.AddSingleton(meterProvider1);
services.AddSingleton(meterProvider2);

这样应用程序在终止时应该会正确释放这两个实例。

英文:

This does not appear to be possible using the WithMetrics extension method. The doc comments on the method reads:

> Note: This is safe to be called multiple times and by library authors.
> Each registered configuration action will be applied sequentially.

Instead, you would need to manually instantiate two separate MetricProviders, like so:

var meterProvider1 = Sdk.CreateMeterProviderBuilder()
    .AddMeter(&quot;Meters.Category1.*&quot;)
    .AddGenevaMetricExporter(exporterOptions =&gt; { exporterOptions.ConnectionString = &quot;myEndpoint1&quot;; })
    .Build();
var meterProvider2 = Sdk.CreateMeterProviderBuilder()
    .AddMeter(&quot;Meters.Category2.*&quot;)
    .AddGenevaMetricExporter(exporterOptions =&gt; { exporterOptions.ConnectionString = &quot;myEndpoint2&quot;; })
    .Build();

Meter MyMeter1 = new Meter(&quot;Meters.Category1.MyMeter1&quot;);
var foo = MyMeter1.CreateHistogram&lt;double&gt;(&quot;Foo&quot;);

Meter MyMeter2 = new Meter(&quot;Meters.Category2.MyMeter2&quot;);
var bar = MyMeter2.CreateHistogram&lt;double&gt;(&quot;Bar&quot;);

However, there is a potential catch. The doc comments on CreateMeterProviderBuilder state:
> Creates a MeterProviderBuilder which is used to build
> a MeterProvider. In a typical application, a single
> MeterProvider is created at application startup and disposed
> at application shutdown. It is important to ensure that the provider is not
> disposed too early.

If you manually make two instances of MeterProvider you will need to figure out how to manage their lifecycle. The WithMetrics extension method does this by hooking in to the .NET dependency injection framework.

A quick solution is registering both MeterProvider instances as singletons, like so:

services.AddSingleton(meterProvider1);
services.AddSingleton(meterProvider2);

This should properly dispose of both instances when the application is terminated.

huangapple
  • 本文由 发表于 2023年7月10日 22:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定