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

AttachLogsToActivityEvent doesn't work with 'IncludeFormattedMessage' in .net7 minimal api project #1001

Open
sanjaydebnath opened this issue Jan 3, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@sanjaydebnath
Copy link

sanjaydebnath commented Jan 3, 2023

Bug Report

List of all OpenTelemetry NuGet
packages
and version that you are
using (e.g. OpenTelemetry 1.0.2):

  • Attaching the csproj here
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0-*" />
    <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />

    <PackageReference Include="OpenTelemetry.Contrib.Preview" Version="1.0.0-beta2" />
    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Exporter.Prometheus.HttpListener" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.4.0-rc.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.10" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.10" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.1.0-beta.2" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include=".usings" />
  </ItemGroup>

</Project>

Runtime version (e.g. net462, net48, netcoreapp3.1, net6.0 etc. You can
find this information from the *.csproj file):

  • net7.0

Symptom

While using 'AttachLogsToActivityEvent' to attach ILogger messages to Activity Events, actual formatted message or scopes or state values are not getting attached.

What is the expected behavior?

All the ILogger log messages should include actual formatted message or scopes or state is those are enabled as part of OpenTelemetryLoggerOptions

What is the actual behavior?

I can only see the default 3 CategoryName, LogLevel & EventId getting logged in console as part of the events
image

Reproduce

Here is the full Program.cs file (along with the csproj above)

using ApiVersioning.Examples;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;

services.AddProblemDetails();
services.AddEndpointsApiExplorer();
services.AddApiVersioning(
            options =>
            {
                options.ReportApiVersions = true;
            })
        .AddApiExplorer(
            options =>
            {
                options.GroupNameFormat = "'v'VVV";
                options.SubstituteApiVersionInUrl = true;
            })
        .EnableApiVersionBinding();
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
services.AddSwaggerGen(options => options.OperationFilter<SwaggerDefaultValues>());

builder.Services.Configure<AspNetCoreInstrumentationOptions>(options => options.RecordException = true);
var resource = ResourceBuilder.CreateDefault().AddService("MyService");
builder.Services
        .AddOpenTelemetry()
        .ConfigureResource(ConfigureResource)
        .WithTracing(o =>
        {
            o.SetSampler(new TraceIdRatioBasedSampler(1.0))
            .AddSource("MyService")
            .AddAspNetCoreInstrumentation(option =>
            {
                option.RecordException = true;
            })
            .AddHttpClientInstrumentation();

            o.AddConsoleExporter(o => o.Targets = ConsoleExporterOutputTargets.Console);

        })
        .StartWithHost();

builder.Logging.ClearProviders();
builder.Logging.Configure(o => o.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
                                                        | ActivityTrackingOptions.TraceId
                                                        | ActivityTrackingOptions.ParentId);
builder.Logging.AddFilter<OpenTelemetryLoggerProvider>("*", LogLevel.Information);
builder.Logging
    .AddOpenTelemetry(loggerOptions =>
    {
        loggerOptions.SetResourceBuilder(resource);
        loggerOptions.IncludeFormattedMessage = true;
        loggerOptions.IncludeScopes = true;
        loggerOptions.ParseStateValues = true;
        loggerOptions.AddConsoleExporter();
        loggerOptions.AttachLogsToActivityEvent();
    });

var app = builder.Build();

var common = app.NewVersionedApi("Common");
var commonV1 = common.MapGroup("/api/v{version:apiVersion}/common").HasApiVersion(1.0);
commonV1.MapGet("/", (ILogger<Program> logger) => 
{
    logger.LogInformation("Calling hello worls api!");
    return Results.Ok("Hello World common v1!");
});

app.UseSwagger();
app.UseSwaggerUI(
    options =>
    {
        var descriptions = app.DescribeApiVersions();

        foreach (var description in descriptions)
        {
            var url = $"/swagger/{description.GroupName}/swagger.json";
            var name = description.GroupName.ToUpperInvariant();
            options.SwaggerEndpoint(url, name);
        }
    });

app.Run();

static void ConfigureResource(ResourceBuilder r) => r.AddService(
            serviceName: "MyService",
            serviceVersion: "1.0.0",
            serviceInstanceId: Environment.MachineName);

We will close this issue if:

  • The repro project you share with us is complex. We can't investigate custom
    projects, so don't point us to such, please.
  • If we can not reproduce the behavior you're reporting.

Additional Context

Add any other context about the problem here.

Attached is the full project.
MinimalOpenApiWithOTLogger.zip

@sanjaydebnath sanjaydebnath added the bug Something isn't working label Jan 3, 2023
@cijothomas cijothomas transferred this issue from open-telemetry/opentelemetry-dotnet Feb 14, 2023
@martinjt
Copy link
Member

martinjt commented Jun 2, 2024

It looks like this has been removed functionality. @CodeBlanch is that correct? if so we should close this as not supported anymore.

@martinjt martinjt closed this as completed Jun 2, 2024
@martinjt martinjt reopened this Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants