Microservice observability by OpenTelemetry!
Tags: opensource, tracing, observability, opentelemetry
Published: 2024-02-08 11:07:40

As a developer, we add logs to trace the issues and understand the flow of the code, but the fundamental benefit of having a logger is to get more tracing details to track the request execution. 
Here we will go through the log observability details and tools which make tracing easier for the microservice architecture services.

![Microservice Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2aiea494jw5lcvdiaznz.png)

**Why do we need to monitor application logs?**
Monitoring the logs is a fundamental process to help in identifying the memory leaks, application errors/issues and alert the development team to take immediate action.

**Why do we need one more observability when we have a log monitor? Monitoring VS Observability?**
Monitoring the logs may provide the detailed execution of the request, but it does not help in analyzing the factored mentioned below.
1. What was the execution span interval of each method involving the operation?
2. How many system resources were used for the execution of the request?

Developers were not worried much about the observability data initially because applications were monolithic, it was easy to trace the request execution and log tracing. But with microservice, it's not that simplified why to identify the request execution because it involves a lot of services for the single request execution.
Distributed log tracing is the mechanism involved in request execution in a microservice architecture system.

**How Does distribution log tracing works?**
Typically distributor logs tracer tools like [Jaeger](https://www.jaegertracing.io/), [Zipkin](https://zipkin.io/) agents will intercept the request header log data with the unique Id and maintain that id in all the child chain requests in the other service and maintain the tracing of the request execution.

**What is Observability data? What is OpenTelemetry?**
Observability data will contains the fundamental data structure which can be presented to get the desired details for analysis and presentation.
Observability data structure:
1. Tracing data.
2. Context log data
3. Metric data

OpenTelemetry is the open-source library that provides the APIs and SDK for the observability instrument data of the application and its combined product of the previously known open source tools.

[OpenCensus](https://opencensus.io/) + [OpenTracing](https://opentracing.io/) = [OpenTelemetry](https://opentelemetry.io/)

OpenTelementry support almost all languages.
1. Java
2. .Net
3. Go
4. Python
5. NodeJS

**OpenTelemetry Architecture**
![OpenTelemetry Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vviljjkdhuz2qbwzwlwy.png)

OpenTelemetry support two types of instrument observability data
1. Manual Instrumentation.
2. Auto Instrumentation.

*Manual Instrumentation:*
OpenTelemetry provides the SDK and API to collect the observability data manually, where the developer needs to initialize the OpenTelemetry logger and collector i.e., add the span in the respective endpoint for the request tracing.

*Auto Instrumentation:*
OpenTelemetry supports the auto instrumentation i.e., We do not need to add any code!. The simple configuration to get the observability data of the application.

Generally auto instrumentation work as a master-agent model.
1. Agent will be collecting the data and send to the process i.e., as shown in the architecture image, 
2. The receiver will get the logs data and process the data as per the observability standards and export it to the tools like [Jaeger](https://www.jaegertracing.io/), [Zipkin](https://zipkin.io/).

**OTEL Key Fields**:
OpenTelementry provides a certain set of keys for the easy analysis of tracing, a few are listed below.
`TraceId`: The TraceId associated with the request.
`SpanId`: Than Span associated with the request.
`TraceFlags`: W3C trace flag.
`Attributes`: Additional associated attributes 

For more W3C trace details [check here]( https://www.w3.org/TR/trace-context/?spm=a2c65.11461447.0.0.a55d2e89B14Qrb#trace-id)

**Tracing request span flow:**
![Tracing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spsi46taw7mgqcj1nxyu.png)
When the user clicks on the "Pay Now" button, the request transverse through service A to service B, and the request is queued for the further process, here we can see the "Span" request taken in each service transverse.

**OpenTelemetry Logger and Metrics:**
**Logger**: logger data will be helpful to trace the request flow, using the [Jaeger](https://www.jaegertracing.io/), [Zipkin](https://zipkin.io/) tool we can visualize the request tracing

**Metrics**: Metrics data will be helpful to visualize the request rates, CPU utilization, etc. using tools like [Prometheus](https://prometheus.io/).

**Part - 2: working samples.
**