英文:
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 =>
{
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"; });
});
Using them later:
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 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
上的文档注释如下所示:
创建一个用于构建
MeterProvider
的MeterProviderBuilder
。在典型的应用程序中,应该在应用程序启动时创建一个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 MetricProvider
s, like so:
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");
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论