How To Do Distributed Tracing With Opentelemetry in Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] folks and welcome back and in today's video we'll be looking into something related to distributed racing now what is distributed tracing actually so now consider in a distributed environment wherein you have multiple Services satisfying a particular incoming request from a client so you need to trace how this particular request is routed to various Services how much time each of the services takes and What's the total response for the client that it gets now lately open Telemetry has been emerged as a common standard which is vendor neutral for collecting metrics logs and traces among the various applications that are running in a distributed environment so today what we'll be doing is we'll be looking at how we can create a spring boot application which actually uses micrometer inside the actuator in order to send the distributed traces in the open Telemetry format which will be read by a jagger back-end now this ciago back end will actually get in this particular traces and using this we can then visualize this onto a UI dashboard through which we can see how much time each of the spans took and what was the entire distributed Trace between the different services so with this let's get started so let's go to start.spring.io and create a project for us so what we are going to do is we are going to add in a few dependencies so we're going to add spring web and we are going to add the actuator dependency now I'm going to give this a particular group name and I'm going to call this as service we're going to use Java 17 and we are going to change this to me well and let's generate this particular project now I have already created the project and we have a sample service that is present here now if you see here what do we have is we have this particular project here in which we have a controller we have this main application here and an oltp configuration here now before all of this let's actually look at the various dependencies that we need so now when we actually added the actuator in the spring web these were the two dependencies that were added at the starter for the actuator and the starter for the web dependence here now along with this what we will have to add is we will have to add these two dependencies so first of all this is the micrometer tracing bridge for open Telemetry now what exactly this will be doing is so the micrometer which is present inside the actuator is responsible for collecting the traces and the Matrix right so we will be using this particular dependency as a bridge which will actually help us to support the open Telemetry standards using this dependent and see we are using this particular bridge for open Telemetry and then after that we will be using this particular dependency to actually send out the open Telemetry format of the traces to an external backend so now in this case our external backend is going to be Jagger and for this we have this particular Docker compose file so for this we have this particular Jagger Docker image that we'll be running using Docker compose and then we have to set this collector otlp enabled what does this do is basically they are going to enable the open Telemetry protocol collector enabled inside this particular image now whatever traces that come in this Jager Docker image collector will actually support otlp format that is the open Telemetry protocol format so first of all after this dependencies that we have just looked at here let's go and look at what our application is doing right now so this is simple a main application here which has a rest Builder so we need to provide a rest template using a builder and in the controller there is something special being done here in a distributed application so when you have a distributed architecture having multiple Services what do we have you have many services that run in together in the same cluster or in the same VPC or in the same private Network that you have right rather than actually creating separate Services what am I doing here is I'm going to use this same application being run twice as two separate instances one running on 8080 port and the other one running on 1890 both such that when I hit this path 1 using a curl command on poetry 8080 this one will actually internally call another service which will be running at 80 90 and invoke this part 2. so I'm just running the same instances it's just going to be running on two separate ports and the first instance will actually call the second install why we are doing this so as to capture the various traces that happens right from we calling the first service then the first serve is calling the second service and the second service and returning response data for service and then finally back to the client now this is a simple setup that will be running now to make this thing work what we have to do is provide this otlp configuration now what does this otlp configuration do is we have this HTTP span exporter which will actually export all our traces and span to a particular Jagger back end now we have to provide this particular URL to it so this URL is going to be configured here and then using this URL it will actually send the traces and the spans to a jagger backend now I have set this thing inside the properties file here wherein I'm setting the URL for Yaga which I will be running at Port 4318 using the docker compose file here so I have exported 4318 here and I'm exposing this particular port to access the particular Jagger UI itself now if you go back to this property file you see here I have set this management tracing sampling probability to 1.0 why am i setting this is basically I want to capture all the traces with the probability of one that is basically all the traces that have been generated will be actually captured and sent to the Yaga backend along with this I am setting this particular logging pattern in order to display us the trace ID and the span ID from each of the services that we'll be running now with all of this being done we can actually start a jagger Docker compose so let's start with Docker compose up and with this actually Jagger is all available now we are going to actually open this here so I have already opened this Jagger back end here and I'm going to now refresh the UI so I have no Services right now because I have no Services communicating with Jagger right now but what I'm going to do is after I have built this particular project I'm going to actually copy the commands from here so here basically what I'm trying to do is I'm going to run this particular jar giving it a particular name as service one and running it at Port 8080. similarly I'm doing the same stuff for service 2 but in this case I'm giving it a name service too and I'm running it at Port 80 90. so let's actually take this one first and run it so the application has now started now this is actually service one which is on the left and now I'm going to run service to on the right perfect so both the services are running now what we're going to do we are going to make a simple curl request to this part one so if I go back to this controller here and I'm going to make a call request to this one so I'm going to run a curl request and I'm going to say 8080 service slash part one so now what it has done is it actually has got a response from part one which also combines a response from part two now this part 2 is actually a response from a service running at Port 8090. let's look at our service logs so if you see here this was the incoming request that came in here and this was at path one and then after that you see here the incoming request came to Part 2 here which responded back to path one and then after it responded back to the curve request now if you look here closely you can see this particular distributed Trace ID this is the same Trace ID for this particular entire Trace call in service one as well as in service 2 here so this both are the same Trace IDs but if you see you have this new span IDs here so this was a span for this particular service and this was the span ID for this particular service the next thing that we're going to look at is inside the Jagger you are so I'm going to refresh this and if you can see here now you see these two Services you can ignore the Jagger query one because that belongs to the internal part of Jagger which also monitors itself and you get this as a third service so let's look at the service one and find all the traces so if you see here an incoming request was made at path one and then afterwards service one was calling service to and let's look at this particular tree so if I open this particular Trace you can see here this was the entire 450 milliseconds around and service one took around 412 milliseconds along with service 2 taking around 163 milliseconds so if I expand this you can see the particular span ID here so this is the four one four F A which was present here so this was a span ID from here and then if you want to see the span ID from here you can see it here so this was the span ID for this particular entire service code so e2b is the span ID from here now you can read all about this on my blog refactorfirst.com I'll add a link to that particular block into the description below so we just saw how we can Implement open Telemetry format for distributed tracing into a spring boot application and then export these particular traces into a jagger back end and visualize the on the Jagger UI now I have written all about this on my blog site refactorfirst.com you can check out the link in the description below and if you like this particular video give it a thumbs up and make sure you subscribe to this particular channel for more such videos to come till then take care and see you in my next one is [Music]
Info
Channel: Refactor First
Views: 6,415
Rating: undefined out of 5
Keywords: OpenTelemetry, Java, OTel, distributed tracing, developer, Programming, Coding, Backend Microservices, Microservices Architecture, Software Development, Backend Development, Web Development, Backend Developer, microservices, spring boot, spring framework, how to debug java, Spring boot debugging, spring example, springboot example, spring boot opentelemetry, opentelemetry jaeger, jaege ui, spring boot jaeger, Java development
Id: jfbUwZwzC98
Channel Id: undefined
Length: 11min 11sec (671 seconds)
Published: Thu Jun 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.