<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amir B on Medium]]></title>
        <description><![CDATA[Stories by Amir B on Medium]]></description>
        <link>https://medium.com/@amirgiraffe?source=rss-32a7e1156d10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*I9LrOMgRsmVDHsq4.jpg</url>
            <title>Stories by Amir B on Medium</title>
            <link>https://medium.com/@amirgiraffe?source=rss-32a7e1156d10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 20:31:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amirgiraffe/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why and How to Run Jaeger Locally]]></title>
            <link>https://medium.com/aspecto/why-and-how-to-run-jaeger-locally-6b8fac37e8fd?source=rss-32a7e1156d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b8fac37e8fd</guid>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[jaeger]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Amir B]]></dc:creator>
            <pubDate>Mon, 28 Jun 2021 08:05:03 GMT</pubDate>
            <atom:updated>2021-07-08T13:46:00.802Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, you’ll learn how to run Jaeger locally, why and when you should do it, as well as what are Jaeger’s limitations when running locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rqna0xHz1zK8LBA-WoCquw.png" /></figure><p>Let’s start with the basics: a distributed tracing system is generally composed of client and backend components.</p><p>I will touch briefly on client components, though most of this post is about backend components.</p><h3>Client Components</h3><p>The client part is usually a set of libraries installed inside an application which “instrument” it — generating a “span” object for each interesting event happening in runtime inside the service.</p><p>A modern and recommended open-source client SDK that does that is <a href="https://opentelemetry.io/">OpenTelemetry</a>.</p><p>Spans on the client alone are meaningless — they need to be accessible to a person who consumes them. Consumers are usually dev-ops teams monitoring a system or developers maintaining the system and adding new features.</p><h4>Trace Usages</h4><p>There are many ways in which collected trace data can be used and provide value. These are the common ones:</p><ul><li>Aggregate spans to trace — group all <strong>spans</strong> (events) which are part of the same <strong>trace</strong> (logical operation) arriving from <strong>different distributed services</strong>, into a single entity</li><li>Query the collected data (show me all traces in the last hour starting at endpoint GET /users in service X)</li><li>Visualize the data — usually in a graph, or timeline</li><li>Find errors (exceptions, 500s, etc) and investigate their root-cause</li><li>Investigate performance bottlenecks</li></ul><h3>Backend Components</h3><p>To fulfill the requirements above, we need to set up backend components.</p><p>They are used to collect spans from client components, process them, store them in a database, expose an API for the data and UI to view traces and perform queries.</p><p><a href="https://www.jaegertracing.io/">Jaeger</a> (a <a href="https://www.cncf.io/">CNCF</a> graduated project) is a popular open-source project with backend components that does that and is easy to set up.</p><p>To use Jaeger in production, it is recommended to install it in a cloud environment with load-balancing, auto-scaling, replications, and all that jazz.</p><p>However, it is sometimes enough to just run it locally in a lightweight and simple setup.</p><h3>Running Locally</h3><p><a href="https://www.jaegertracing.io/docs/1.23/getting-started/">The recommended</a> approach for running Jaeger backend locally is to use docker:</p><pre>$ docker run -d --name jaeger \<br>  -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \<br>  -p 5775:5775/udp \<br>  -p 6831:6831/udp \<br>  -p 6832:6832/udp \<br>  -p 5778:5778 \<br>  -p 16686:16686 \<br>  -p 14268:14268 \<br>  -p 14250:14250 \<br>  -p 9411:9411 \<br>  jaegertracing/all-in-one:1.23</pre><p>And access the UI in <a href="http://localhost:16686/">http://localhost:16686</a>.</p><p>You can then configure an Opentelemetry Client SDK installation or <a href="https://github.com/open-telemetry/opentelemetry-collector">OpenTelemetry Collector</a> to use Jaeger exporter and send trace data to this local Jaeger.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8f6c6d6d63c5a1c244f0dc8fa85ce08/href">https://medium.com/media/f8f6c6d6d63c5a1c244f0dc8fa85ce08/href</a></iframe><h3>Running Local Jaeger: Benefits</h3><h4>Faster Debugging</h4><p>If you work on a service codebase (e.g., fixing a bug, developing a new feature, or implementing integration to other services/databases/messaging systems, etc.), most likely that this is what you do:</p><ol><li>You start an instance of the service on your local dev station</li><li>Send traffic to it to test your changes</li><li>Validate the behavior you were expecting</li></ol><p>By instrumenting it locally, you can debug development issues faster.</p><p>For example: find the point in your app where an error occurred with less logging to console, breakpoints, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QGRN3kSu5FcnxXhvKTk9_Q.png" /><figcaption>An example of a trace in Jaeger showing error while accessing Redis key as a list</figcaption></figure><h4>Running Tests</h4><p>When running your integration test suite locally — if a test fails, it can sometimes be easier to understand what went wrong by examining it in Jaeger UI, where you can view highlighted errors and events organized into a hierarchical structure.</p><h4>Instrumentation Development</h4><p>If you are writing a new instrumentation library, observing the tracing output in a UI can be much easier than browsing through textual logs. You can browse the <a href="https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation">JS instrumentation package</a> for more info and examples.</p><h3>Running Local Jager: Limitations</h3><p>Jaeger is free, relatively easy to set up, and will do a good job for most basic setups and tracing needs.</p><p>The UI and features set are quite basic and you may quickly find yourself in the need of <a href="https://www.aspecto.io/compare-jaeger-aspecto/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=why-and-how-to-run-jaeger-locally"><strong>more advanced features</strong></a>.</p><p><a href="https://www.aspecto.io/compare-jaeger-aspecto/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=why-and-how-to-run-jaeger-locally">Other alternatives</a> can give value and increase development productivity in the following cases:</p><h4>Lack of End to End Visibility in Async Messaging</h4><p>When using async messaging systems, generally, there are two cases for traces.</p><p>The first one is when the message broker generates one trace. Jaeger does a great job of displaying us with that one trace.</p><p>The second case, common in batch processing scenarios, is when the senders and receivers in message brokers like Kafka and AWS SQS generate multiple traces (for example, each receive starts a new trace). In this case, Jaeger will display these traces separately. That makes it more complicated to track and debug complex transactions.</p><p>More advanced backends might have an out-of-the-box solution for that and will <a href="https://www.aspecto.io/blog/how-to-achieve-end-to-end-microservices-visibility-in-asyn-messaging-with-opentelemetry/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=why-and-how-to-run-jaeger-locally">detect it and merge those traces into a single logical flow</a>.</p><h4>Cross Environment Traces</h4><p>If your organization works with Jaeger in production, and let’s say you want to use Jaeger to do your tests, sending your local traces to the production Jaeger is highly suboptimal.</p><p>Not only can it pollute the production environment, but it makes it difficult to find your traces within this trace jungle. By running Jaeger locally, you get an isolated playground for your tests and development.</p><p>However, one major pitfall in this scenario is that your local Jaeger will show only the part of the trace generated from your local dev station. It means you lose the context of how your traces communicate and affect downstream and upstream services (i.e., production and staging).</p><p>In this case, <strong>you won’t have an isolated and local development session while seeing the full effect of your changes</strong> across the different environments.</p><h4>Advanced Search</h4><p>Free text search on all data or based on trace attributes. For example, if you want to search a token in the payload within specific traces.</p><h4>Trace Data Processing and Insights</h4><p>Jaeger presents raw traces and highlights errors, however, generating insights based only on that data isn’t trivial and quite complex.</p><p>Examples for such insights can be API breaking change detection, aggregation of traces based on structure, parameter journey in a trace, dependency analysis, comparison to baseline from production or staging, etc.</p><h4>Enhanced UI</h4><p>Jaeger UI enumerates all attributes for a span in a long list. It does not group or organizes related data, show JSON content in a tree, highlight common data like HTTP status code, and other good stuff that makes our life easier.</p><h3>The Bottom Line</h3><p>Running local Jaeger offers great benefits. It is easy to set up, and when it comes to faster debug and running tests, you get this extra confidence you might want when working locally.</p><p>Jaeger is a great tool and does an awesome job answering your basic tracing needs.</p><p>However, when your work with services gets a bit more complex or when you want to actually boost productivity when working locally, the scope of Jaeger is narrow.</p><p>If the limitations I mentioned above are a deal-breaker for you (if not now, they might be in the future), there are several vendors in the market, supplying various solutions which enhance your tracing-based workflow. One that can help you overcome all these issues is <a href="https://www.aspecto.io/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=why-and-how-to-run-jaeger-locally"><strong>Aspecto</strong></a>.<br> <br>Aspecto gives you everything you get with Jaeger but with enhanced UI, search, and troubleshooting capabilities for <a href="https://www.aspecto.io/product/local-debugging/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=why-and-how-to-run-jaeger-locally">local development and debugging</a>. It takes 2 minutes to <a href="https://www.aspecto.io/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=why-and-how-to-run-jaeger-locally">get started</a> with, it’s free and OpenTelemetry-based. Think of it as Jaeger and Chrome DevTools fusion for your distributed applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*4kibn-TmgXipcOkWfDqMnw.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b8fac37e8fd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/why-and-how-to-run-jaeger-locally-6b8fac37e8fd">Why and How to Run Jaeger Locally</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Protobuf.js vs JSON.stringify Performance]]></title>
            <link>https://medium.com/aspecto/protobuf-js-vs-json-stringify-performance-d3809355ff3e?source=rss-32a7e1156d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3809355ff3e</guid>
            <category><![CDATA[encoding]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[protobuf]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Amir B]]></dc:creator>
            <pubDate>Thu, 08 Apr 2021 12:21:17 GMT</pubDate>
            <atom:updated>2021-04-08T12:21:17.975Z</atom:updated>
            <content:encoded><![CDATA[<p><em>TL;DR — encoding and decoding string-intensive data in JavaScript is faster with JSON than it is with protobuf.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xif5JsWeO6PpKMCbcKYWZg.png" /></figure><p>When you have structured data in JavaScript, which needs to be sent over the network (for another microservice for example) or saved into a storage system, it first needs to be serialized. The serialization process converts the data object you have in the JavaScript program memory, into a buffer of bytes, which can then be deserialized back into a JavaScript object.</p><p>Two popular serialization methods are JSON and Google Protocol Buffers (protobuf)</p><h3>JSON</h3><p>Serializing data to JSON is as easy as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68c4ddabe37ac6aeb15dc7075407d481/href">https://medium.com/media/68c4ddabe37ac6aeb15dc7075407d481/href</a></iframe><h3>protobuf.js</h3><p>Google Protocol Buffers is a method of serializing structure data that is based on a scheme (written in .proto file). Example of how to serialize the previous payload to Protobuf with the protobufjs package:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aca616454709f3a00baa4081b2a0cf6/href">https://medium.com/media/5aca616454709f3a00baa4081b2a0cf6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b43315eba811506fe5bf68ed5fbcf02/href">https://medium.com/media/8b43315eba811506fe5bf68ed5fbcf02/href</a></iframe><p>You can see that the generated output is only 7 bytes long, much less than the 23 bytes we got on JSON serialization. Protobuf is able to serialize data so compactly mainly because it does not need to embed the field names as text in the data, possibly many times (“name” and “age” in the example are replaced by short descriptors of 2 bytes).</p><h3>Picking the Right Format</h3><p>Choosing the right serialization format that works best for you is a task that involves multiple factors.</p><p>JSON is usually easier to debug (the serialized format is human-readable), and easier to work with (no need to define message types, compile them, install additional libraries etc).</p><p>Protobuf on the other hand usually compresses data better and has built-in protocol documentation via the schema.</p><p>Another important factor is the CPU performance — the time it takes for the library to serialize and deserializes a message. In this article, we want to compare just the performance in JavaScript. You might eventually choose a format that is less performant but delivers value in other factors, or performance might be a big issue for you, in which case — keep reading.</p><h3>Encode Performance</h3><p>At <a href="https://www.aspecto.io/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=protobuf-vs-json">Aspecto</a>, we wrote an SDK that collects trace events and exports them to an open-telemetry collector. The data is formatted as JSON and sent over http. The exporter and collector can also communicate in protobuf using the protobufjs library.</p><p>Since the protobuf format is so compressed, we might be tempted to think that encoding to protobuf requires less CPU, which is measured as number of operations (encode/decode) in a second. A quick google search on the topic strengthen this thesis, and the <a href="https://github.com/protobufjs/protobuf.js#performance">Performance Section in protobufjs documentation</a> led us to replace our SDK exporter from JSON to protobuf payload, believing we will get better performance.</p><h4>Actual Performance</h4><p>After changing from JSON serialization to protobuf serialization, we run our SDK benchmark and surprisingly found that performance <strong>decreased</strong>. That observation, which we first believed was a mistake, sent us to further investigate the issue.</p><h4>Benchmarking — baseline</h4><p>First of all, we run the original <a href="https://github.com/protobufjs/protobuf.js/tree/master/bench">benchmark of protobufjs library</a>, to get a solid ground to start from. Indeed we got results similar to the library README:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e479a069c870b199757f402d20e65f67/href">https://medium.com/media/e479a069c870b199757f402d20e65f67/href</a></iframe><p>These results show that protobuf.js performance is better than JSON, which was in contrast to our previous observation.</p><h4>Benchmark — telemetry data</h4><p>We then modified the benchmark to encode our example data which is an <a href="https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/trace/v1/trace.proto">opentelemetry trace data</a>. We copied the proto files and data to the benchmark, and got the following results:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33d4f8e7d6152a2821df70666f4382d8/href">https://medium.com/media/33d4f8e7d6152a2821df70666f4382d8/href</a></iframe><p>These were the results we expected — for this data, protobuf is actually slower than JSON.</p><h4>Benchmark — strings</h4><p>We got two results for two different data schemas. In one - protobufjs is faster, and in the second — JSON is faster. Looking at the schemas, the immediate suspect was the number of strings. Our schemas are composed almost entirely of strings. So we created a third test, populating a simple schema with many many many strings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/800f4f09696d98aa6bf9004337cb52af/href">https://medium.com/media/800f4f09696d98aa6bf9004337cb52af/href</a></iframe><p>We run the benchmark with this payload (10,000 strings, of length 10 each)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8381fc4d8eb8b4ef91f9d74c9019c86/href">https://medium.com/media/b8381fc4d8eb8b4ef91f9d74c9019c86/href</a></iframe><p>And the results proved our suspicion:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6629fea3a07cca41dacb4a1f9abb18a/href">https://medium.com/media/b6629fea3a07cca41dacb4a1f9abb18a/href</a></iframe><p>When your data is composed of many strings, protobuf performance in JavaScript drops below those of JSON. It might be related to JSON.stringify function being implemented in C++ inside V8 engine, and highly optimized compared to the JS implementation of protobufjs.</p><h4>Decoding</h4><p>The benchmarks above are for encoding (serializing). The benchmarks results for decoding (deserializing) are similar.</p><h3>Conclusion</h3><p>If you have the time, our recommendation is to profile your common data, understand the expected performance of each option, and choose the format that works best for your needs.</p><p>It is important to be aware that protobuf is not necessarily the fastest option. If your data is mainly string, then JSON format might be a good choice.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3809355ff3e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/protobuf-js-vs-json-stringify-performance-d3809355ff3e">Protobuf.js vs JSON.stringify Performance</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ts-node RAM consumption]]></title>
            <link>https://medium.com/aspecto/ts-node-ram-consumption-12c257e09e13?source=rss-32a7e1156d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/12c257e09e13</guid>
            <category><![CDATA[memory-improvement]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[ts-node]]></category>
            <dc:creator><![CDATA[Amir B]]></dc:creator>
            <pubDate>Wed, 14 Oct 2020 13:40:50 GMT</pubDate>
            <atom:updated>2020-10-14T13:40:50.462Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR — if you use ts-node or ts-node-dev and care about RAM usage, use --tranpileOnly or tsc directly to reduce it by X6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BMNCL9DUv4SXKZZy" /><figcaption>Photo by <a href="https://unsplash.com/@harrisonbroadbent?utm_source=medium&amp;utm_medium=referral">Harrison Broadbent</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>It turns out that runningts-node-dev / ts-node is constantly consuming hundreds of megabytes of RAM even for small and simple applications. In development, it is usually not a big concern, but it can be if your application is running inside a docker container with limited resources (for example, with Docker Desktop on Mac which allocates by default only 2GB of RAM to all the containers in total).</p><p>TypeScript code should be transpiled to Javascript. This can be done before running the process (tsc) or in runtime (ts-node). The most efficient way is transpiling before running, however, this is very not developer-friendly as it takes forever.ts-node-devloads everything into memory, watches the changes the developer is making and transpiles the project fast on every change.</p><p>I encountered the issue while building a demo application to showcase our product at <a href="https://aspecto.io/?utm_source=meduim&amp;utm_medium=post&amp;utm_campaign=ts-node-memory-usage">Aspecto</a>. I run multiple typescript services with docker-compose, and begun to see arbitrary ts-node-dev processes exiting without even running the application with the message “Done in 79.06s”. This was due to a lack of memory - each typescript service was using ~600MB of RAM out of the total 2GB available for all containers.</p><p>After digging a bit, I found a few possible solutions and wanted to share them:</p><h4>Run ts-node-dev with option --transpile-only</h4><p>In my case, adding the --transpile-only option to ts-node-dev reduced the consumed RAM from ~600MB to ~170MB.</p><p>The price is that, well, the typescript code will only be transpiled, and typechecking will be skipped. Most modern IDEs (vscode, web storm), has built-in typescript IntelliSense which highlights errors, so for me, it was a fair price to pay.</p><p>If you use ts-node to run code in production which was already successfully compiled and tested in the CI, you can only benefit from setting this option.</p><h4>Compile the code with tsc and monitor file changes with nodemon</h4><p>Instead of using ts-node-dev which consumes a lot of memory, it is possible to compile the application directly with tsc and then run it from dist/build like this: node dist/index.js . For automatic reload on source file changes, nodemon / node-dev can be used. This is my “start” script in package.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d32bc3ce1273cb9ff472f6454d85da3/href">https://medium.com/media/3d32bc3ce1273cb9ff472f6454d85da3/href</a></iframe><p>This approach reduced the RAM on my service from ~600MB to ~95MB (but there is still a spike in RAM to 600Mb for few seconds while tsc is compiling).</p><p>Unlink the previous option, this approach WILL check for typescript errors and warnings, and the service will not start if errors exist in the code.</p><p>The price to pay here is a longer compilation time. In my setup, it’s about 10 seconds from saving the file until the service restarts.</p><h4>Increase Docker Desktop Available RAM</h4><p>This is the easiest fix. Just allocate more Memory to Docker Desktop by going to Preferences =&gt; Resources =&gt; Memory, and increase the value.</p><p>While it fixes the immediate problem, the containers will still consume a lot of memory, and if you have plenty of them, it might be a problem soon enough.</p><p>Also, changing the default configuration should be done by every user that wants to run the system with docker-compose, which introduces complexity in installation and usage.</p><h3>Summary</h3><ul><li>If memory consumption is not an issue for you (most cases), just use ts-node in production and ts-node-dev in development.</li><li>If you care about memory, then you have a tradeoff between fast restart time after modifications but typechecking only in the IDE (set --transpileOnly), or have typechecking in compilation, but slower restart on each modification (directly use tsc and nodemon / node-dev )</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12c257e09e13" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/ts-node-ram-consumption-12c257e09e13">ts-node RAM consumption</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Context Managment in OpenTelemetry NodeJS]]></title>
            <link>https://medium.com/aspecto/context-managment-in-opentelemetry-nodejs-e5cddf22a09e?source=rss-32a7e1156d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5cddf22a09e</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Amir B]]></dc:creator>
            <pubDate>Mon, 21 Sep 2020 11:51:43 GMT</pubDate>
            <atom:updated>2021-03-09T11:39:29.503Z</atom:updated>
            <content:encoded><![CDATA[<h3>Context Management in OpenTelemetry NodeJS</h3><p>This post shares some of our experience gained by writing nodejs plugins and debugging context issues at <a href="https://www.aspecto.io/?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=context-management-opentelemetry-nodejs">Aspecto</a>.</p><p>You might find it interesting if:</p><ul><li>You are developing an instrumentation plugin for opentelemetry in node.</li><li>You get broken-traces or the structure of the trace tree is not as expected.</li><li>You would like to have a deeper understanding of how the magic works under the hood.</li></ul><p>So you started using OpenTelemetry in your javascript application to collect traces. Since you are reading an article about context management, I assume that you already read and understand what OpenTelemetry is all about and the basics of how to setup tracing for your application. You probably already run some example application like the <a href="https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/basic-tracer-node">basic-tracer-node</a> under the “examples” directory in <a href="https://github.com/open-telemetry/opentelemetry-js">opentelemetry-js</a> repository, and got a trace in Jaeger:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*WY43qQJMa4KxYiP_7Hteyg.png" /></figure><p>In the trace above we have one main span, with 10 doWork child spans. This child-parent connection between main and doWork is what we want to talk about - how doWork knows to link to its parent?</p><h3>What is Span Context</h3><p>A trace is a collection of spans, with some relation between the spans (parent &lt;&gt; children). In the example above we have 11 different spans which all belong to the same trace.</p><p>In OpenTelemetry, each span is handled autonomously — it starts, ends, and processed orthogonally to other spans. Jaeger received each of the above spans individually and might receive many other spans from other traces in between. Since those traces share the same traceId, Jaeger can fuse them into one trace. It draws the spans as a tree because each span record the span id of it’s parent. We’ll call these two properties SpanContext. They are captured each time a span is created via the tracer.startSpan() function.</p><p>Span context is the glue that connects those separate spans into a trace. The SpanContext interface is defined in @opentelemetry/api:</p><pre>export interface SpanContext {<br>   traceId: string;<br>   spanId: string;<br>   // and few other context attributes<br>}</pre><h3>The Goal</h3><p>The goal is to have a complete, well-structured trace tree.</p><p>If everything works properly, all you need to do is to install open telemetry plugins for the libraries you are using and want to auto instrument, setup and configure OpenTelemetry tracing, and should not bother yourself with context.</p><p>When things don’t work well (for various reasons), you might encounter:</p><ul><li>Broken traces — where you expect to see one trace for a single execution, but receive multiple traces where each one is a sub-part of the full trace. For example: If your application receives an http call and writes something to the database, and you get two traces: one with http span, and another with database span.</li><li>Wrong tree hierarchy — when you logically expect a span A to be the child of some span B, but instead it resides under another span C, giving the impression that the flow of the code is different from what it really is.</li></ul><h3>How a Span Gets its Context?</h3><p>The most straightforward way to link a span to its parent is to set the parent option on SpanOptionswhen starting the span:</p><pre>const span = tracer.startSpan(&#39;doWork&#39;, { parent });</pre><p>The parentspan stores its own id and the trace id it belongs to, which becomes the parentSpanId and traceId of the new span. This method is sometimes useful, but it doesn’t fit for general instrumentation plugins which patch module functions and cannot usually pass around the parent span object to other plugins or places where it is needed.</p><p>Let&#39;s illustrate with an example - we’ll take the basic-trace-node example that we used earlier and change it a bit:</p><ul><li>Replace the doWork function with a more realistic example that uses async-await semantic and axios to GETsome data from an external HTTP endpoint.</li><li>Install the https plugin, so outgoing HTTP traffic from thedoWork function will create a span per request.</li><li>Reduce the work spans from 10 to 2, so Jaeger will be less noisy, and await for there completion with `Promise.all()`</li></ul><p>The code after those modifications becomes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90f5ee7248bef988289cc19f2a4970e1/href">https://medium.com/media/90f5ee7248bef988289cc19f2a4970e1/href</a></iframe><p>If we run the above example and check Jaeger, we’ll see broken-traces — there are 3 traces instead of one! Our old friend “main” span as one-span trace, and two additional traces with single-span each, for the two HTTP operations. The issue is that the two HTTP spans were created internally in the @opentelemetry/https plugin and can’t tell their parent is “main” span. To fix the issue, we need to set a scope for the “main” span with the `tracer.withSpan` function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c316df11e1870fd1522c0e5711bd813/href">https://medium.com/media/2c316df11e1870fd1522c0e5711bd813/href</a></iframe><p>ThewithSpan function executes its callback parameter in such a way that when a new span is started inside that function or one of the cascading sync or async calls, the new span context is set correctly to parentSpan. Since we called doWork() from within the scope of withSpan , and it calls axios.get() which calls the instrumented https module - the HTTP spans are now created with the right traceId and parentSpan:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*1MJ-zL9X-pY3fjiT6G1_BQ.png" /></figure><p>We can see one complete trace, as well as that the two requests are executed simultaneously since they run with Promise.all and async function. We fixed the broken-traces issue by setting the scope for the span.</p><h3>ContextManager</h3><p>ContextManager is a global, singleton service of the OpenTelemetry js framework which tracks the currently active context at any time. It is used by the tracer to query for the current active Context using the active()function, and to set scoped Context for following function calls using the with() and bind() functions.</p><h3>AsyncHooksContextManager</h3><p>OpenTelemetry will take care of setting the correct context for any function called from inside the with() scope, even for async function and promises which start new spans after being awaited.</p><p>For example, the following code will produce a correct trace where the three HTTP spans are children of the main span:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f0d8142a2309966a4e9be0f40561799/href">https://medium.com/media/8f0d8142a2309966a4e9be0f40561799/href</a></iframe><p>The context will propagate correctly to the timer callback being executed after 1 second, to the awaited `axios.get` call, and to the non awaited, second axios.get call. This is done by subscribing to various lifecycle events of asynchronous resources using the <a href="https://nodejs.org/api/async_hooks.html">async_hooks</a> node module.</p><h3>Debugging</h3><p>If you are writing a plugin, or need to debug an issue with span context not being set correctly, here are some tips from our experience at Aspecto:</p><ul><li>To see the current active context, you can examine the return value from opentelemetry.context.active() imported as const opentelemetry = require(&quot;@opentelemetry/api&quot;) . This function will return a Context object, which is an empty object if there is no active context, and object with key such as “Symbol(OpenTelemetry Context Key ACTIVE_SPAN)” if a context is set.</li><li>If the code is using `thenables` objects (objects that has a then()method, and behave like a promise, but are not a real promise), then due to a bug in V8, context will not propagate across the thenable call. The following example code will create child_span as a new trace, and will not link it correctly under the main parent, even though it is awaited from inside the withSpan call.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9ef0ac336848a29d8e55eaa5b051b2a/href">https://medium.com/media/c9ef0ac336848a29d8e55eaa5b051b2a/href</a></iframe><ul><li>The withSpan function is a common practice in js plugins. It scopes the part of code that should run under the context of a specific span. Another common practice is to use the bind() function, which binds a context to a later run of an arbitrary function or EventEmitter. It can sometimes be surprising to run a function and receive a context that is different from what’s stated in withSpan. So keep that in mind in case you see some voodoo context being set and not sure where it is coming from.</li><li>Make sure your spans are scoped correctly. In many modules, there are complicated callbacks logic and interactions with other modules. If your trace is not structured correctly, check which parts of the code resides under the withSpan() and bind() functions.</li></ul><p>if you have any questions on how to leverage OpenTelemetry on your environment feel free to reach me or the Aspecto team.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5cddf22a09e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/context-managment-in-opentelemetry-nodejs-e5cddf22a09e">Context Managment in OpenTelemetry NodeJS</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OpenTelemetry JS plugin for aws-sdk]]></title>
            <link>https://medium.com/aspecto/opentelemetry-js-plugin-for-aws-sdk-ce4cf61bdd3f?source=rss-32a7e1156d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce4cf61bdd3f</guid>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[tracing]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[aws-sdk]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Amir B]]></dc:creator>
            <pubDate>Sun, 12 Jul 2020 11:29:55 GMT</pubDate>
            <atom:updated>2021-02-16T12:24:58.113Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR — new JS OpenTelemetry auto instrumentation plugin for aws-sdk is available <a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-aws-sdk">here</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ADTj8y27n4vzXLAAtfvCfA.jpeg" /></figure><p><a href="https://opentelemetry.io/">Open Telemetry</a> is a CNCF project, which, among other things, enables the collection of distributed traces.</p><p>Traces enable observability into a software system by representing the flow of calls between different components in a service, or between different services, as well as relevant data per operation. Extracting trace data from a component is done by installing and setting up an Open Telemetry plugin which patch function calls and create span per operation.</p><h3>AWS SDK</h3><p>If your application’s backend is running Node.js on AWS, high chances that you are using the aws-sdk JS package, which exposes an API for communicating with the services offered by Amazon, such as S3, SQS, SNS, etc. <br>We can uncover significant insights by collecting trace data about those interactions. For example, grasping the full picture of how a service performs I/O or to understand the dependencies of a service in the production environment.</p><h3>Open Telemetry Plugin</h3><p>The new plugin for aws-sdk is instrumenting this package, creating an open telemetry span for each request and response to AWS. Currently, version 2 of the sdk is supported. Version 3 support will be added in after its GA launch.</p><p>The plugin has a generic part that works immediately and uniformly for all services accessibly by the SDK.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5f945b37b9a38ed4886d7a1b076b6ae/href">https://medium.com/media/f5f945b37b9a38ed4886d7a1b076b6ae/href</a></iframe><p>Specific logic can be <a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/plugin-aws-sdk#custom-user-attributes">hooked internally</a> (in the plugin level, always accessible to all users) or externally (custom configurable user behavior), which provides per-service semantic extensions where needed.</p><h3>SQS Handling</h3><p>SQS spans are created with additional attributes and processing spans, as specified in open telemetry <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md">Messaging System Specification</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*1igzxTMhqdEYvtgoN239PQ.png" /><figcaption>example of SQS receiveMessage request</figcaption></figure><p>The above screenshot is an example trace from our production environment. The first span is SQS receiveMessage operation, followed by the underlying HTTP message used by the SDK to access AWS, followed by two messaging.operation = processing spans for the processing of the messages received from the queue. <br>Finally, messages are deleted from the queue with another call to aws-sdk deleteMessage() function (followed by the HTTP span used to communicate with AWS).</p><p>The plugin is released to the community with the hope it will provide significant value. It is planned to be extended and improved on the run, in order to instrument as many AWS services possible, in the best manner.</p><p>Community contributions and involvement are more than welcome :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce4cf61bdd3f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/opentelemetry-js-plugin-for-aws-sdk-ce4cf61bdd3f">OpenTelemetry JS plugin for aws-sdk</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OpenTelemetry plugin for kafkajs]]></title>
            <link>https://medium.com/aspecto/opentelemetry-plugin-for-kafkajs-8789960b1e66?source=rss-32a7e1156d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/8789960b1e66</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[aspecto]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Amir B]]></dc:creator>
            <pubDate>Thu, 18 Jun 2020 10:47:55 GMT</pubDate>
            <atom:updated>2021-02-16T11:22:31.809Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR — new JS OpenTelemetry plugin for kafkajs, is available <a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-kafkajs">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*WzriR5GU_4BJfzxk" /></figure><p><a href="https://opentelemetry.io/">Open Telemetry</a> is a CNCF project, which, among other things, enables the collection of distributed traces.</p><p><a href="https://www.aspecto.io/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=kafkajs">Aspecto</a> is a developer-centric observability solution and we use OpenTelemetry at the backbone of our product. While implementing it on our backend, we found a few plugins that are missing, especially when dealing with asynchronous communication. One of them was kafkajs. We took this opportunity to give back to the community and develop it ourselves. With this plugin, you can track all Kafka interactions in your collected traces and get a more comprehensive view of your application behavior when using Kafka as a message broker. This article briefly describes a new plugin for the kafkajs package.</p><p>The kafkajs plugin captures producer and consumer operations and creates spans according to <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md">the semantic conventions for Messaging Systems</a>.</p><ul><li>Each message being produced or consumed is represented by a span with attributes such as messaging.destination(topic name).</li><li>Context is propagated from producers to consumers, which means that when a message is sent to Kafka, the trace will reveal which services consume it and what other cascading operations happen down the pipe.</li><li>Batch operations can aggregate multiple messages into a single batch and receive\process them together. This is handled in the plugin according to the specification.</li><li>The plugin can be extended with hooks, which enable users to run custom logic for adding span attributes depending on the Kafka message.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jOnvk0vVUj0ur4cK_-7ENg.png" /><figcaption>Simple trace example in Jaeger viewer</figcaption></figure><p>The above screenshot shows an example of a producer application named kafka-producer, that exposes an Http endpoint (first line), route it in express (second line), and produce two messages to a Kafka topic namedtest, which are then consumed by another application calledkafka-consumer .</p><p>As mentioned above, kafkajs was one of the missing plugins we found and as you are reading this we are working to add more plugins. For developers who develop in microservices architecture, Aspecto is a developer-centric observability solution that abstracts the native complexity of microservices. As so, we are very much invested in OpenTelemetry so if you have any questions, feel free to reach out to me or the team.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8789960b1e66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/opentelemetry-plugin-for-kafkajs-8789960b1e66">OpenTelemetry plugin for kafkajs</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>