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

Processor does not seem to add attribute when exported to Console #5732

Closed
smoms opened this issue Jul 1, 2024 · 2 comments
Closed

Processor does not seem to add attribute when exported to Console #5732

smoms opened this issue Jul 1, 2024 · 2 comments
Labels
question Further information is requested

Comments

@smoms
Copy link

smoms commented Jul 1, 2024

Processor does not seem to add attribute when exported to Console

Hi,

I have a simple processor which adds a new attributes currentDateTimeInUtc
This attribute is not showing/being added when the exporter is to Console but it is added when it is exported to an OTLP collector. Why?

Here the code:

internal class LogAttributesProcessor : BaseProcessor<LogRecord>
{
    private readonly string _name;


    public LogAttributesProcessor(string name = "LogAttributesProcessor")
    {
        _name = name;
    }

    public override void OnEnd(LogRecord record)
    {

        var tempDic = record.Attributes.ToDictionary(x => x.Key, y => y.Value);
        IDictionary<string, object?> finalDic = new Dictionary<string, object?>();

        foreach (var attr in tempDic)
        {
            ---some logic--
        }
        //Inject UTC DateTime attribute in ISO-8601 format
        **finalDic["currentDateTimeInUtc"] = DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);**

        record.Attributes = finalDic.ToList();
    }

}

And here how it is registered:

internal static void ConfigureLoggingOpenTelemetry(this ILoggingBuilder loggingBuilder, TelemetryOptions telemetryOptions)
{
    loggingBuilder.AddOpenTelemetry(options =>
    {
        options
            .SetResourceBuilder(
                ResourceBuilder
                    .CreateDefault()
                    .AddService(telemetryOptions.ServiceName!, telemetryOptions.ServiceNamespace!, telemetryOptions.ServiceVersion!)
                );
        if (!string.IsNullOrWhiteSpace(telemetryOptions.EndpointLog?.ToString()))
        {
            options.AddOtlpExporter(o =>
            {
                o.Protocol = OtlpExportProtocol.HttpProtobuf;
                o.Endpoint = telemetryOptions.EndpointLog;
             });
        }
        else
        {
            options.AddConsoleExporter();
        }

        options.AddProcessor(new LogAttributesProcessor());
    });
}

Additional context

image

@smoms smoms added the question Further information is requested label Jul 1, 2024
@cijothomas
Copy link
Member

Strange.. I guess the order + buffering + shared state is working against you!
Could you do options.AddProcessor(new LogAttributesProcessor()) before adding the exporters?

@smoms
Copy link
Author

smoms commented Jul 2, 2024

@cijothomas you are definitely right. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants