gpt4 book ai didi

ASP.NET Core 7: using OpenTelemetry to send traces/metrics to Grafana(ASP.NET Core 7:使用开放遥测向Grafana发送跟踪/度量)

转载 作者:bug小助手 更新时间:2023-10-25 11:42:49 30 4
gpt4 key购买 nike



I'm trying to send metrics/traces from my ASP.NET Core 7 application to Grafana.

我正试图从我的ASP.NET Core 7应用程序向Grafana发送度量/跟踪。


Here's my Docker Compose file.

这是我的《码头工人》作文文件。


version: '3.4'

networks:
my-network:
name: my_network

services:
sample.restaurant.server:
image: ${DOCKER_REGISTRY-}samplerestaurantserver
build:
context: .
dockerfile: sample/Sample.Restaurant.Server/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- OTEL_EXPORTER_OTLP_ENDPOINT=grpc://otel-collector:4317
- OTEL_METRICS_EXPORTER=otlp
- OTEL_RESOURCE_ATTRIBUTES=service.name=Sample.Restaurant.Server
ports:
- 54576:80
- 54577:443
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
depends_on:
- database
- otel-collector
networks:
- my-network
otel-collector:
image: otel/opentelemetry-collector
ports:
- 4317:4317
volumes:
- ./collector-config.yml:/etc/otel-collector/collector-config.yml
networks:
- my-network
grafana:
image: grafana/grafana
restart: unless-stopped
ports:
- 3000:3000
volumes:
- grafana_data:/var/lib/grafanaa
depends_on:
- otel-collector
networks:
- my-network

volumes:
grafana_data: {}

And here's my collector config:

下面是我的收集器配置:


receivers:
otlp:
protocols:
grpc:

exporters:
otlp:
endpoint: otel-collector:4317

service:
pipelines:
traces:
receivers: [otlp]
exporters: [otlp]
metrics:
receivers: [otlp]
exporters: [otlp]


And here's my service collection extension method that configures OpenTelemetry.

下面是配置OpenTelemeter的服务集合扩展方法。


public static class TelemetryConfiguration
{
public static IServiceCollection AddTelemetry(this IServiceCollection services,
IWebHostEnvironment webHostEnvironment,
string telemetryNamespace)
{
services.AddOpenTelemetry()
.ConfigureResource(resource =>
{
resource
.AddService(
serviceName: webHostEnvironment.ApplicationName,
serviceNamespace: telemetryNamespace,
serviceVersion: Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "1.0.0",
serviceInstanceId: Environment.MachineName)
.AddAttributes(new Dictionary<string, object>
{
{ "deployment.environment", webHostEnvironment.EnvironmentName }
});
})
.WithMetrics(metrics =>
{
metrics
.AddAspNetCoreInstrumentation()
.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.AddOtlpExporter(builder => builder.Endpoint = new Uri("http://localhost:4317"));
})
.WithTracing(tracing =>
{
tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(builder => builder.Endpoint = new Uri("http://localhost:4317"));
});

return services;
}
}

Firstly, is this correctly configured?

首先,这是否配置正确?


Secondly, how do I setup the data source in Grafana to receive the data from my collector?

其次,如何在Grafana中设置数据源以从我的收集器接收数据?


更多回答

I think your config will not work: you are trying to use localhost, while connecting between different containers, this is incorrect. If I understand what you are trying to do correctly, you should use new Uri("http://otel-collector:4317") instead.

我认为您的配置不会起作用:您试图在不同容器之间连接时使用本地主机,这是不正确的。如果我理解您的意图是正确的,那么您应该使用新的URI(“http://otel-collector:4317”)。

And regarding Grafana, AFAIK, otel is not a proper data source - it is mere universal processor of metrics, logs and such, but it doesn't store those. You need something to store data, that will be used by Grafana for visualization. This might be Prometheus, for example. Please look at this demo architecture.

至于Grafana,AFAIK,OTEL不是一个合适的数据源--它仅仅是指标、日志等的通用处理器,但它不存储这些。你需要一些东西来存储数据,Grafana将使用它来进行可视化。例如,这可能是普罗米修斯。请看这个演示架构。

优秀答案推荐

The issue here is that you're attempting to use the OpenTelemetry collector as a Datastore. It collects, processes and forwards data to datastores, so in your example, you'll need to add a series of datastores for the various signals.

这里的问题是,您正在尝试将OpenTelemeter收集器用作数据存储。它收集、处理数据并将其转发到数据存储,因此在您的示例中,您需要为各种信号添加一系列数据存储。


My recommendation would be:

我的建议是:



  • Traces - Jaeger

  • Metrics - Prometheus

  • Logs - OpenSearch


If you want to, you can then use the free local version of Grafana to visualise things like Metrics.

如果你愿意,你可以使用Grafana的免费本地版本来可视化像Metrics这样的东西。


For production, I'd highly recommend thinking about a SaaS solution as scaling of those can be hard if you're not used to running lots of containers and datastores. That said, all of those OSS/Free solutions can be hosted and scaled if you choose to.

对于生产,我强烈建议考虑SaaS解决方案,因为如果您不习惯运行大量容器和数据存储,扩展这些解决方案可能会很困难。也就是说,如果您选择的话,所有这些OSS/Free解决方案都可以托管和扩展。



If you're open to using Grafana Cloud for storing your data, it's quite simple to ingest OTLP. Here's the related documentation.

如果您愿意使用Grafana Cloud存储您的数据,那么使用OTLP非常简单。以下是相关文档。


You would need to sign up for Grafana Cloud (there's a free tier) and change your collector config according to the template below:

您需要注册Grafana Cloud(有一个免费级别),并根据以下模板更改您的收集器配置:


extensions:
basicauth/otlp:
client_auth:
username: <instanceID>
password: <Cloud Access Policy token>

exporters:
otlphttp:
auth:
authenticator: basicauth/otlp
endpoint: https://otlp-gateway-<zone>.grafana.net/otlp

service:
extensions: [basicauth/otlp]
pipelines:
metrics:
receivers: [...]
processors: [...]
exporters: [..., otlphttp]
traces:
receivers: [...]
processors: [...]
exporters: [..., otlphttp]
logs:
receivers: [...]
processors: [...]
exporters: [..., otlphttp]

更多回答

No, trying to use the Grafana container within Docker Compose run locally on my laptop

否,尝试使用我笔记本电脑上本地运行的Docker Compose中的Grafana容器

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com