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

[BUG] Exceptions are not registered as errors in bridge mode #2472

Closed
NikiforovAll opened this issue Oct 23, 2024 · 3 comments
Closed

[BUG] Exceptions are not registered as errors in bridge mode #2472

NikiforovAll opened this issue Oct 23, 2024 · 3 comments

Comments

@NikiforovAll
Copy link

APM Agent version

Elastic Cloud / APM

Environment

windows, net8

Describe the bug

When I use Elastic.Apm in bridge mode the exceptions are recorded on Activity.RecordExceptions, I don't see them on "Errors" tab

Image
Image
Image

To Reproduce

using System.Diagnostics;
using Elastic.Apm.AspNetCore;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddOpenTelemetry(logging =>
{
    logging.IncludeFormattedMessage = true;
    logging.IncludeScopes = true;
});

builder
    .Services.AddOpenTelemetry()
    .WithTracing(tracing =>
    {
        if (builder.Environment.IsDevelopment())
        {
            // We want to view all traces in development
            tracing.SetSampler(new AlwaysOnSampler());
        }

        tracing
            .AddSource("Worker")
            .AddHttpClientInstrumentation()
            .AddAspNetCoreInstrumentation()
            .AddConsoleExporter();
    });

builder.Services.AddHostedService<Worker>();

var app = builder.Build();

app.UseElasticApm(app.Configuration);

app.MapGet("/", () => "Hello World!");

app.Run();

public class Worker : BackgroundService
{
    public static readonly ActivitySource ActivitySource = new("Worker");

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            using var activity = ActivitySource.StartActivity("Worker.ExecuteAsync");

            try
            {
                throw new Exception("Something went wrong");
            }
            catch (Exception ex)
            {
                activity?.SetStatus(Status.Error.WithDescription(ex.Message));
                activity?.RecordException(ex);
            }
            await Task.Delay(TimeSpan.FromSeconds(15), stoppingToken);
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <RootNamespace>elastic_apm_otel_bridnge_errors</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Elastic.Apm.AspNetCore" Version="1.30.0" />
      <PackageReference Include="Elastic.Apm" Version="1.30.0" />
      <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
      <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
      <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
      <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
      <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
      <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
  </ItemGroup>

</Project>

Steps to reproduce the behavior:

Run the code above.

Expected behavior

I can find errors on the "Errors" tab.

Actual behavior

No errors are registered

@NikiforovAll NikiforovAll added the bug Something isn't working label Oct 23, 2024
@stevejgordon
Copy link
Contributor

RecordException is an extension method of the OTel SDK. The bridge is designed to capture spans directly from the .NET Activity APIs. If you're using the OTel SDK, you can send OTLP directly to Elastic APM rather than rely on our SDK and the bridge. Is anything blocking you from using the OTLP exporter from the SDK, rather than our APM Agent? We also have a distro you might want to check out. Therefore, the current behaviour is expected.

In .NET 9 (and System.Diagnostics.DiagnosticSource 9), a dedicated AddException method is being added. Once that is released, we will review if that's something we can and should bridge as an APM error.

Does that help?

@stevejgordon stevejgordon added question Further information is requested and removed triage bug Something isn't working labels Nov 11, 2024
@NikiforovAll
Copy link
Author

Yes, I think it answers my questions. Thank you for the answer

Adding this information in the docs would be great so others can save some time :)

@stevejgordon stevejgordon added docs and removed question Further information is requested labels Nov 12, 2024
@stevejgordon
Copy link
Contributor

@NikiforovAll, I'll review the docs when we update for .NET 9, but I'm not sure this needs to be a note there as we don't imply we support anything except for the .NET API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants