英文:
Is there a way to instrument metrics/tracing in a .NET library for both Prometheus AND application insights
问题
我正在为我的当前公司构建内部库,并且我想为Prometheus和Application Insights同时实施度量和跟踪。
度量将包括仪表、直方图和计数器。
我还需要进行跟踪。
我理解system.diagnostics.activity
和system.diagnostics.metrics
可以让我支持prometheus-net
和OpenTelemetry
,但不能支持Application Insights。
我是否遗漏了某种方式可以使这3个收集器在我的库中可以直接工作?
我真的不想要分开调用度量/跟踪库。
虽然这个示例不是最佳的,但希望你们能理解我的意思。
英文:
I am building internal libraries for my current company and I want to instrument metrics and tracing for both Prometheus and Application insights.
The metrics will consist of gauges, histograms, and counters.
I also have a need for tracing.
It's my understanding that system.diagnostics.activity
and system.diagnostics.metrics
would allow me to support prometheus-net
and OpenTelemetry
, but not application insights.
Am I missing some way allowing all 3 collectors to work out of the box with my library?
I'd really hate to have to have 2 separate calls to metrics/tracing libraries.
Contrived Example:
public class CookieService{
private readonly ICookieRepository _cookieRepository;
public CookieService(ICookieRepository cookieRepository){
_cookieRepository = cookieRepository;
}
public async Task AddCookies(CookieDto dto){
// Track cookie flavor
// Start span
// Track timing
_cookieRepository.Add(dto);
// End timing
// End span
}
}
Not the greatest example, but hopefully you all will get the idea
答案1
得分: 1
The .NET OpenTelemetry SDK具有用于Azure Monitor(Application Insights现在是其中的一部分)和Prometheus的导出器。
安装这些包:
在您的WithTracing
块中,设置Azure Monitor导出器:
.AddAzureMonitorTraceExporter(o => o.ConnectionString = "InstrumentationKey=your_key")
在您的WithMetrics
块中,设置Azure Monitor和Prometheus导出器:
.AddAzureMonitorMetricExporter(o => o.ConnectionString = "InstrumentationKey=your_key")
.AddPrometheusExporter()
Prometheus导出器将创建一个/metrics
路由以正常方式抓取。
另一种解决方案是完全采用OpenTelemetry Collector。这具有Azure Monitor和Prometheus远程写入导出器,可以用来抓取指标和跟踪信息的任何其他代理。
如果您选择此方法,您只需要OTLP导出器:
它的设置非常简单:
// 端点默认为localhost:4317
.AddOtlpExporter(config => config.Endpoint = new Uri("http://host:port"))
以下是一个导出到两个目标的Collector配置,请确保替换所有大写字母的占位符为实际值:
extensions:
basicauth/metrics:
client_auth:
username: METRICS_USER_ID
password: API_KEY
receivers:
otlp:
protocols:
grpc:
http:
processors:
batch:
timeout: 1s
send_batch_size: 1024
exporters:
logging:
loglevel: debug
prometheusremotewrite:
endpoint: https://PROMETHEUS_URL
auth:
authenticator: basicauth/metrics
azuremonitor:
instrumentation_key: INSTRUMENTATION_KEY
service:
extensions: [basicauth/metrics]
pipelines:
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheusremotewrite]
traces:
receivers: [otlp]
processors: [batch]
exporters: [azuremonitor]
英文:
The .NET OpenTelemetry SDK has exporters for Azure Monitor (Application Insights is a part of this now) and Prometheus.
Install these packages:
In your WithTracing
block, setup the Azure Monitor exporter:
.AddAzureMonitorTraceExporter(o => o.ConnectionString = "InstrumentationKey=your_key")
In your WithMetrics
block, setup the Azure Monitor and Prometheus exporters:
.AddAzureMonitorMetricExporter(o => o.ConnectionString = "InstrumentationKey=your_key")
.AddPrometheusExporter()
The Prometheus exporter will create a /metrics
route to scrape like normal.
An alternative solution is to fully adopt the OpenTelemetry Collector. This has Azure Monitor and Prometheus remote write exporters and would any other agents you're running to scrape metrics and traces.
If you go this route, you only need the OTLP exporter:
It's setup is very simple:
// Endpoint defaults to localhost:4317
.AddOtlpExporter(config => config.Endpoint = new Uri("http://host:port"))
Here is a Collector config that exports to both destinations, be sure to swap out the real values for the placeholders in all caps:
extensions:
basicauth/metrics:
client_auth:
username: METRICS_USER_ID
password: API_KEY
receivers:
otlp:
protocols:
grpc:
http:
processors:
batch:
timeout: 1s
send_batch_size: 1024
exporters:
logging:
loglevel: debug
prometheusremotewrite:
endpoint: https://PROMETHEUS_URL
auth:
authenticator: basicauth/metrics
azuremonitor:
instrumentation_key: INSTRUMENTATION_KEY
service:
extensions: [basicauth/metrics]
pipelines:
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheusremotewrite]
traces:
receivers: [otlp]
processors: [batch]
exporters: [azuremonitor]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论