Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Metrics (OpenTelemetry with Prometheus Export endpoint) #2010

Merged
merged 5 commits into from
Oct 18, 2024

Conversation

Masterjun3
Copy link
Collaborator

Ever since .NET 8.0, there are built-in metrics for both ASP.NET Core and also our PostgreSQL Npgsql.

With the OpenTelemetry nuget package, we collect these metrics. And with the Prometheus Export nuget package we can expose them on an endpoint /Metrics, which I guarded behind the SeeDiagnostics permission.

If you access that endpoint you will see all live metric data in Prometheus format, which is a very big text file of stuff like

db_client_commands_bytes_written_bytes_total{otel_scope_name="Npgsql",otel_scope_version="0.1.0",pool_name="Host=127.0.0.1;Port=5432;Database=TASVideos;Username=postgres;Include Error Detail=True"} 90222 1729267870852
http_server_active_requests{otel_scope_name="Microsoft.AspNetCore.Hosting",http_request_method="GET",url_scheme="https"} 1 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route=""} 2 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="{*url}"} 10 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="Wiki/PageNotFound"} 10 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="/Metrics"} 3 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="Forum"} 3 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="{id:int}M"} 1 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="Forum/Subforum/{id}"} 1 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="Forum/Posts/{id}"} 1 1729267870852
aspnetcore_routing_match_attempts_total{otel_scope_name="Microsoft.AspNetCore.Routing",aspnetcore_routing_is_fallback="false",aspnetcore_routing_match_status="success",http_route="Forum/Topics/{id}"} 1 1729267870852

And that's all this PR does. No data is stored, no data is visualized.


Actually visualizing this data is difficult, because nothing is ever simple.
You need to set up your own Prometheus server that scrapes off this data. This is where data is actually stored: in your own Prometheus instance.
To visualize this data, you need another server like Grafana that queries your Prometheus server and makes cool lines on charts and stuff.
Did I say nothing is ever simple? You can't send http headers with the Prometheus server, so you need another server that acts as a reverse proxy and adds your authentication cookie with the request.

TASVideos/Program.cs Outdated Show resolved Hide resolved
@adelikat adelikat self-requested a review October 18, 2024 16:48
@adelikat adelikat merged commit 0329372 into main Oct 18, 2024
1 check passed
@adelikat adelikat deleted the opentelemetry-prometheus-metrics branch October 18, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants