<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Motti Bechhofer</title>
    <description>The latest articles on DEV Community by Motti Bechhofer (@mottibec).</description>
    <link>https://dev.to/mottibec</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F630103%2Fecbf672e-f20d-41d7-8bc1-d7bb8207f854.jpeg</url>
      <title>DEV Community: Motti Bechhofer</title>
      <link>https://dev.to/mottibec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mottibec"/>
    <language>en</language>
    <item>
      <title>Guide to Distributed Tracing with OpenTelemetry Dotnet</title>
      <dc:creator>Motti Bechhofer</dc:creator>
      <pubDate>Wed, 25 Jan 2023 12:51:56 +0000</pubDate>
      <link>https://dev.to/aspecto/guide-to-distributed-tracing-with-opentelemetry-dotnet-1533</link>
      <guid>https://dev.to/aspecto/guide-to-distributed-tracing-with-opentelemetry-dotnet-1533</guid>
      <description>&lt;p&gt;In this OpenTelemetry dotnet guide, you will learn exactly how to set up a .Net project that leverages OpenTelemetry, from scratch, and without any prior knowledge of OpenTelemetry.&lt;/p&gt;

&lt;p&gt;We will build a simple to-do app that uses ASP.NET CORE and MongoDB. We will then use OpenTelemetry to generate spans, and send them to Aspecto for visualization.&lt;/p&gt;

&lt;p&gt;You can view the complete code on GitHub &lt;a href="https://github.com/aspecto-io/opentelemetry-examples/tree/master/dotnet" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  OpenTelemetry Overview
&lt;/h2&gt;

&lt;p&gt;If you're somewhat new to OpenTelemetry, here are the basics you need to know (you can also visit our &lt;a href="https://www.aspecto.io/opentelemetry-bootcamp/?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;OpenTelemetry Bootcamp&lt;/a&gt; if you want to become an OTel ninja).&lt;/p&gt;

&lt;p&gt;OpenTelemetry is a suite of APIs and SDKs that facilitate the collection, export, and generation of trace data, log data, and metrics (also known as the three pillars of observability). Led by the Cloud Native Computing Foundation (CNCF, the organization responsible for Kubernetes), we utilize OpenTelemetry to gather data from events and operations occurring within our system, enabling us to instrument our distributed services. &lt;/p&gt;

&lt;p&gt;This data is ultimately helpful in understanding and analyzing the behavior of our software, as well as troubleshooting any performance issues or errors.&lt;/p&gt;

&lt;p&gt;OpenTelemetry serves as a single library that captures all data under a single specification and transports it to a designated location (e.g., backend, collector, or open-source supporting tools). In this OpenTelemetry dotnet guide, there are a few key terms to understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Span&lt;/strong&gt;: A span represents an action or operation that occurs within our system. This can include an HTTP request or a database operation that takes place over a specific duration of time. Spans often have a parent-child relationship with other spans.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Trace&lt;/strong&gt;: Traces are essentially "call stacks" for distributed services, representing a tree of spans connected in a parent-child relationship. Traces outline the progression of requests across various services and components in our application (such as databases, data sources, queues, etc.). For example, sending an API call to the user service may result in a database query to the users database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/g-pQpGwQmgU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Exporter&lt;/strong&gt;: Once we create a span, we need to send it to a designated backend. This may be in memory, &lt;a href="https://www.aspecto.io/blog/jaeger-tracing-the-ultimate-guide/?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;Jaeger Tracing&lt;/a&gt;, or even output to the console. The exporter handles the process of sending the data to the backend.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Instrumentation&lt;/strong&gt;: Instrumentation libraries allow us to gather data and generate spans based on the libraries used in our applications, such as Kafka, Mongo, AspNetCore, etc. There are two approaches to instrumenting our application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;Auto instrumentation&lt;/em&gt;: Automatically creating spans from the libraries used in our application with the aid of ready-to-use OpenTelemetry libraries.&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Manual instrumentation&lt;/em&gt;: Manually adding code to our application to define the beginning and end of each span and the payload.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;For a more comprehensive understanding of OpenTelemetry terminology, visit the &lt;a href="https://opentelemetry.io/docs/concepts/signals/" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  OpenTelemetry Dotnet: Building Our Project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setting up .Net app
&lt;/h3&gt;

