What is OpenTelemetry?

07 januari 2022 om 10:00 by ParTech Media - Post a comment

Technology evolves rapidly and there have been huge developments in the last few years. For example, three-tier architecture-based applications are moving towards MVC architecture; big monolithic applications are getting converted to confined and small microservices.

The evolution happens to address the difficulties and to enhance the performance and user-friendliness of the application when compared with the older models.

Microservices bring in a lot of advantages for handling the code, managing the servers that host them, and the confined section of business that it takes care of. But, it also brings new challenges.

Imagine this scenario - a set of microservices are integrated into an organization and they are dependent on some data within them. Now say there is a business flow that interacts with all the microservices at different instances of the flow. There can be connectivity to the same microservice multiple times. Everything will look fine until a problem arises.

For instance, if the service did not receive a proper response, then it becomes critical to figure out the instance and trace it out. However, tracing out written manual code over the entire business logic would be more cumbersome to maintain than the original code itself.

So, to observe the health, performance, and trace the application flow which can be independent of programming languages and when it comes to observability, metrics, events, logs, and traces play a key role in it.

Let's understand OpenTelemetry, a tool that can help in addressing the above-mentioned problems, and also on how to implement it in .Net.

Table of contents

  1. Introduction to OpenTelemetry
  2. Implementing OpenTelemetry in .Net
  3. Final Words

Introduction to OpenTelemetry

In general, Telemetry means a collection of data at a remote place and transmitting it to a different equipment/place for monitoring and tracking purposes. The same is applicable in the software industry as well. As the name states, OpenTelemetry is an open-source framework for enhancing observability. It provides vendor-agnostic, API, SDK, and supporting tools for collecting the data and offers support for understanding the inferences from the data.

It helps in managing the performances of complex distributed systems and assists the professionals by giving a better understanding of the systems’ behavior and performance. It collects logs, metrics, and traces, which are then exported to the backend systems for analysis. The stand-out features of OpenTelemetry include -

  • Its consistency in collecting the telemetry data
  • Backward compatibility with other observing tools
  • Streamlined observability.

Let us now see how to implement OpenTelemetry in a .Net project and what insights it provides.

Implementing OpenTelemetry in .Net

To understand and implement OpenTelemetry, we need two API projects so that we can check the tracking of requests and responses. We are going to make use of the default web API in one project and for another, we are going to make a few tweaks to add OpenTelemetry. We will also discuss how to examine it in the console window.

Create the default API project by using the ‘ASP.NET Core WebAPI’ template and provide a name for the solution. Now run the solution.

By default, the application opens in the browser with localhost and a port number. Make a note of this detail.

The next step is to create the second solution with the same project template and provide a valid name for the solution. Here, we are going to implement the OpenTelemetry and also the logic to call the first API.

After creating the second API solution, right-click on the project and go to Manage NuGet, and install the below NuGet packages.

  • OpenTelemetry
  • OpenTelemetry.Instrumentation.AspNetCore
  • OpenTelemetry.Instrumentation.Http
  • OpenTelemetry.Extensions.Hosting
  • OpenTelemetry.Exporter.Console

Note: Check the include pre-release checkbox in the NuGet package manager window. This is needed because some of the packages are in a pre-release state.

Now open the Startup.cs file and in the ConfigureServices method, add the below lines.

//This is for register IHttpClientFactory 
services.AddHttpClient();
//This is to register for OpenTelemetry
​      services.AddOpenTelemetryTracing((builder) =>
​      {
​        builder
​          .AddHttpClientInstrumentation()
​          .AddAspNetCoreInstrumentation()
​          .AddConsoleExporter();
​      });

The next step is to make changes in the weatherforecastcontroller.cs file to use the constructor injection of the IHttpClientFactory and create a client to access the first API.

Declare a global variable in the class - weatherforecastcontroller.cs -

private readonly IHttpClientFactory _client;

In the constructor parameter, add ‘IHttpClientFactory client’ along with existing parameters.

Inside the constructor, assign the client object.

_client = client;

The next step is to create a client. For the sake of simplicity, here we are creating the client within the Get method. After modifying the Get method, it looks like below.

public async Task<IEnumerable<WeatherForecast>> Get()
​    {
​      var client = _client.CreateClient("test"); 
​      var response = await client.GetAsync("https://localhost:4434/weatherforecast");
​      var rng = new Random();

​      return Enumerable.Range(1, 5).Select(index => new WeatherForecast

​      {
​        Date = DateTime.Now.AddDays(index),
​        TemperatureC = rng.Next(-20, 55),
​        Summary = Summaries[rng.Next(Summaries.Length)]
​      })
​      .ToArray();
​    }

Changes in the code are completed now. Let's change the output execution to Console from IIS Express. And if we run the solution, a console window pops up with https://localhost:5001 and http://localhost:5000.

Try hitting the URL https://localhost:5001/WeatherForecast.

Do you see the response in the browser window?

Now check the console window for OpenTelemetry’s work.

If you observe the result in the console window carefully, you will notice that initially, the swagger window was opened. Then if we notice, there is an event with activity id and activity parent id. This means it is a child event and it has the details of the API call inside the second API.

If we see further below, the event has an activity id that matches with the parent id of the previous event, indicating that the events are connected, which is the actual endpoint call of the second API.

Final Words

OpenTelemetry provides great benefits and data for analysis. The traces of the OpenTelemetry can also be exported to various other backend systems which provide visual interaction for it, for example - Jaeger.

Nieuwste