Skip to content

Automatically log correlation IDs when Micrometer tracing is being used #33280

Closed
Listed in
@mhalbritter

Description

@mhalbritter
Contributor

Right now, as documented users have to set this in their config to get span id and trace ids in logs:

logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

Besides that it abuses the logging level pattern, it is not a good developer experience.

I would prefer it to be auto-configured in the case a Tracer bean is available. Not sure if this is technically feasible.

We should additionally introduce a logging.pattern.correlation property which can be used.

Activity

added this to the 3.1.x milestone on Nov 21, 2022
changed the title [-]Improve span and trace id display in logs[/-] [+]Make is easier to add span and trace id to logs[/+] on Nov 21, 2022
mentallurg

mentallurg commented on Nov 27, 2022

@mentallurg

@mhalbritter : I agree that the current for using trace ID and span ID in the logging format is bad and should be improved.
But I don't agree that it should be added to the format automatically. It may help in some cases, but it can make logs worse readable in many other cases.
Besides, new features should not break the current behaviour whenever possible.

welsh

welsh commented on Dec 5, 2022

@welsh

@mentallurg previous behaviour that I experienced was that adding:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

Did add it automatically to the default logging, so it was a surprise that I needed to manually add it back in when migrating to Spring Boot 3.

changed the title [-]Make is easier to add span and trace id to logs[/-] [+]Make it easier to add span and trace id to logs[/+] on Mar 1, 2023
modified the milestones: 3.1.x, 2.7.x, 3.2.x on May 16, 2023
philwebb

philwebb commented on May 22, 2023

@philwebb
Member

A few of us discussed this issue again today and we'd like to default to the following format if tracing is enabled:

When a correlation ID is present we'll use traceId dash spanId as recommended by w3c:

2023-05-22T08:53:19.665-07:00 DEBUG 7704 --- [nio-8080-exec-5] [646b8fef796a9e022e983eb44cb82a2f-fe26c1e3afcf59c5] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

If the correlation is missing, we'll pad with spaces:

2023-05-22T08:53:19.665-07:00 DEBUG 7704 --- [nio-8080-exec-5] [                                                 ] o.s.web.servlet.DispatcherServlet        : Completed 200 OK```

If one part of the correlation ID is missing, we'll pad with spaces and include the dash. This is considered a bug and we want the logs to look strange:

2023-05-22T08:53:19.665-07:00 DEBUG 7704 --- [nio-8080-exec-5] [646b8fef796a9e022e983eb44cb82a2f-                ] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2023-05-22T08:53:19.665-07:00 DEBUG 7704 --- [nio-8080-exec-5] [                                -fe26c1e3afcf59c5] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

We think showing the application name in the logs is a useful feature outside of correlation IDs. We'll open a new issue to support it and enable it by default when correlation IDs are shown.

We also discussed that it's important that slueth style correlation IDs can be used if the user wishes to do so.

changed the title [-]Make it easier to add span and trace id to logs[/-] [+]Automatically log correlation IDs when Micrometer tracing is being used[/+] on Jun 22, 2023

9 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @philwebb@welsh@jonatan-ivanov@mentallurg@mhalbritter

      Issue actions

        Automatically log correlation IDs when Micrometer tracing is being used · Issue #33280 · spring-projects/spring-boot