&lt;p&gt;As mentioned, we are going to build a to-do app that uses ASP.NET CORE and MongoDB. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here's how it's done:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1. Start by setting up a new web API project by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new webapi -o OpenTelemetryExample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2. We will create a simple to-do list application using MongoDB in this example. So let's install the Mongodb driver for dotnet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package MongoDB.Driver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3. Create our to-do model. Create a file named Todo.cs in a directory called Models. Our model will have an id and a description. So our code should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace OpenTelemetryExample.Models;

public class Todo
{
  [BsonElement("_id")]
  [BsonId]
  [BsonRepresentation(BsonType.ObjectId)]
  public string Id { get; set; } = null!;

  [BsonElement("title")]
  public string Title { get; set; } = null!;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4. Add our controller to the Controllers folder. Our controller will expose three endpoints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  POST /todo -- To create a new todo item &lt;/li&gt;
&lt;li&gt;  GET /todo -- To get all todos&lt;/li&gt;
&lt;li&gt;  GET/todo/:Id -- To get a specific todo item &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our controller should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using OpenTelemetryExample.Models;

namespace OpenTelemetryExample.Controllers;

[ApiController]
[Route("[controller]")]
public class TodoController : ControllerBase
{
  private readonly IMongoCollection&amp;lt;Todo&amp;gt; _todos;
  private readonly ILogger&amp;lt;TodoController&amp;gt; _logger;

  public TodoController(ILogger&amp;lt;TodoController&amp;gt; logger, IMongoDatabase database)
  {
      _logger = logger;
      _todos = database.GetCollection&amp;lt;Todo&amp;gt;("todos");
  }

  [HttpPost]
  public async Task&amp;lt;ActionResult&amp;lt;Todo&amp;gt;&amp;gt; PostTodo([FromBody] Todo todo)
  {
      await _todos.InsertOneAsync(todo);
      return CreatedAtAction(nameof(GetTodos), todo);
  }

  [HttpGet]
  public async Task&amp;lt;ActionResult&amp;lt;IEnumerable&amp;lt;Todo&amp;gt;&amp;gt;&amp;gt; GetTodos()
  {
      var todos = await _todos.Find(m =&amp;gt; true).ToListAsync();
      return Ok(todos);
  }

