Git Product home page Git Product logo

Comments (12)

cijothomas avatar cijothomas commented on June 17, 2024

can I avoid to get the body in the attrbiutes section? It is not an attribute for my requirements..

Because you have an attribute named "body"!
string? body = null,

from opentelemetry-dotnet.

smoms avatar smoms commented on June 17, 2024

@cijothomas thanks for your answer.
That's right I use body as attribute.
But again my question, assuming I use this list of attributes (without body now):

 [LoggerMessage(LogLevel.Information, "{???}")]
        public static partial void LogInformation(this ILogger logger,
            LogType logType,
            string? businessTxKeyIoTDeviceId = null,
            string? businessTxKeyIoTDeviceSn = null,
            string? businessTxKeyOrganizationId = null,
            string? businessTxKeyUserId = null,
            string? businessTxKeyUserGuid = null,
            string? auditEventAction = null,
            string? entityType = null,
            string? entityId = null,
            string? subjectId = null);

How can I now send a log message with body plus (the above) attributes?

from opentelemetry-dotnet.

smoms avatar smoms commented on June 17, 2024

Actually I have used a Processor to strip the body from the attributes and now I get what I want to see:

body": "this is my payload message",
  "traceid": "18d9f28764f0753ed00f00ddedb5a37b",
  "spanid": "3a6ab85a68dfe41d",
  "severity": "Information",
  "flags": 1,
  "attributes": {
    "businessTxKeyIoTDeviceSn": "blue_sky",
    "businessTxKeyUserGuid": "12345",
    "businessTxKeyUserId": "abc",
    "logType": "Diagnostic",
    "{OriginalFormat}": "{body}"
  },

question is: is this the expected/right approach?

from opentelemetry-dotnet.

cijothomas avatar cijothomas commented on June 17, 2024

@cijothomas thanks for your answer. That's right I use body as attribute. But again my question, assuming I use this list of attributes (without body now):

 [LoggerMessage(LogLevel.Information, "{???}")]
        public static partial void LogInformation(this ILogger logger,
            LogType logType,
            string? businessTxKeyIoTDeviceId = null,
            string? businessTxKeyIoTDeviceSn = null,
            string? businessTxKeyOrganizationId = null,
            string? businessTxKeyUserId = null,
            string? businessTxKeyUserGuid = null,
            string? auditEventAction = null,
            string? entityType = null,
            string? entityId = null,
            string? subjectId = null);

How can I now send a log message with body plus (the above) attributes?

isn't that the default behavior you are observing? i.e body + all the above as attributes?
Can you show the full code, so we can take a look at if OTLP Exporter is missing some special casing..

from opentelemetry-dotnet.

smoms avatar smoms commented on June 17, 2024

I am not sure my question is clear then.

I need both attributes and body but the body not as an attrbiute.
If - as you suggest I remove the body from the attribute list, can you please tell me how I can send the body field in the log message?

So far I was using body as attribute, which again I do not want as an attribute. I was calling the looging API with something similar to:

logger.LogInformation(LogType.Diagnostic,
                body: "this is my payload message",
                businessTxKeyUserId: "xxx",
                businessTxKeyIoTDeviceId: "xxx",
                businessTxKeyUserGuid: userGuid,          
                businessTxKeyDeviceSn: "12345");

Here the configuration:

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

from opentelemetry-dotnet.

cijothomas avatar cijothomas commented on June 17, 2024

I used this example with OTLP Exporter
https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/getting-started-console/Program.cs#L19

And this is what I see in OTel Collector:

LogRecord #0
ObservedTimestamp: 2024-02-21 14:59:26.8189406 +0000 UTC
Timestamp: 2024-02-21 14:59:26.8189406 +0000 UTC
SeverityText: Information
SeverityNumber: Info(9)
**Body: Str(Food `{name}` price changed to `{price}`.)**
Attributes:
     -> name: Str(artichoke)
     -> price: Double(9.99)
Trace ID:
Span ID:
Flags: 0
LogRecord #1
ObservedTimestamp: 2024-02-21 14:59:26.8479572 +0000 UTC
Timestamp: 2024-02-21 14:59:26.8479572 +0000 UTC
SeverityText: Critical
SeverityNumber: Fatal(21)
Body: Str(A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).)
Attributes:
     -> brandName: Str(Contoso)
     -> productDescription: Str(Salads)
     -> productType: Str(Food & Beverages)
     -> recallReasonDescription: Str(due to a possible health risk from Listeria monocytogenes)
     -> companyName: Str(Contoso Fresh Vegetables, Inc.)
Trace ID:
Span ID:

Both body and attributes are populated correctly. Can you use this example as-is, and see if it matches your expectation? And then adjust your question based on that, to make sure I am following your scenario fully.

from opentelemetry-dotnet.

smoms avatar smoms commented on June 17, 2024

@cijothomas thanks.
I understand this example. And it is also showing the point of my issue.
Please see the "pre-defined" body field in your example:

Body: Str(A {productType}recall notice was published for{brandName} {productDescription}produced by{companyName} ({recallReasonDescription}).)

generate by the code:

 [LoggerMessage(LogLevel.Critical, "**A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).**")]
    public static partial void FoodRecallNotice(
        this ILogger logger,
.....

Again question is:

how can a client - using logger.Information(..) function - pass a body field dynamically instead of being constrained by it as a pre-defined string?
In other words, the body here can just interpolate attributes in a pre-defined string. But what if I want a completely indipendent and dynamic body field that a client can pass in?

Normally in aIlogger.Information() call a client is free to pass any body/message information freely and dynamically without being constrained by a pre-defined, interpolated string template.

PS
This is my issue. And I may agree with you that the Title in this question might not be indeed accurate.

from opentelemetry-dotnet.

cijothomas avatar cijothomas commented on June 17, 2024

There is no need to keep using the same body. That is what you'll get by following the source-generation way of using ILogger.

You can use ILogger.Log, pass any attributes, and provide a formatter which can be used to produce "body" dynamically.
To do that:

  1. Enable this https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/logs/customizing-the-sdk#includeformattedmessage
  2. use the foundational Log api and pass the formatter where you can control what the body would look like.

let me know if this helps. I can write an example here, if not clear.

from opentelemetry-dotnet.

smoms avatar smoms commented on June 17, 2024

@cijothomas it would be great an example. many thanks.

from opentelemetry-dotnet.

cijothomas avatar cijothomas commented on June 17, 2024

@cijothomas it would be great an example. many thanks.

logger.Log(logLevel: LogLevel.Information,
            eventId: default,
            state: new List<KeyValuePair<string, object>>()
            {
                new KeyValuePair<string, object>("food", "apple"),
                new KeyValuePair<string, object>("price", 3.99)
            },
            exception: null,
            formatter: (state, ex) => "Use any message here.!"
            );

"Use any message here.!" will be the body. And you are free to decide what its value will be. I just hard coded it for example.

from opentelemetry-dotnet.

smoms avatar smoms commented on June 17, 2024

Ok that's the standard ILogger API. Since my aim is to wrap the log as a library to encapsulate a pre-defined list of attributes here instead there would be a free list of key-value pairs...

I am following the initial apporach where my trick is to have the body as an attrbute and then I strip it out with an OTel Processor. I am not sure how much artificial is this though ;)

from opentelemetry-dotnet.

cijothomas avatar cijothomas commented on June 17, 2024

Closing as there is no further questions left.

from opentelemetry-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.