Spring Boot Sleuth - TraceI vs TraceIdString - spring-boot

I am learning about sleuth tracing. And while running the application, I could see logs with trace Id (ec88298d62773aa6) along with spandId and application name. What I want to know is
ID available in logs is traceIdString and not traceId ?
What is the difference between the two ?
And during logs analysis, should we consider traceId or traceIdString?
Sample log
2021-10-07 16:35:04.421 INFO [demo,ec88298d62773aa6,ec88298d62773aa6] 1324 --- [nio-8080-exec-1] com.example.demo.demo.DemoApplication : inside controller method
Thanks for your response.

traceIdString is the hex representation of the traceId as can be seen here: https://github.com/openzipkin/brave/blob/master/brave/src/main/java/brave/propagation/TraceContext.java#L218
During problem analysis you will usually see the hex representation in logs or the user interfaces of distributed tracing systems.

Related

Log metrics in Springboot applicaiton using LoggingMeterRegistry

I have some metrics being created in my springboot application and want to log the details of the metrics in a specific format as below:
2018-07-06 17:00:28,884 [metrics-logger-reporter-thread-1] [INFO] (com.codahale.metrics.Slf4jReporter) type=METER, name=io.dropwizard.jetty.MutableServletContextHandler.async-timeouts, count=0, mean_rate=0.0, m1=0.0, m5=0.0, m15=0.0, rate_unit=events/second
I'm currently using LoggingMeterRegistry to log my metrics but the format they get printed it is:system.cpu.usage{} value=0.041922","logger_name":"io.micrometer.core.instrument.logging.LoggingMeterRegistry"
I know that the required format can be achieved through codahale.dropwizard metrics but i want the same using LoggingMeterRegistry from "micrometer" since thats what i am using to calculate my other metrics.
Any help?

How to add same prefix to all LOGS in request in SpringBoot project

I use #Slf4j to add logs in all layers in my SpringBoot project.
I want to use some unque ID for all logs inside onq request.
For example generate UUID at the RestController and automatic add it to every logs in this thread
For example
[..... Some UUID ....] "The real logger message"
How to do this?
It need to have possibility to filter all logs of specific request.
If your application is threaded (or any better word to describe "opposed to a reactive application where it might be possible that everything happens in the main thread"), use the mapped diagnostic context (MDC). The MDC is a thread-bound Key-Value Map, where you can put and retrieve data. Baeldung has a tutorial on logging with the mdc using multiple logging frameworks. Also, there are plenty of examples across the web.
If your application should be reactive, you may wanna check out this project reactor reference.

How to log the trace id using Springboot custom filters

I have few custom filters in my Springboot Webflux API. This project has been activated with Spring Sleuth, however, these filters are not logging the trace and span ids in the log messages.
I made sure that the order was set properly for these filters.
Example:
2020-03-23 12:53:18.895 -2020-03-23 12:53:18.895 INFO [my-spring-boot,,,] 9569 --- [ctor-http-nio-3] c.d.a.f.test.myTestEnvWebFilter : Reading data from header
Can someone please provide your insights on this?
It might be due to the default sampler percentage configuration, take a look at this article for an example:
https://www.baeldung.com/tracing-services-with-zipkin

How to Add DataDog trace ID in Logs using Spring Boot + Logback

OK, I spent quiet some time figuring out how to configure stuff to have DataDog trace ID in logs but couldn't get it working. To be clear what I'm looking for is to see trace IDs in logs message, the same way that adding spring-cloud-starter-sleuth to the classpath, automatically configure Slf4j/Logback to show trace IDs in log messages.
Where I've started:
We've got a simple web spring boot application running as a Docker container deployed as an AWS Elastic BeansTalk, whose logs go to CloudWatch and we read them there.
We have DataDog as a Java agent (thus no dependencies in pom.xml)
We have SLF4J/Logback in our dependencies list.
There's no other related depndencies (like dd-trace-ot or any opertracing libs)
What I did so far:
I found on SO that adding opentracing-spring-cloud-starter will add log integration automatically. But I couldn't get it working.
On DD website, it says configuring the pattern is enough to see the IDs, but in our case it didn't work. (is it because we don't have our logs a JSON?). Also, adding dd-trace-ot didn't help.
Notes:
We can't switch to JSON logs.
We can't switch to any other library (e.g. Slueth).
We can't go away from CloudWatch.
Can someone tell me how exactly I need to configure the application to see trace IDs in log messages? Is there any documentation or samples I can look at?
Do you have the ability to add some parameters in the logs sent. From the documentation you should be able to inject the trace id into your logs in a way that Datadog will interpret them.
You can also look at a parser to extract the trace id and span id from the raw log. This documentation should help you out on that.
From the documentation, if you don't have JSON logs, you need to include dd.trace_id and dd.span_id in your formatter:
If your logs are raw formatted, update your formatter to include
dd.trace_id and dd.span_id in your logger configuration:
<Pattern>"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -
%X{dd.trace_id:-0} %X{ dd.span_id:-0} - %m%n"</Pattern> ```
So if you add %X{dd.trace_id:-0} %X{ dd.span_id:-0}, it should work.

How to trace total in/out bandwidth used by users in spring boot application?

After some researching, I have discovered that there is a spring actuator project which is used to monitor and log information about the application. Here is the link that suggests how to put log in to my own custom repository for post processing. [http://www.sedooe.com/2017/08/tracing-requests-and-responses-with-spring-boot-actuator/ ]
However, the total in/out byte counts are not included. I know that I can try to wrap HttpServeletRequest and HttpServeletResponse (in the filter, pass them to the filter chain so that it can count in/out throughput whenever they are consumed or written out) which will return custom SerlvetInputStream and ServletOutputStream that can count the number of bytes.
This seems to be a common problem in business application and there should already be a solution so I wonder if there is any easier way to achieve this.
From my understanding the Spring-Boot 1.5 actuator implementation doesn't provide such a functionality. (Assuming you are talking about Spring-Boot 1.5.)
What you could do is having a look at the Micrometer project. It's the new actuator metrics "backend" for Spring-Boot 2.0. In case you are still using Spring-Boot 1.5 you can use the micrometer-spring-legacy library in your app.
Key thing to understand here is that for Spring-Boot 1.5 this is a metric backend which is living next to the "old" actuator.
What the project provides is instrumentation for embedded Jetty and Tomcat server. Allthough only the latter does provide in and out traffic metrics.
Assuming Tomcat, you'll get tomcat_global_received_bytes_total and tomcat_global_sent_bytes_total metrics over which you can reason about in your favorite monitoring system.

Resources