question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AddAspNetCoreInstrumentation does not output traces when behind Ocelot proxy

See original GitHub issue

Bug Report

List of all OpenTelemetry NuGet packages:

  • OpenTelemetry.Exporter.Console 1.4.0-rc.1
  • OpenTelemetry.Exporter.OpenTelemetryProtocol 1.4.0-rc.1
  • OpenTelemetry.Exporter.Prometheus.AspNetCore 1.4.0-rc.1
  • OpenTelemetry.Extensions.Hosting 1.4.0-rc.1
  • OpenTelemetry.Instrumentation.AspNetCore 1.0.0-rc9.10
  • OpenTelemetry.Instrumentation.Http 1.0.0-rc9.10
  • OpenTelemetry.Instrumentation.SqlClient 1.0.0-rc9.10

Runtime version:

  • net6.0

Symptom

Using Ocelot as an API gateway to downstream services, and using AddAspNetCoreInstrumentation in the downstream services, the HTTP requests going to those downstream services do not produce trace output. If we call the downstream services directly, traces are logged. (Tested with both console exporter and OTLP exporter.)

What is the expected behavior?

Traces should be logged also when using an API gateway in front.

What is the actual behavior?

Traces are only logged for non-forwarded requests.

Reproduce

Using the following set-up code for OpenTelemetry in the downstream service:

services.AddOpenTelemetry()
                .WithMetrics(
                    (builder) =>
                    {
                        builder.AddAspNetCoreInstrumentation();
                        builder.AddHttpClientInstrumentation();
                        builder.AddPrometheusExporter();
                    }
                )
                .WithTracing(
                    (builder) =>
                    {
                        builder.AddHttpClientInstrumentation();
                        builder.AddAspNetCoreInstrumentation();
                        builder.AddSqlClientInstrumentation(
                            options => options.SetDbStatementForText = true);
                        builder.AddHotChocolateInstrumentation();
                        builder.AddOtlpExporter(options =>
                            configuration.GetSection("Tracing:OpenTelemetry").Bind(options)
                        );
                    }
                )
                .StartWithHost();

No OpenTelemetry is added to the API gateway service.

Additional Context

Using the following Ocelot configuration:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "api",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "SwaggerKey": "MyService"
    }
  ]
}

Where api is the name of the downstream service running with docker-compose.

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
vishweshbankwarcommented, Dec 14, 2022

Does it repro if you use AlwaysOnSampler ?

1reaction
aristoteloscommented, Dec 21, 2022

OK, I think I get it. So any ASP.NET Core-based API gateway will probably create parent spans for each request, and this will stop my downstream service spans from being sampled, because the default sampling strategy is to skip traces that have parents that were not sampled. In my case, I will anyways add OpenTelemetry instrumentation and an OpenTelemetry exporter to the API gateway, which solves the problem for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

gateway ocelot API doesn't work .NET 6 ASP.NET C# - ...
In .net core 6, from ocelot 17.0.1 you have to use "Routes" instead "ReRoute". and you can put it directly in the appsettings.json....
Read more >
Building Ocelot API Gateway Microservices with ASP.NET ...
We can simply define our internal services as the proxy layer hanging out. When the user throws a request from the application, he...
Read more >
Ocelot Documentation
Ocelot is aimed at people using .NET running a micro services / service orientated architecture that need a unified.
Read more >
Designing and implementing API Gateways with Ocelot in . ...
Coupling: Without the API Gateway pattern the client apps are ... It acts as a reverse proxy, routing requests from clients to services....
Read more >
Implementing an API Gateway in ASP.NET Core with Ocelot
This article briefly discusses the concepts around microservices architecture and how you can work with API Gateways to have a consistent ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found