  [HttpGet("{id}")]
  public async Task&amp;lt;ActionResult&amp;lt;Todo&amp;gt;&amp;gt; GetTodoById(string id)
  {
      var todo = await _todos.Find(todo =&amp;gt; todo.Id == id).FirstAsync();
      return Ok(todo);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5. Register the Mongodb client in the dependency injection. Go to the program.cs file in the root directory:&lt;/p&gt;

&lt;p&gt;In the main method, we will add the following code to init Mongodb (we will run mongo on running on your local machine on &lt;em&gt;port 27017&lt;/em&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var mongoUrl = MongoUrl.Create("mongodb://localhost:27017");
var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
var mongoClient = new MongoClient(clientSettings);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6. Then, register it as a singleton.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddSingleton(mongoClient.GetDatabase("todo"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7. Now we can run our dotnet project with the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;8. And create a todo with a post request to &lt;em&gt;/todo&lt;/em&gt; and view it by making a get request to &lt;em&gt;/todo&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding OpenTelemetry and visualizing our Dotnet traces
&lt;/h3&gt;

&lt;p&gt;You are now familiar with the fundamental concepts of spans, traces, instrumentations, and generally how OpenTelemetry can be utilized to generate traces for Dotnet applications. Let's add OpenTelemetry to our project and send spans for visualization with Aspecto.&lt;/p&gt;

&lt;p&gt;Using Aspecto to visualize data is incredibly straightforward. If you wish to explore Aspecto for yourself, you can try the &lt;a href="https://www.aspecto.io/pricing/?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;free-forever&lt;/a&gt; plan which offers unlimited features including unlimited workspaces, user invites, sampling rules, and more. &lt;/p&gt;

&lt;p&gt;Consider playing with the &lt;a href="https://app.aspecto.io/play/search?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;Live Playground&lt;/a&gt; to gain a deeper understanding. &lt;/p&gt;

&lt;p&gt;Here's how you do it:&lt;/p&gt;

&lt;p&gt;1. Install the following dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting --prerelease
dotnet add package OpenTelemetry.Instrumentation.AspNetCore --prerelease
dotnet add package OpenTelemetry.Instrumentation.Http --prerelease
Dotnet add package MongoDB.Driver.Core.Extensions.DiagnosticSources
dotnet add package MongoDB.Driver.Core.Extensions.OpenTelemetry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will be exporting spans to Aspecto using this package we just installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2. Configure OpenTelemetry by going to the program.cs file and add the following code to the main method. Make sure to add your aspecto token to your ASPECTO_API_KEY environment variable (&lt;a href="https://app.aspecto.io/user/login?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;get it here&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        var serviceName = "your-service-name";
        var serviceVersion = "your-service-version";
builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =&amp;gt;
            tracerProviderBuilder
                .AddOtlpExporter(opt =&amp;gt;
                {
                    opt.Endpoint = new Uri("https://collector.aspecto.io:4317/v1/traces");
                    opt.Headers = $"Authorization={Environment.GetEnvironmentVariable("ASPECTO_API_KEY")}";
                })
                .AddSource(serviceName)
                .SetResourceBuilder(
                    ResourceBuilder.CreateDefault()
                        .AddService(serviceName: serviceName, serviceVersion: serviceVersion))
                .AddHttpClientInstrumentation()
                .AddAspNetCoreInstrumentation()
                .AddMongoDBInstrumentation()
        );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡Good to know -- If you wish to export traces to Jaeger, you should use the &lt;code&gt;AddJaegerExporter&lt;/code&gt; instead of the &lt;code&gt;AddOtlpExporter&lt;/code&gt;. Visit the opentelemetry-dotnet &lt;a href="https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Exporter.Jaeger/README.md" rel="noopener noreferrer"&gt;repository&lt;/a&gt; to see how it's done.&lt;/p&gt;

&lt;p&gt;3. Next, add this code to register the mongo db instrumentation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;clientSettings.ClusterConfigurator = cb =&amp;gt; cb.Subscribe(new DiagnosticsActivityEventSubscriber());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4. The complete program.cs file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using MongoDB.Driver;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;

internal class Program
{
    private static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        //Mongodb instrumentation config
        var mongoUrl = MongoUrl.Create("mongodb://localhost:27017");
        var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
        clientSettings.ClusterConfigurator = cb =&amp;gt; cb.Subscribe(new DiagnosticsActivityEventSubscriber());
        var mongoClient = new MongoClient(clientSettings);

        //OTel config
        var serviceName = "todo-dotnet";
        var serviceVersion = "1.0.0";
        builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =&amp;gt;
            tracerProviderBuilder
                .AddOtlpExporter(opt =&amp;gt;
                {
                    opt.Endpoint = new Uri("https://collector.aspecto.io:4317/v1/traces");
                    opt.Headers = $"Authorization={Environment.GetEnvironmentVariable("ASPECTO_API_KEY")}";
                })
                .AddSource(serviceName)
                .SetResourceBuilder(
                    ResourceBuilder.CreateDefault()
                        .AddService(serviceName: serviceName, serviceVersion: serviceVersion))
                .AddHttpClientInstrumentation()
                .AddAspNetCoreInstrumentation()
                .AddMongoDBInstrumentation()
        );

        //dependency injection config
        builder.Services.AddControllers();
        builder.Services.AddSingleton(mongoClient.GetDatabase("todo"));
        var app = builder.Build();
        app.MapControllers();
        app.Run();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you should be able to run your application and view the traces in Aspecto.&lt;/p&gt;

&lt;p&gt;Run Mongo locally. You can do it with docker.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 27017:27017 --name mongo -d mongo:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The end result should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh3.googleusercontent.com%2F1sU45IHvyNSzbuUZ00H0pRJ4XO5tPPNa3Rq1caGUVlGwXpE-LdpF2-fVX8P0fdqTvaPYm7ctfv45ox6Yol8T3XjXssQ7fBEbI7gOhuRlj5uoUzHB9WS7K6S-D1QPtbBiY8Eb14CDeQUzpukEP8PZtagxRam_qyNXj-zNai2iZ9doLjvueMdG-_V4JxIvHw" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh3.googleusercontent.com%2F1sU45IHvyNSzbuUZ00H0pRJ4XO5tPPNa3Rq1caGUVlGwXpE-LdpF2-fVX8P0fdqTvaPYm7ctfv45ox6Yol8T3XjXssQ7fBEbI7gOhuRlj5uoUzHB9WS7K6S-D1QPtbBiY8Eb14CDeQUzpukEP8PZtagxRam_qyNXj-zNai2iZ9doLjvueMdG-_V4JxIvHw" alt="Aspecto platform OpenTelemetry dotnet traces visualization" width="1600" height="845"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced OpenTelemetry Learning
&lt;/h2&gt;

&lt;p&gt;If you want to learn more about OpenTelemetry and become an OTel ninja, check out this free, 6-episode, OpenTelemetry &lt;a href="https://www.aspecto.io/opentelemetry-bootcamp/?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;Bootcamp&lt;/a&gt; (vendor-neutral).&lt;/p&gt;

&lt;p&gt;This is your OpenTelemetry &lt;a href="https://www.aspecto.io/opentelemetry-bootcamp/?utm_source=post&amp;amp;utm_medium=devto&amp;amp;utm_campaign=OpenTelemetry-dotnet" rel="noopener noreferrer"&gt;playbook&lt;/a&gt; where you will learn everything, from the very basics all the way to running OTel in production. Including how to manage cost, sampling, messaging systems, and much more.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Episode 1: OpenTelemetry Fundamentals&lt;/li&gt;
&lt;li&gt; Episode 2: Integrate Your Code (logs, metrics, and traces)&lt;/li&gt;
&lt;li&gt; Episode 3: Deploy to Production + Collector&lt;/li&gt;
&lt;li&gt; Episode 4: Sampling and Dealing with High Volumes&lt;/li&gt;
&lt;li&gt; Episode 5: Custom Instrumentation&lt;/li&gt;
&lt;li&gt; Episode 6: Testing with OpenTelemetry&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>gratitude</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Instrument your node.js socket.io apps with OpenTelemetry like a PRO 🛠️</title>
      <dc:creator>Motti Bechhofer</dc:creator>
      <pubDate>Thu, 03 Jun 2021 11:19:48 +0000</pubDate>
      <link>https://dev.to/aspecto/instrument-your-node-js-socket-io-apps-with-opentelemetry-like-a-pro-d5p</link>
      <guid>https://dev.to/aspecto/instrument-your-node-js-socket-io-apps-with-opentelemetry-like-a-pro-d5p</guid>
      <description>&lt;p&gt;Today we released a first-of-its-kind open-telemetry auto instrumentation plugin.&lt;br&gt;
A plugin for socket.io instrumentation.&lt;/p&gt;
&lt;h2&gt;
  
  
  What makes this instrumentation different?
&lt;/h2&gt;

&lt;p&gt;Well, first, it's the first full instrumentation for socket.io ever written. &lt;br&gt;
Second, there are no open-telemetry specs for socket.io or WebSocket; usually, at &lt;a href="http://aspecto.io" rel="noopener noreferrer"&gt;aspecto&lt;/a&gt;, we try to make our instrumentation as close to the specs as possible, but in this case, we had to improvise. &lt;/p&gt;
&lt;h2&gt;
  
  
  This is what we choose to do:
&lt;/h2&gt;

&lt;p&gt;it seemed reasonable to categorized socket.io under the messaging specs since we have a producer (emit) and a receiver (on), and a message.&lt;br&gt;
Having looked at how other messaging systems with unique characteristics like Kafka do it, we added some custom attributes to the socket.io traces.&lt;br&gt;
&lt;code&gt;messaging.socket.io.event_name&lt;/code&gt; with the event name,&lt;br&gt;
&lt;code&gt;messaging.socket.io.rooms&lt;/code&gt; with an array of socket.io rooms&lt;br&gt;
&lt;code&gt;messaging.socket.io.namespace&lt;/code&gt; with the namespace&lt;/p&gt;
&lt;h2&gt;
  
  
  So what do you need to do?
&lt;/h2&gt;

&lt;p&gt;First, create a node.js socket.io app, (need one? &lt;a href="https://github.com/mottibec/stocker" rel="noopener noreferrer"&gt;we got you&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;now for the fun stuff&lt;br&gt;
install open telemetry in your app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install \
  @opentelemetry/core \
  @opentelemetry/node \
  @opentelemetry/tracing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now install our socket.io plugin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i opentelemetry-instrumentation-socket.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, this is the most essential part. Since the plugin patches socket.io, you must init it before any module requires it, so we're going to put our initialization code at the top of the &lt;code&gt;index.ts&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;first, let's import the files&lt;br&gt;
it should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NodeTracerProvider } from "@opentelemetry/node";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { SocketIoInstrumentation } from "opentelemetry-instrumentation-socket.io"; 
import { ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/tracing";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and to the exciting part, register our socket.io plugin&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const provider = new NodeTracerProvider();
registerInstrumentations({
  tracerProvider: provider,
  instrumentations: [new SocketIoInstrumentation()],
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and some bla bla code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we are done!&lt;/p&gt;

&lt;p&gt;The complete initialization code should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NodeTracerProvider } from "@opentelemetry/node";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { SocketIoInstrumentation } from "opentelemetry-instrumentation-socket.io";
import { ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/tracing";

const provider = new NodeTracerProvider();
registerInstrumentations({
  tracerProvider: provider,
  instrumentations: [new SocketIoInstrumentation()],
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can run the app and see the traces in the console thanks to the &lt;code&gt;ConsoleSpanExporter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is an example trace from &lt;a href="https://github.com/mottibec/stocker" rel="noopener noreferrer"&gt;stocker&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  traceId: 'e05c479e3d3c7980a1290ef8a8c1d669',
  parentId: undefined,
  name: '/[AAPL] send',
  id: '327a29bf1d58469c',
  kind: 3,
  timestamp: 1622705582441890,
  duration: 428,
  attributes: {
    'messaging.system': 'socket.io',
    'messaging.destination_kind': 'topic',
    'messaging.socket.io.event_name': 'price-update',
    'messaging.socket.io.rooms': [ 'AAPL' ],
    'messaging.socket.io.namespace': '/',
    'messaging.destination': '/'
  },
  status: { code: 0 },
  events: []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find the source code on &lt;a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-socket.io" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Isn't that cool? 😎&lt;/p&gt;

&lt;p&gt;Here’s how we visualize Socket.io traces in &lt;a href="https://www.aspecto.io/?utm_source=dev.to&amp;amp;utm_medium=post&amp;amp;utm_campaign=socket-io-instrumentation"&gt;Aspecto&lt;/a&gt; 🤯&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmx1s9yvqd8hlertg4bcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmx1s9yvqd8hlertg4bcy.png" alt="aspecto platform screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out our other awesome &lt;a href="https://github.com/aspecto-io/opentelemetry-ext-js" rel="noopener noreferrer"&gt;instrumentations&lt;/a&gt; made with ❤️ including &lt;a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-kafkajs" rel="noopener noreferrer"&gt;Kafka&lt;/a&gt; and &lt;a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-aws-sdk" rel="noopener noreferrer"&gt;aws-sdk&lt;/a&gt; instrumentations.&lt;/p&gt;

&lt;p&gt;Feel free to &lt;a href="https://www.aspecto.io/contact-us/" rel="noopener noreferrer"&gt;reach out to us&lt;/a&gt; with any feedback ✌️&lt;/p&gt;

&lt;h1&gt;
  
  
  About Aspecto
&lt;/h1&gt;

&lt;p&gt;Aspecto is an OpenTelemetry-based troubleshooting platform that helps developers prevent distributed application issues from their local dev environment and across the entire development cycle.&lt;br&gt;
You can think of it as the Chrome DevTools for your distributed applications.&lt;br&gt;
Aspecto is used for detecting and troubleshooting microservices-based distributed systems, and preventing software failures before deployment.&lt;/p&gt;

</description>
      <category>socketio</category>
      <category>webdev</category>
      <category>showdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
