<?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 Alexander Knips on Medium]]></title>
        <description><![CDATA[Stories by Alexander Knips on Medium]]></description>
        <link>https://medium.com/@alexanderknips?source=rss-23fa5bd03ac------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9pssv2ZmElm3l6c6.</url>
            <title>Stories by Alexander Knips on Medium</title>
            <link>https://medium.com/@alexanderknips?source=rss-23fa5bd03ac------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 01:27:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexanderknips/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[Hive Mind]]></title>
            <link>https://medium.com/@alexanderknips/hive-mind-eac9cbf3489c?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/eac9cbf3489c</guid>
            <category><![CDATA[knowledge-graph]]></category>
            <category><![CDATA[ai-governance]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[ai-memory]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Mon, 25 May 2026 18:49:14 GMT</pubDate>
            <atom:updated>2026-05-25T18:49:14.166Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>My promise: </strong>this article was written by me, with only light AI editing. The sketches were generated with AI from my handwritten notes.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*xVp0zDTME857J6lUSP9M5Q.png" /><figcaption>First draft introduction</figcaption></figure><h3>The Fork in the Road</h3><p>There has been a big push for AGI: truly general artificial intelligence with capabilities beyond the human mind.</p><p>In a time of large uncertainty, we have to make technological bets even if we are not the true influencers and decision-makers. Simplified, there is a fork in the road ahead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kcI1Ai8KiS9LHMsMz2qI5w.png" /><figcaption><strong>What are we aiming for?</strong></figcaption></figure><p>Fundamentally, I don’t believe anyone actually wants to relinquish 100% of decision-making power to AI. Even the biggest proponents of the bullish version of AI want to control it as a tool. They still see themselves on top of the hierarchy of decision-making.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*26zUuwsFBYhZfXi68cS8Dw.png" /><figcaption>Most humans want agents to be hierarchichally in control of agents</figcaption></figure><blockquote>On a side note: if you are the individual who relinquishes your own thinking to AI within your own role, you will be replaced.</blockquote><h3>Knowledge Moves Through Power Structures</h3><p>Given this form of hierarchy, knowledge is either exchanged through references to external documents, by flowing along the paths defined in the hierarchy, or by creating additional paths in order to share information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ABwS-qof7aWnfaIsq38hA.png" /><figcaption><strong>Agents communication patterns</strong></figcaption></figure><p>Obviously, in corporations you will also have ad-hoc communications between human actors that don’t follow the hierarchical connections. But human actors are aware that they share information in these cases and do not have immediate decision-making power. Agents should distinguish this in the same way for sufficient governance over the system.</p><h3>Governance Should Not Live Inside the Model</h3><p>Regardless of your choice of hierarchy, current systems employed in corporations for AI governance are insufficient already at the current scale, and certainly soon enough at larger scale.</p><p>While we can certainly wait until the big AI labs build these tools for governance, there are many reasons to keep governance and decision tracking separate. Using models as a commodity and separating the harness from the model gives flexibility and control. It creates room for cost cutting, control of data persistence, and the ability to use decision tracking beyond metrics — as a data source central to the corporation.</p><p>Sure, one might say that a provider could do this for us, but we very likely want to separate this. <strong>The short version is that governance should not be locked inside the same provider that generates the decisions.</strong></p><h3>Decisions Are Getting Quieter</h3><p>With the advent of agentic AI, we have all accepted that agents do indeed make their own decisions, however small those decisions might be. For some people, it is the agent that decides how to format and structure your notes. For another person, it is the agent choosing a software library. And for yet another person, the agent might choose the marketing strategy.</p><p>With all this being said, we are seemingly moving in a direction where decisions, small and large, are being made more quietly than ever. Waiting for a retrospective to happen when things go wrong has been the most common approach to summarize the details of decision-making. <strong>More often than not, people cannot explain why certain decisions were made by any actor.</strong></p><p>Sure, you can ask a human why they decided what they did a while ago. Depending on the importance of the decision, they will remember. But will they be able to explain the full reasoning chain? Maybe sufficiently. But once agentic decision-making comes into the mix, certainly not.</p><h3>Archaeology Is Not Memory</h3><p>Agents don’t really have memory beyond the context window. Reading or even finding a Claude or Codex chat transcript is closer to archaeology than a quick search. Without a system that tracks decision-making, reconstructing the reasoning chain becomes incredibly difficult.</p><p>This is where my vision comes into play:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CKJuJbJ3n4KZNGxCyrRikg.png" /><figcaption>A Decision Graph for Humans and Agents</figcaption></figure><p>Beyond the memory of one specific agent, this allows us to have a global graph of how our corporation makes decisions and what factors come into play.</p><p>You might say this is oversight, but in the corporate world we certainly want some kind of transparency. Sure, there will be configuration mechanisms that hide some parts of the graph from some actors, but overall the system should be designed with transparency in mind.</p><h3>What the Graph Can Answer</h3><p>How does this differ from other knowledge graphs?</p><p>Most knowledge graphs optimize for knowledge retrieval by connecting nodes along similarity. I specifically want to incorporate causal connections. While this will have some overhead, it will allow extreme precision, such as:</p><ul><li>Why was the previous decision overridden by a new one?</li><li>What new evidence made us change our mind?</li></ul><p>Beyond this, in the future it will also be able to inform new decision-making.</p><ul><li>How does a question or problem relate to previous ones?</li><li>Can we bypass a long investigation in one place, or invest in more analysis in another?</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4MLA78BDpUgiv-bNwsGptg.png" /><figcaption>A chain of d</figcaption></figure><h3>The Basic Units of the Graph</h3><p>It is clear that one central unit is a decision. But decisions are not the whole taxonomy. A decision is the commitment point of the graph. It is where an actor selects, rejects, or constrains possible future action.</p><p>For the sake of this problem, we can define a decision as:</p><blockquote><em>A commitment, made by an authorized actor, that selects, rejects, or constrains possible future actions within a defined scope.</em></blockquote><p>We will also capture rejected options within the knowledge graph. A rejected choice is, after all, still a decision. A decision is made within a context. Context is given as references to other nodes in the graph or external artifacts. This makes context a property of a node, but not a node itself.</p><p>Strong decision-making is based on evidence. Without evidence, a decision is just a bet. Some evidence, or priors, make it a calculated bet.</p><p>We will also have assumption nodes. An assumption is a premise accepted for the purpose of reasoning when we cannot, or simply choose not to, fully establish its validity within the current scope. In math and science, assumptions are always made. Our intellectual honesty requires us to sufficiently describe them.</p><p>Certainly, in the corporate world, some actors are more trustworthy than others and their decision-making can be more instinctive. We might accept that from a serial founder, but certainly not as much from junior people or agents. Otherwise, extreme oversight and micro-management becomes necessary.</p><p>Beyond this system envisioned here, we should still allow for creative ideas from actors without giving up on intellectual transparency. Otherwise, we are just lazy.</p><p>There is more to come in this series of blog posts. <strong>A first open-source version can be followed at </strong><a href="https://github.com/alexknips/hivemind"><strong>https://github.com/alexknips/hivemind</strong></a><strong>.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eac9cbf3489c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Effect-TS 1: Http backend demo]]></title>
            <link>https://medium.com/@alexanderknips/if-you-are-unfamiliar-with-effect-systems-zio-or-effect-ts-i-recommend-reading-the-following-8a7bc51124a4?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a7bc51124a4</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Sat, 27 Jul 2024 01:30:25 GMT</pubDate>
            <atom:updated>2024-07-30T01:02:06.317Z</atom:updated>
            <content:encoded><![CDATA[<h3>A simple hello world style start into this series</h3><p>If you are unfamiliar with Effect Systems, ZIO, or Effect-TS, I recommend reading the following article: <a href="https://www.sandromaglione.com/articles/typescript-code-with-and-without-effect#running-an-effect">https://www.sandromaglione.com/articles/typescript-code-with-and-without-effect#running-an-effect</a> and then returning here.</p><p>Other good information about Effect-TS can be found on:</p><ul><li><a href="https://www.sandromaglione.com/articles/typescript-code-with-and-without-effect">https://effect.website/</a></li><li><a href="https://www.sandromaglione.com/articles/typescript-code-with-and-without-effect">https://www.youtube.com/@effec</a><a href="https://www.youtube.com/@effect-ts">t-ts</a></li></ul><p><em>Disclaimer: The following article will work with Effect-TS Version 3 hoping that the APIs will stay compatible for the near future.</em></p><p>Let’s make our lives easier by using Bun for this project. Bun is fantastic for quickly starting small TypeScript projects because it’s super fast and easy to use. It combines a native TypeScript transpiler, a built-in bundler, and a speedy package manager, so we can dive right into coding without the hassle of setting up multiple tools.</p><ol><li>Open a terminal and navigate to the directory where you want to create your project. Create a clean folder:</li></ol><pre>mkdir effect-web<br>cd effect-web</pre><p>2. Run the following command to initialize a new Bun project:</p><pre>bun init</pre><p>We will just use the default options. This command will create a new Bun project with the default project structure and configuration files.</p><p>3. Once the project is initialized, you can start adding your code and customizing the project according to your needs. Let’s install some basic dependencies for what we want to do with Effect Http.</p><pre>bun add effect effect-http effect-http-node @effect/platform @effect/platform-node</pre><p>4. We now create a new Typescript file with the name app.ts and the following content</p><pre>import { Schema } from &quot;@effect/schema&quot;;<br>import { pipe } from &quot;effect&quot;;<br>import { Api } from &quot;effect-http&quot;;<br><br>// Define the response schema for the user endpoint<br>const UserResponse = Schema.Struct({<br>    name: Schema.String,<br>    id: pipe(Schema.Number, Schema.int(), Schema.positive())<br>});<br>// Define the request query schema for the getUser endpoint<br>const GetUserQuery = Schema.Struct({ id: Schema.NumberFromString });<br>// Create an instance of the Effect HTTP API<br>const api = pipe(<br>    Api.make({ title: &quot;Users API&quot; }),<br>    Api.addEndpoint(<br>        pipe(<br>            Api.get(&quot;getUser&quot;, &quot;/user&quot;),<br>            Api.setResponseBody(UserResponse),<br>            Api.setRequestQuery(GetUserQuery)<br>        )<br>    )<br>);</pre><p>Here we are using the library <a href="https://github.com/sukovanej/effect-http">https://github.com/sukovanej/effect-http</a>. It’s possible that this library becomes part of the official Effect-TS framework later on.</p><p>The code above demonstrates how to create a simple web server using Effect HTTP. It defines a response schema for the user endpoint and a request query schema for the getUser endpoint. The Api.make function is used to create the definition of our API, and the Api.addEndpoint function is used to add an endpoint to the API routes. This is not the actual implementation; you can think of it as an interface.</p><p>5. The actual implementation is achieved by changing our imports to</p><pre>import { Schema } from &quot;@effect/schema&quot;;<br>import { Effect, pipe } from &quot;effect&quot;;<br>import { Api, RouterBuilder } from &quot;effect-http&quot;;</pre><p>and implementing the actual execution as</p><pre>const app = pipe(<br>    RouterBuilder.make(api),<br>    RouterBuilder.handle(&quot;getUser&quot;, ({ query }) =&gt; <br>        Effect.succeed({ name: &quot;milan&quot;, id: query.id })),<br>    RouterBuilder.build<br>)</pre><p>6. We execute the server by adding</p><pre>import { NodeRuntime } from &quot;@effect/platform-node&quot;<br>import { NodeServer } from &quot;effect-http-node&quot;;<br>// remaining code<br>app.pipe(NodeServer.listen({ port: 3000 }), NodeRuntime.runMain);</pre><p>7. In another terminal we call the server by using curl</p><pre>curl &quot;http://localhost:3000/user?id=1&quot;</pre><p>which will corretly print {&quot;name&quot;:&quot;milan&quot;,&quot;id&quot;:1} to STDOUT.</p><p>This is the first part of a series about Effect-TS in backend engineering. Future topics will cover:</p><ul><li>More features from Effect-HTTP</li><li>Layers and Services in Effect-TS</li><li>Interaction with OpenAI APIs</li><li>Telemetry through OpenTelemetry</li></ul><p>I hope you enjoyed this introduction to writing functional TypeScript code with Effect-TS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a7bc51124a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OpenTelemetry for Kafka and NestJS]]></title>
            <link>https://medium.com/@alexanderknips/opentelemetry-for-kafka-and-nestjs-bb4ca3795a06?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb4ca3795a06</guid>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[kafkajs]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[cloud-native]]></category>
            <category><![CDATA[nestjs]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Sun, 02 Jun 2024 22:45:43 GMT</pubDate>
            <atom:updated>2024-06-02T23:12:33.563Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, let me show you how to set up OpenTelemetry in a Kafka and NestJS microservices architecture. I will use a practical example of an e-commerce application to demonstrate Event Driven Architecture but show the implementation with the toy example that I have previously shown in <a href="https://medium.com/@alexanderknips/open-telemetry-with-multiple-nestjs-and-logs-in-tracing-62453e3e6cb2">https://medium.com/@alexanderknips/open-telemetry-with-multiple-nestjs-and-logs-in-tracing-62453e3e6cb2</a>.</p><p>Let’s first discuss what Event Driven Architecture is.</p><h3>What is Event Driven Architecture</h3><p>Event-Driven Architecture (EDA) is a design paradigm where systems communicate through events — persisted messages that describe completed changes of state. Unlike command-driven approaches, where services send direct requests to perform actions, EDA emphasizes using events to notify services about state changes, though it also allows for commands when necessary. This architecture decouples microservices asynchronously, enabling each service to handle its domain logic independently. A service can achieve a completed change within its domain when satisfied with the received event or command. This decoupling enhances scalability and resilience, allowing microservices to evolve independently and respond dynamically to changes.Command driven architecture might not sound familiar but one micro-service calling the REST interface to command it do something is exactly that.</p><p>Within my sphere of experience, we have used EDA mostly for its capability to adapt to changes and allow better separation of concerns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U__v44lFqqnl54g4wLXYgg.png" /><figcaption>An example could be an e-commerce application with a shopping cart and multiple other micro-services. Here we still kept the command driven nature of the payment processing but decided to separate the ordering service and shipment fulfillment in an event-driven manner.</figcaption></figure><p>Let’s investigate this on our much smaller example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*EgywEf_AtPPsdfOHhBa_6w.png" /><figcaption>Command Driven vs Event Driven style in our toy example.</figcaption></figure><p>Now we have separated the three micro-services <strong>nest1, nest2 and nest3 </strong>through events instead of commands (REST in implementation). The services are more independent in execution and conceptually. This would also allow us to independently evolve the micro-services themselves.</p><h3>The need for telemetry in Event Driven Architecture</h3><p>Similar to how we previously separated functionality into microservices, we want to trace user interactions and all their components. This includes seeing how a client’s call leads to subsequent steps, whether our architecture is command-driven or event-driven. This tracing allows us to understand and monitor the complete flow of user actions through the system.</p><h3>How to achieve this conceptually</h3><p>Previously we attached the trace parent (<a href="https://opentelemetry.io/docs/concepts/glossary/">https://opentelemetry.io/docs/concepts/glossary/</a>) completely automatically by using <em>HttpInstrumentation.</em></p><p>Here, headers are used to attach the current trace id as the subsequent trace parent when making REST call to another micro-service.</p><p>The Open Telemetry documentation explains context propagation in a lot of detail here: <a href="https://opentelemetry.io/docs/concepts/signals/traces/#context-propagation">https://opentelemetry.io/docs/concepts/signals/traces/#context-propagation</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uoYXSsSlKDHLi3nCU9Xu9Q.png" /><figcaption>Nested span in Command Driven Architecture</figcaption></figure><p>We will do exactly the same in Kafka, except of the parent span actually completing. This does not prevent us from propagating the trace through Kafka and it’s headers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X8xvv4l7nvddRyNlLovqQg.png" /></figure><p>Let’s go through the steps what’s happening here again.</p><ol><li>A Span is created in Nest1. This span might have a whole hirarchy of child spans in itself. This span has a specific spanId. A sample span might look like</li></ol><pre>{<br>  &quot;name&quot;: &quot;/kafka&quot;,<br>  &quot;context&quot;: {<br>    &quot;trace_id&quot;: &quot;7bba9f33312b3dbb8b2c2c62bb7abe2d&quot;,<br>    &quot;span_id&quot;: &quot;086e83747d0e381e&quot;<br>  },<br>  &quot;parent_id&quot;: &quot;&quot;,<br>  &quot;start_time&quot;: &quot;2021-10-22 16:04:01.209458162 +0000 UTC&quot;,<br>  &quot;end_time&quot;: &quot;2021-10-22 16:04:01.209514132 +0000 UTC&quot;,<br>  &quot;status_code&quot;: &quot;STATUS_CODE_OK&quot;,<br>  &quot;status_message&quot;: &quot;&quot;,<br>  &quot;attributes&quot;: {<br>    &quot;net.transport&quot;: &quot;IP.TCP&quot;,<br>    // more attributed<br>  }<br>}</pre><p>2. Now we will publish an event to Kafka with the additional header</p><pre>{&quot;traceparent&quot;:&quot;086e83747d0e381e&quot;}</pre><p>3. The subscriber that will subscribe to the topic will pick up the message and open a new span. The new span might look similar to<br>where the previous span was added as a parent_id.</p><pre>{<br>  &quot;name&quot;: &quot;/kafka&quot;,<br>  &quot;context&quot;: {<br>    &quot;trace_id&quot;: &quot;7bba9f33312b3dbb8b2c2c62bb7abe2d&quot;,<br>    &quot;span_id&quot;: &quot;094e12347a0a211d&quot;<br>  },<br>  &quot;parent_id&quot;: &quot;086e83747d0e381e&quot;,<br>  &quot;start_time&quot;: &quot;2021-10-22 16:04:01.209458162 +0000 UTC&quot;,<br>  &quot;end_time&quot;: &quot;2021-10-22 16:04:01.209514132 +0000 UTC&quot;,<br>  &quot;status_code&quot;: &quot;STATUS_CODE_OK&quot;,<br>  &quot;status_message&quot;: &quot;&quot;,<br>  &quot;attributes&quot;: {<br>    &quot;net.transport&quot;: &quot;IP.TCP&quot;,<br>    // more attributed<br>  }<br>}</pre><p>When this is then pushed to the Open Telemetry backend (e.g. Jaeger or something similar), it can make sense of the span and how they relate to each other.</p><h3>Implementation</h3><p>Let’s install and configure the Open Telemetry KafkaJS extension (<a href="https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-kafkajs">https://github.com/aspecto-io/opentelemetry-ext-js/tree/master/packages/instrumentation-kafkajs</a>) in our 3 micro-services.</p><p>We will add the <em>KafkaJsInstrumentation </em>to our tracing configuration.</p><pre>const sdk = new NodeSDK({<br>  traceExporter,<br>  instrumentations: [<br>    new NestInstrumentation(),<br>    new HttpInstrumentation(),<br>    new ExpressInstrumentation(),<br>    new KafkaJsInstrumentation({<br>      // see under for available configuration<br>    })<br>  ],<br>  resource: new Resource({<br>    [SemanticResourceAttributes.SERVICE_NAME]: &#39;nest1&#39;,<br>  }),<br>});</pre><p>Follow the Kafka documentation to publish and subscribe to topics within our demo application. Refer to:</p><ul><li><a href="https://docs.nestjs.com/microservices/kafka">https://docs.nestjs.com/microservices/kafka</a></li><li><a href="https://docs.nestjs.com/faq/hybrid-application">https://docs.nestjs.com/faq/hybrid-application</a> to enable HTTP and Kafka at the same time</li></ul><p>or take a look the the full source code provided in:</p><ul><li><a href="https://github.com/alexknips/nest-open-telemetry"><em>https://github.com/alexknips/nest-open-telemetry</em></a></li></ul><p>Kafka runs locally in my example through <em>docker-compose </em>configured as</p><pre>version: &#39;3.7&#39;<br>services:<br>  kafka-ui:<br>    image: provectuslabs/kafka-ui:latest<br>    ports:<br>      - 8080:8080<br>    environment:<br>      KAFKA_CLUSTERS_0_NAME: local<br>      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092<br>      KAFKA_CLUSTERS_0_KAFKACONNECT_0_NAME: debezium<br>      KAFKA_CLUSTERS_0_KAFKACONNECT_0_ADDRESS: http://debezium-connect:8083<br>      DYNAMIC_CONFIG_ENABLED: &#39;true&#39;<br>    networks:<br>      - jaeger-example<br>  zookeeper:<br>    image: confluentinc/cp-zookeeper:7.5.0<br>    hostname: zookeeper<br>    ports:<br>      - &#39;2181:2181&#39;<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_TIME: 2000<br>    networks:<br>      - jaeger-example<br>  kafka:<br>    image: confluentinc/cp-kafka:7.5.0<br>    hostname: kafka<br>    depends_on:<br>      - zookeeper<br>    ports:<br>      - &#39;29092:29092&#39;<br>      - &#39;9092:9092&#39;<br>      - &#39;9101:9101&#39;<br>    environment:<br>      KAFKA_BROKER_ID: 1<br>      KAFKA_ZOOKEEPER_CONNECT: &#39;zookeeper:2181&#39;<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT<br>      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092<br>      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1<br>      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1<br>      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1<br>      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0<br>      KAFKA_JMX_PORT: 9101<br>      KAFKA_JMX_HOSTNAME: localhost<br>    healthcheck:<br>      test: [&#39;CMD&#39;, &#39;kafka-topics&#39;, &#39;--bootstrap-server&#39;, &#39;kafka:29092&#39;, &#39;--list&#39;]<br>      interval: 10s<br>      timeout: 10s<br>      retries: 10<br>    networks:<br>      - jaeger-example<br>  wait-kafka-init:<br>    image: confluentinc/cp-kafka:7.2.1<br>    hostname: wait-kafka-init<br>    depends_on:<br>      kafka:<br>        condition: service_healthy<br>    entrypoint: [&#39;/bin/sh&#39;, &#39;-c&#39;]<br>    command: |<br>      &quot;<br>      # blocks until kafka is reachable<br>      kafka-topics --bootstrap-server kafka:29092 --list<br><br>      echo -e &#39;Kafka is reachable&#39;<br>      &quot;<br>    networks:<br>      - jaeger-example<br>  jaeger:<br>    image: jaegertracing/all-in-one:latest<br>    ports:<br>      - &quot;16686:16686&quot;<br>      - &quot;4318:4318&quot;<br>      - &quot;4317:4317&quot;<br>    environment:<br>      - LOG_LEVEL=debug<br>    networks:<br>      - jaeger-example<br>  nestapp1:<br>    image: nestapp1<br>    build:<br>      context: nest1<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3001:3000&quot;<br>    environment:<br>      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318<br>      - KAFKA_BOOTSTRAP_SERVERS=kafka:29092<br>    networks:<br>      - jaeger-example<br>    depends_on:<br>      - jaeger<br>      - nestapp2<br>      - nestapp3<br>      - wait-kafka-init<br>  nestapp2:<br>    image: nestapp2<br>    build:<br>      context: nest2<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3002:3000&quot;<br>    environment:<br>      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318<br>      - KAFKA_BOOTSTRAP_SERVERS=kafka:29092<br>    networks:<br>      - jaeger-example<br>    depends_on:<br>      - jaeger<br>      - nestapp3<br>      - wait-kafka-init<br>  nestapp3:<br>    image: nestapp3<br>    build:<br>      context: nest3<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3003:3000&quot;<br>    environment:<br>      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318<br>      - KAFKA_BOOTSTRAP_SERVERS=kafka:29092<br>    networks:<br>      - jaeger-example<br>    depends_on:<br>      - jaeger<br>      - wait-kafka-init<br><br>networks:<br>  jaeger-example:</pre><p>I am aware that this looks very complex and I can get into more details if needed at another time.</p><h3>Result</h3><p>In the Jaeger UI, you will see the full traces of our Event-Driven Architecture example, showing how one application creates an event, pushes it to Kafka, and the next service in line picks it up successfully. This visibility into the system’s operations makes observability much easier in distributed systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tZ_CLaMzfsX4drMocC_z7w.png" /><figcaption>Traces for Event Driven Architecture toy example</figcaption></figure><p>We can now observe how one application creates an event, pushes it to Kafka and the next one in line picks it up successfully.</p><p>As mentionted previously take a look at the source code provided in <a href="https://github.com/alexknips/nest-open-telemetry"><em>https://github.com/alexknips/nest-open-telemetry</em></a><em>.</em></p><p>Good luck playing around and extending this to your use-cases.</p><h3>Further reading</h3><p>The actual extension is explained in a lot of detail in the following artices:</p><ul><li><a href="https://www.aspecto.io/blog/distributed-tracing-for-kafka-with-opentelemetry-in-node/">Distributed Tracing for Kafka with OpenTelemetry in Node | Aspecto</a></li><li><a href="https://www.aspecto.io/blog/opentelemetry-kafkajs-instrumentation-for-node-js/">OpenTelemetry KafkaJS Instrumentation for Node.js | Aspecto</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb4ca3795a06" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Systems Design 2 — TicTacToe]]></title>
            <link>https://medium.com/@alexanderknips/systems-design-2-tictactoe-c145ca27cc4f?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/c145ca27cc4f</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[oop]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Wed, 24 Jan 2024 06:47:25 GMT</pubDate>
            <atom:updated>2024-01-24T06:47:25.860Z</atom:updated>
            <content:encoded><![CDATA[<h3>Systems Design 2 — TicTacToe</h3><h4>An example where Pseudo-Code gets you farther than extensive diagramming.</h4><p>Continuing our series, we will now design one of the games mentioned at <a href="https://igotanoffer.com/blogs/tech/system-design-interviews#file">https://igotanoffer.com/blogs/tech/system-design-interviews</a> section 2.</p><blockquote><em>Another topic that comes up frequently is designing a classic game. The exact game you’ll be asked about varies, but here are some of the most common ones we’ve seen:</em></blockquote><ul><li>Tic-tac-toe</li><li>Chess</li><li>Boggle</li><li>Minesweeper</li><li>Cards/poker</li></ul><p>In the first round, we will take a look what they all have in common in terms of requirements and then go into more detail afterwards.</p><h3>Requirement Gathering — Fundamentals</h3><p>Let’s gather our requirements once more.</p><h3>Requirements</h3><ol><li>All of the games are played in rounds. Some by a single player (minesweeper), most by two players (Tic-Tac-Toe) and others by N players (cards, Poker, Boggle).</li><li>After a player makes a move, the state of the game needs to be updated and shown to the players.</li><li>Stop the game if one of the player wins or there is a draw.</li><li>Allow moves according to game rules</li><li>Calculate winner after each move according to game rules.</li></ol><p>The non-functional requirements are simple</p><h3>Non-functional requirements</h3><ul><li>Make user-interaction seamless and pleasant</li><li>No further non-functional requirements for now</li></ul><p>As the non-functional requirements are kept simple, we will not try to overengineer the solution either. Sure, the stateholders will ask for additional functional and non functional requirements such as scoring boards, how to join a game, chat within a game or the ability to run thousand of games in parallel. Obviously then there will be asks for monetization, authentication, authorization, in-game purchases and the like but we will focus on the core domain.</p><p>Let’s choose TicTacToe as our special case and continue from there.</p><p>The rules are (summarized by ChatGPT):</p><ol><li><strong>Game Board</strong>: Tic-Tac-Toe is played on a 3x3 grid, which can be drawn on paper or played on a computer or mobile app.</li><li><strong>Players</strong>: There are two players in the game. One player uses ‘X’ and the other uses ‘O’.</li><li><strong>Objective</strong>: The main objective is to be the first player to get three of their marks in a row — horizontally, vertically, or diagonally.</li><li><strong>Game Play</strong>: Players take turns placing their mark (X or O) in an empty square on the grid. The first player is usually the ‘X’ and the second player is the ‘O’. <strong>Winning the Game</strong>: A player wins if they manage to place three of their marks in a row, column, or diagonal.</li><li><strong>Draw</strong>: If all 9 squares are filled and neither player has 3 marks in a row, the game is considered a draw.</li></ol><p>Let’s start with the architecture and design.</p><h3>Architecture and Design</h3><p>Similar to my previous post <a href="https://hashnode.com/edit/clr5yzlkh000b09lc77rq5l45">https://hashnode.com/edit/clr5yzlkh000b09lc77rq5l45</a>, we will choose an appropriate architectural style before we design the specifics.</p><p>Both a layered traditional application and a monolithic architecture could be sensible choices, depending on the use-case. For a web-based application it’s almost impossible to actually make it completelty monolithic if the two players are on different computers. So some layering (client + server) will automatically be involved.</p><p>A Micro-Service or Service-Oriented-Architecture just does not have any benefits in this use-case. There is a single set of rules and those most likely would be kept in a single micro-service. You cannot really consider the presentation layer to be a micro-service either. It just does not apply here, really.</p><p>We also know that a Data Centric architectural style is not benefitial as we did not even have any benefits about persistence. This does not, however, mean that you cannot be successful by modelling the game logic through OOP and the relationship of the objects that model our domain.</p><p>The specific domain of our game can be transformed into an implementation with OOP and functional programming, either way.</p><p>This particular exercise is an example where pseudo-code or real interfaces with missing implementation are a good way to start quickly and iterate on the design. Diagrams are inferiour in my opinion in this particular exercise.</p><h3>Object Oriented Programming Approach</h3><p>Let’s start with the traditional approach of sketching out this game in OOP. We will try to do this in the most traditional approach for OOP.</p><pre>class Player {<br>    // constructor missing<br>    symbol: string;<br>    name: string;<br>}  <br>class Board {<br>    // constructor missing<br>    grid: string[][];<br>}<br>class TicTacToeGame {<br>    // constructor missing<br>    board: Board;<br>    player1: Player;<br>    player2: Player;<br>    currentPlayer: Player;<br>    move(row: number, col: number): boolean;<br>    checkWin(): true;<br>    checkDraw(): boolean;<br>    switchPlayer(): void;<br>}<br><br>const game = // init class for TicTacToe;<br>let gameIsRunning = true;<br>// displayBoard(game.board);<br>while (gameIsRunning) {<br>    // obtain moveParams<br>    const moveSuccessful = game.move(moveParams);<br>    // displayBoard(game.board); display the changed board after a move<br>    if (moveSuccessful) {<br>        if (game.checkWin()) {<br>            gameIsRunning = false;<br>        } else if (game.checkDraw()) {<br>            gameIsRunning = false;<br>        } else {<br>            game.switchPlayer();<br>        }<br>    }<br>}</pre><p>The first class is to define what a Player is. A minimum requirement would be that each player has a symbol and a name associated to it.</p><p>Now we define the Board class with its data structure as simply a two dimensional array of string. This is obviously awful and we could improve on it later with an enum or a more specific Field class. In traditional OOP one probably would choose a Field class.</p><p>But the more interesting aspect is the class TicTacToeGame . The board and the two players have to be initialized. The currentPlayer switches through the switchPlayer method. move is called with some parameters obtained from user input. In the game loop we end the game on a draw or a win and show the board if a move has been made.</p><p>While this works, this must be improved. From this starting point we would now</p><ul><li>Add validation that the two players have not choosen the same symbol while narrowing down the symbol through a class or enum. This adds further complexity. This validation logic would need to be implemented on the class TicTacToeGame .</li><li>Express what parts of the classes are private and what parts are public</li><li>Lock the properties player1 and player2 as they cannot be modified after the game started.</li></ul><p>The player class could look something like</p><pre>class Symbol {<br>    private value: string;<br>    private constructor(value: string) {<br>        this.value = value;<br>    }<br>    public static readonly X = new Symbol(&quot;X&quot;);<br>    public static readonly O = new Symbol(&quot;O&quot;);<br>}</pre><p>This gives us the ability to use X and O directly.</p><p>How would we validate that the two players are set correctly.</p><pre>private validatePlayerSymbols(player1: Player, player2: Player): void {<br>    const symbol1 = player1.getSymbol();<br>    const symbol2 = player2.getSymbol();<br><br>    if ((symbol1 === Symbol.X &amp;&amp; symbol2 === Symbol.O) || (symbol1 === Symbol.O &amp;&amp; symbol2 === Symbol.X)) {<br>        // Symbols are valid<br>    } else {<br>        throw new Error(&quot;Players must have different symbols: either &#39;X&#39; or &#39;O&#39;.&quot;);<br>    }<br>}</pre><p>This would solve it for now but we would utterly rely on run-time validation to implement the game rules.</p><p>Could we do better? Sure, we could setup the symbol of the second player after choosing the first player chooses the symbol.</p><p>But this does not change the fact that the fundamental classes don’t actually describe the game rules directly declaratively.</p><p>Let’s see how this would be done in Functional Programming.</p><h3>Functional Programming Approach</h3><p>We use Algebraic Data Types (wiki: <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">ADT</a>) to express the Player as a type.</p><pre>type PlayerSymbol = &#39;X&#39; | &#39;O&#39;;</pre><p>Next we define the Player</p><pre>type Player&lt;S extends Symbol&gt; = {<br>    name: string;<br>    symbol: S;<br>};</pre><p>by us not using Symbol but S within the Player type we can then create a utility type</p><pre>type OppositeSymbol&lt;S extends Symbol&gt; = S extends &#39;X&#39; ? &#39;O&#39; : &#39;X&#39;;</pre><p>and further define</p><pre>player1: Player&lt;S&gt;;<br>player2: Player&lt;OppositeSymbol&lt;S&gt;&gt;;</pre><p>to fully encapsulate that the second player has the opposite symbol at compile time instead of runtime.</p><pre>type BoardSymbol = PlayerSymbol | &#39;&#39;;<br>type Board = BoardSymbol[][];</pre><p>we actually don’t need to do more than that right now to define any board. In functional programming we prefer to create functions and keep the data structures as simple as possible. However, there are cases where we do embrace <strong>OOP</strong> and <strong>Functional Programming</strong> at once. Which is easily possible in <strong>TypeScript</strong>.</p><p>Functions themselves are types indeed and that’s why we can just define the overall Move function as a type itself.</p><pre>type Move = (board: Board, row: number, col: number, <br>    symbol: PlayerSymbol) =&gt; Board;</pre><p>As it’s not a method anymore, we pass the board into the function as well as getting it as a result. Which is necessary as want to use Board as an immutable value and create a new value every single time a move is made. In functional programming we embrace immutable values. Multi-paradigm languages such as Typescript or Scala can obviously get around this by allowing mutable variables as well as immutable values.</p><p>Similar to before, we still need</p><pre>isGameOver(board: Board): boolean;<br>function getOppositeSymbol&lt;S extends PlayerSymbol&gt;(symbol: S): OppositeSymbol&lt;S&gt; {<br>    // ... implementation missing<br>}<br>const makeMove: Move = (board: Board, row: number, col: number, <br>    symbol: PlayerSymbol) =&gt; { // implementation ... }<br><br>function ticTacToeGameLoop(board: Board, currentPlayer: PlayerSymbol): void {<br>    printBoard(board); // this is not a pure function without side-effects<br>    const moveParams = obtainMoveParams(); // also not a pure function without side-effects<br>    if (isGameOver(board)) {<br>        // exit<br>    }<br>    <br>    const nextBoard = makeMove(board, moveParams);<br>    // Switch player<br>    const nextPlayer = getOppositeSymbol(currentPlayer);<br>    // Recursive call for the next turn<br>    ticTacToeGameLoop(nextBoard, nextPlayer);<br>}<br><br>// Starting the game<br>const initialBoard: Board = // ... init the board<br>ticTacToeGameLoop(initialBoard, &#39;X&#39;); // just an example where X starts</pre><p>We define the overall game loop as a recursive function instead of a loop as this gives us the cleanest functional implementation possible.</p><p>Displaying the board is a side-effect as we are mutating the outside state. Similarily, obtaining an outside state is considered a side-effect because it violates one of the core principles of the paradigm: function purity. A function is considered pure if it always produces the same output for the same input and does not cause any observable side effects.<br>However, we completely isolated all side-effects to two functions. The code could easily be modified to abstract all side-effects in a single function as we could combine printBoard and obtainMoveParams.</p><p>Some people might prefer to even define the application of the side-effects through a Monad. I am not one of these people, yet.</p><p>If we wanted to go further and actually narrow down the function ticTacToeGameLoop I would opt to define it through an effectful framework such as <a href="https://effect.website/">EffectTS</a> which is similar to ZIO in Scala.</p><pre>// Effect&lt;Requirements, Error, Value&gt; <br>type TicTacToeGameLoop = Effect.Effect&lt;Console, never, void&gt;</pre><p>This really is just a teaser and I hope that I can get into this topic on another part of my blog series.</p><h3>Summary</h3><p>We’ve seen the advantages of using functional programming to define game rules through compile-time safe constructs. This approach significantly enhances the correctness and conciseness of the implementation, surpassing traditional methods like extensive unit testing and runtime validation.</p><p>However, transitioning to this style can be challenging. TypeScript, with its flexibility, provides an excellent opportunity to learn functional programming without being overly restrictive.</p><p>This design task also underscores the effectiveness of directly writing code, particularly with functional programming patterns. While diagrams and mathematical definitions are alternatives, they may not be as familiar to many developers. TypeScript offers a more accessible and practical approach to exploring these concepts.</p><p>Thanks for following.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c145ca27cc4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clarity LIMS 6.2 On Kubernetes/K8s]]></title>
            <link>https://medium.com/@alexanderknips/clarity-lims-6-2-on-kubernetes-k8s-e265da9408b3?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/e265da9408b3</guid>
            <category><![CDATA[lims-software]]></category>
            <category><![CDATA[illumina]]></category>
            <category><![CDATA[lims-software-providers]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Fri, 12 Jan 2024 22:11:49 GMT</pubDate>
            <atom:updated>2024-01-12T22:11:49.285Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Karius Inc. is leading the technology development and offering of infectious disease diagnostic based on microbial cell-free DNA (mcfDNA). Karius Test offers a fast and unbiased method to detect over 1,500 potentially disease-causing pathogens in humans.</blockquote><p>At Karius (<a href="http://www.kariusdx.com/">www.kariusdx.com</a>), we run some automation scripts that are integrated into Illumina’s Clarity LIMS (<a href="https://www.illumina.com/products/by-type/informatics-products/clarity-lims.html">https://www.illumina.com/products/by-type/informatics-products/clarity-lims.html</a>) within our Kubernetes clusters. We do this mostly for security reasons and having more control and automation about the deployment process, but it also gives us better insights and observability.</p><p>Not many laboratories run Clarity Automation Workers through docker but hopefully this article will help the one who do.</p><p>After the switch of Clarity LIMS in 6.2 to Oracle Linux 8 (see release notes: <a href="https://support-docs.illumina.com/SW/ClarityCore_v6/Content/SW/ClarityLIMS/RelNotesv62_swCL.htm">https://support-docs.illumina.com/SW/ClarityCore_v6/Content/SW/ClarityLIMS/RelNotesv62_swCL.htm</a>), our base docker image is the following:</p><p><em>Dockerfile.base</em></p><pre>FROM oraclelinux:8<br>RUN yum -y install dejavu-sans-fonts<br>RUN yum -y install fontconfig<br>RUN yum install -y openssl<br>COPY ClarityLIMS-PreReqs-6.2.1.0-2.x86_64.rpm .<br>RUN rpm -i ClarityLIMS-PreReqs-6.2.1.0-2.x86_64.rpm<br>COPY ClarityLIMS-SecretUtil-1.4.0.2-48.x86_64.rpm .<br>RUN rpm -i ClarityLIMS-SecretUtil-1.4.0.2-48.x86_64.rpm<br>COPY BaseSpaceLIMS-AutomationWorker-2.2.1.0-4.x86_64.rpm .<br>RUN rpm -i BaseSpaceLIMS-AutomationWorker-2.2.1.0-4.x86_64.rpm</pre><p>This takes the base Oracle Linux 8 Docker image and installs fundamental requirements to install Clarity LIMS 6.2.1 and prerequisites.</p><p>Please talk to Clarity LIMS support to obtain the specific rpm files as referenced here.</p><p>This will obviously only get you so far and nothing can be run yet.</p><p>The next step in the installation is to create a Docker image that can configure the secrets necessary to run.</p><p>The next file assumes that we have built the previous Docker image as clarityautomationbase and tagged it as latest. Please change the tagging accordingly to your requirements.</p><p><em>Dockerfile.secrets</em></p><pre>FROM clarityautomationbase:latest<br># our own config and script<br>COPY secrets.sh /scripts/secrets.sh<br>COPY init-files /init-files<br>RUN [&quot;chmod&quot;, &quot;+x&quot;, &quot;/scripts/secrets.sh&quot;]<br># encryption key has to be equal in bartender and bartender-secrets<br>ENV CLARITYSECRET_ENCRYPTION_KEY=REPLACEME<br>ENV CLARITYSECRET_HOME=&quot;/opt/gls/clarity/tools/secretutil&quot;<br>ENV CLARITYSECRET_JAR=&quot;secretutil.jar&quot;<br>ENV USER=glsjboss<br>USER glsjboss<br>ENTRYPOINT [&quot;/scripts/secrets.sh&quot;]</pre><p>which whill build a docker image named <em>clarityautomation-secrets:tag</em></p><p>where</p><p><em>secrets.sh</em></p><pre>#!/bin/bash<br>cp /init-files/* /opt/gls/clarity/tools/secretutil/conf/<br>/opt/gls/clarity/config/configure_claritylims_secretutil.sh &lt; /inputs/secrets.dat</pre><p>and the init files are</p><p><em>init-files/secret.properties</em></p><pre>clarity/app.api.password=<br>clarity/app.ftp.password=<br>clarity/app.ldap.managerPass=<br>clarity/app.rabbitmq.password=<br>clarity/db.tenant.password=<br>clarity/db.clarity.password=<br>clarity/db.lablink.password=<br>clarity/db.reporting.password=</pre><p>and</p><p><em>init-files/vault.properties</em></p><pre>application.mode=vault<br>application.secret-engine=secret<br>application.tenant-hostname=localhost<br>application.vault-namespace=<br><br>## Retry Configuration<br>application.retry-config.maxAttempts=7<br>application.retry-config.initialInternalMillis=5000<br>application.retry-config.retryMultiplier=2<br><br>############################# VAULT #############################<br>## Refer to https://docs.spring.io/spring-vault/docs/current/reference/html/#vault.core.environment-vault-configuration<br>## Server configuration<br>vault.uri=http://localhost:8200<br>vault.authentication=TOKEN<br><br>## SSL<br>#vault.ssl.key-store=<br>#vault.ssl.key-store-password=<br>#vault.ssl.trust-store=<br>#vault.ssl.trust-store-password=<br><br>## Authentication<br># Token / Cubbyhole<br>vault.token=<br><br># App ID<br>vault.app-id.app-id=<br>vault.app-id.user-id=<br>vault.app-id.app-id-path=<br><br># App Role<br>vault.app-role.role-id=<br>vault.app-role.secret-id=<br>vault.app-role.app-role-path=approle<br><br># AWS EC2<br>vault.aws-ec2.role=<br>vault.aws-ec2.role-id=<br>vault.aws-ec2.identity-document=<br>vault.aws-ec2.aws-ec2-path=<br><br># Azure<br>vault.azure-msi.role=<br><br># Kubernetes<br>vault.kubernetes.role=<br>vault.kubernetes.service-account-token-file=<br>vault.kubernetes.kubernetes-path=<br>############################# VAULT #############################</pre><p>So why is this even another Docker image? Why are there init-files and what’s secret.sh doing?</p><p>If you later want to run the automation worker, it will need to be run as the user glsai but the secrets configuration needs to be run as glsjboss . Thus we will separate these two concerns and first run the docker image <em>clarityautomation-secrets:tag </em>as an init container within our K8s cluster. Each container will define the user that it should be run as.</p><p>It would be a strict anti-pattern trying to switch users at runtime using Docker. And believe me that I also tried this. But you will run into security and technical issues.</p><p>Next, <em>secret.sh </em>depends on /inputs/secrets.dat as values put into Illumina’s <em>configure_claritylims_secretutil.sh </em>script. That file should be mounted through a volume mount in K8s so it is available at runtime if you wish to have different secrets in each Kubernetes cluster.</p><p>Further, <em>configure_claritylims_secretutil.sh </em>will modify the files in <em>/opt/gls/clarity/tools/secretutil/conf/. </em>In order to pass on the content to the actual docker container run in K8s, we need to mount it as a volume in Kubernetes. If we do that, the folder will be empty. <em>configure_claritylims_secretutil.sh </em>however requires the files to look as described above. As K8s deployments are often managed in another repo, I did not want to put these files there and decided to mount an empty volume and then move the files as shown above from <em>/init-files </em>into the correct folder <em>/opt/gls/clarity/tools/secretutil/conf/.</em></p><p>After the init-container is started, the correct configuration will be in <em>/opt/gls/clarity/tools/secretutil/conf/ </em>mounted as some volume in K8s.</p><p>Now, the main Docker container will take over. It is build from</p><p><em>Dockerfile.start</em></p><pre>FROM clarityautomationbase:latest<br># our own config and script<br>COPY config.sh /scripts/config.sh<br>COPY start.sh /scripts/start.sh<br>COPY template.csv /template.csv<br>RUN [&quot;chmod&quot;, &quot;+x&quot;, &quot;/scripts/config.sh&quot;]<br>RUN [&quot;chmod&quot;, &quot;+x&quot;, &quot;/scripts/start.sh&quot;]<br># encryption key has to be equal in bartender and bartender-secrets<br>ENV CLARITYSECRET_ENCRYPTION_KEY=REPLACEME<br>USER glsai<br>ENTRYPOINT [&quot;/scripts/start.sh&quot;]</pre><p>which you will need to mount the volume as described above into.</p><p>Another config script now has to run but that is combined with the actual start script</p><pre>#!/bin/bash<br>/opt/gls/clarity/automation_worker/node/bin/configure.sh /inputs/config.dat<br>/opt/gls/clarity/automation_worker/node/bin/automatedinformatics.wrapper.sh start<br>tail /opt/gls/clarity/automation_worker/node/logs/wrapper.log<br># we will just wait for 10 seconds and then tail.<br># as even -F does not seem to work right now and shows error<br># &quot;has been replaced with a remote file. giving up on this name&quot;<br>echo &quot;Waiting for 10s...&quot;<br>sleep 10<br>tail -F /opt/gls/clarity/automation_worker/node/logs/automatedinformatics.log</pre><p><em>/inputs/config.dat </em>is again mounted from a volume as it differs by environment and the actual automation worker is then started and the correct logs are tailed.</p><p>Please reach out to me if you find mistakes as I had to modify the code for public consumption.</p><p>Thanks for reading, Alex.</p><blockquote>As-Is Disclaimer: this blog post makes no warranty of any kind, expressed or implied, regarding the accuracy, adequacy, validity, reliability, or completeness of provided information. The reader is responsible for validating the information’s suitability for their use case.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e265da9408b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Systems Design 1 — Twitter]]></title>
            <link>https://medium.com/@alexanderknips/systems-design-1-twitter-b332f0d8e6b5?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/b332f0d8e6b5</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[systems-engineering]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Tue, 09 Jan 2024 06:32:35 GMT</pubDate>
            <atom:updated>2024-01-09T06:32:35.685Z</atom:updated>
            <content:encoded><![CDATA[<h3>Systems Design 1 — Twitter</h3><p><strong>How would you design a social media app?</strong></p><p>Let’s take a look at a common systems design interview question as posted on <a href="https://igotanoffer.com/blogs/tech/system-design-interviews#file">https://igotanoffer.com/blogs/tech/system-design-interviews#file</a></p><blockquote><em>For this question you’ll typically be asked to design a specific app, such as Twitter, Instagram, etc. For this example, we’ll assume the interviewer asked you to design Twitter.</em></blockquote><p>While it may seem obvious, the very first step is to ask enough questions in order to define the functional and non-functional requirements.</p><p>And please don’t remind me that it’s actually called <strong>X,</strong> I don’t care. Only Elon does.</p><p>Most likely, after asking some questions, we would arrive at a list of features such as</p><h3>Requirement Gathering</h3><h3>Requirements</h3><ol><li>Feed of posts/tweets</li><li>Post a post/tweet (while defining its visibility)</li><li>Comment on a tweet, vote on a tweet</li><li>Follow an account</li><li>Search for tweets</li><li>Moderation through moderators</li></ol><p>As most of these interview questions will focus on scaling as well, let’s define the non-functional requirements</p><h3>Non-functional requirements</h3><ul><li>100M=10⁸ users, @ 1 tweet per day per user on average =&gt; 10⁸ tweets a day</li><li>Search quick across the whole dataset</li><li>Service globally available</li><li>Reading 100s of messages each day per user =&gt; 10¹¹ tweets read each day</li></ul><p>So while the write load is high, the read load is even higher.</p><h3>High Level Design</h3><p>While we could ask even more questions about further features, this is sufficient to get started and design the high level system.</p><p>In this whole series I will be using the C4 Model approach to software design (<a href="https://c4model.com/">https://c4model.com/</a>), that I will quickly summarize here. The C4 model uses 4 levels of software design instead of putting everything in a single diagram:</p><ol><li><strong><em>System Context diagram:</em></strong> <em>The scope is a single software system and this diagram shows how people interact with the software system in scope. A software system is the highest level of abstraction of software.</em></li><li><strong><em>Container diagram:</em></strong> <em>A container (not necessarily a docker container) is a single executable unit of the software system. The Container diagram shows the containers involved in the system context in scope.</em></li><li><strong><em>Component diagram:</em></strong> <em>Within each container we have components that we show in this diagram. Components are the next level of abstraction and are not executable by their own.</em></li><li><strong><em>Code diagram:</em></strong> <em>Here we define how different elements of code (classes, objects, functions, methods, DB tables) interact.</em></li></ol><p>For our example of twitter we will focus some of the more imporant people interacting with the Twitter Software System.</p><h3>System Context Diagram</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/0*QT5cFQ2argbETers" /></figure><p>Right now we have had to make no decisions about the architectural styles yet but in the next step we will need to.</p><p>The main architectural styles that are commonly used are</p><ol><li>Micro-Services, Service-Oriented</li><li>Event-Driven</li><li>Layered</li><li>Data-Centric</li><li>Component-Based</li><li>Domain-Driven</li></ol><p>I intentionally grouped the micro-services style and SOA together as they are similar enough and exist on a spectrum of size but are similar enough.</p><p>Let’s go through the which styles make sense in this particular case.</p><p>A <strong>traditionally layered</strong> approach will not easily fit the scalability requirements as each layer will be hard to scale. If you look more closely within parts of the app, some layering design elements will appear, however. <strong>So a No.</strong></p><p>A <strong>data-centric</strong> approach can work but would solely rely on the scalability of the database used. For a system with such a simple data model there are also no benefits to this approach. <strong>So a No.</strong></p><p>A <strong>component-based</strong> approach improves how the code is organized but traditionally it’s still executed as a single executable. There have been arguments made that code should be written without defining how it is later executed but that’s a whole other topic and right now most people would understand <strong>component-based</strong> architecture to have a single executable and a single data-base. As such, they are not suitable at this scale. <strong>So a No.</strong></p><p>A <strong>domain-driven</strong> approach can work at any scale as it does not by itself offer guidance on the specific implementation but addressing the complexity inherent in sophisticated software domains. Fundamentally twitter is actually not such a domain. It’s rather simple and thus we don’t have a strong reason to apply this technique here. <strong>So a No.</strong></p><p>Let’s talk about the <strong>Event-Driven</strong> and <strong>Micro-Services</strong> (SOA included) approach. In a traditional <strong>Micro-Services</strong> the part of the system responsible for the posts (tweets) would need to push the data to the part of the system responsible for generating the twitter feed. That coupling is unnecessary and can be reduced trough an <strong>Event Driven</strong> approach.</p><p>At parts of the system where integration is more important than separation, we will not use the <strong>Event Driven</strong> approach.</p><h3>Container Diagram</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cWQyl_u2TSlMJ6xW" /></figure><p>By using DDD and separating how <strong>Commands</strong>, <strong>Events</strong> and <strong>Views</strong> fit into the containers to fulfill the requirements we have effectively arrived at an Event-Driven approach featuring <strong>CQRS (Command Query Responsibility Segregration)</strong>. You can read more about <strong>CQRS</strong> at <a href="https://martinfowler.com/bliki/CQRS.html">https://martinfowler.com/bliki/CQRS.html</a>.</p><p>By separating the <strong>Post &amp; Comments</strong> aspect of our system from the <strong>Feeds</strong> aspect, the read and write side are seggregated and the two different containers can be optimized separately. <strong>Post &amp; Comments</strong> can now employ a database that is optimized for heavy reads while <strong>Feeds</strong> can be continuously be updated through events and optimized for heavy reads.</p><p>One of the techniques that we will need to employ at the scales in question is partitioning. Partitioning of the <strong>Feeds</strong> will be easy as each user can have a feed personalized for them. Everything is neatly separated. AI can be used to optimize the feed for achieving the longest time of stay of each user.</p><p>Partitioning <strong>Post &amp; Comments</strong> will be much harder but generally high throughput can be achieved through putting load balancer and queues in front of the <strong>Command</strong> handling of this container.</p><p>The <strong>Post &amp; Comments</strong> is optimized for writes through an API gateway (with load balancers) and a <strong>Queue</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/0*_5w7WE6vAeC6mvLv" /></figure><p>While the feed container is optimized for reads and can be further optimized with caching.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PUasXqGCgo5fBDkB" /></figure><p>This is it for now but more features can be added quite easily to this fundamental design.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b332f0d8e6b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My interactive resume terminal]]></title>
            <link>https://medium.com/@alexanderknips/my-interactive-resume-terminal-15fa5e32c4d2?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/15fa5e32c4d2</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[openai-chatgpt]]></category>
            <category><![CDATA[xtermjs]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[openai-gpt-4]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Mon, 01 Jan 2024 04:55:03 GMT</pubDate>
            <atom:updated>2024-01-01T05:04:16.187Z</atom:updated>
            <content:encoded><![CDATA[<p>How I made an interactive CV that behaves similar to a terminal and is enhanced by AI</p><p><em>The full code is available at</em> <a href="https://github.com/alexknips/nextjs-cv"><em>https://github.com/alexknips/nextjs-cv</em></a><em>.</em></p><p><strong><em>The idea is funky and pretty useless but seamed fun to me: Build a resume website in a completely different way by making it look like a terminal but able to answer like a ChatBot.</em></strong></p><p>After installing npm with the option of your choise (brew, manually, nvm, …), you can create the basic NextJS site by</p><pre>npx create-next-app@latest</pre><p>I chose the following options</p><ul><li>Would you like to use TypeScript? <strong>Yes</strong></li><li>Would you like to use ESLint? <strong>Yes</strong></li><li>Would you like to use Tailwind CSS? <strong>Yes</strong></li><li>Would you like to use `src/` directory? <strong>Yes</strong></li><li>Would you like to use App Router? (recommended) <strong>Yes</strong></li></ul><p>Let’s install xtermjs and some additional plugins. (<a href="https://xtermjs.org/">https://xtermjs.org/</a>)[<a href="https://xtermjs.org/">https://xtermjs.org/</a>] is the project behind many modern JS based terminal implementations such as the terminal in VS Code.</p><pre>npm install xterm xterm-addon-fit xterm-addon-web-links</pre><p>The addon xterm-addon-fit will allow us to refit the xtermjs element to the parent selement and resize the number of columns and rows accordingly. xterm-addon-web-links will allow links in the terminal to be clickable and open in new tabs.</p><p>We will modify the page.tsx to reference XTermCompoment that we will create just after.</p><pre>&quot;use client&quot;;<br>import { useEffect, useState } from &quot;react&quot;;<br>import dynamic from &quot;next/dynamic&quot;;<br>const XTermComponent = dynamic(() =&gt; import(&quot;./XTermComponent&quot;), {<br>  ssr: false,<br>});<br>export default function Home() {<br>  const [isXTermLoaded, setIsXTermLoaded] = useState(false);<br>  const [inProgress, setInProgress] = useState(false);<br>  const handleProgressChanged = (inProgress: boolean) =&gt; {<br>    setInProgress(inProgress);<br>  };<br>  useEffect(() =&gt; {<br>    setIsXTermLoaded(true);<br>  });<br>  return (<br>    &lt;div className=&quot;flex flex-col h-screen&quot;&gt;<br>      &lt;header className=&quot;bg-customBlack flex flex-col items-center justify-center h-24&quot;&gt;<br>        &lt;h1 className=&quot;text-4xl font-bold font-mono text-gray-200&quot;&gt;TITLE&lt;/h1&gt;<br>        &lt;p className=&quot;text-xl text-gray-600 font-mono mt-2&quot;&gt;SUB-TITLE&lt;/p&gt;<br>      &lt;/header&gt;<br>      &lt;div className=&quot;flex flex-grow justify-center items-center bg-customBlack&quot;&gt;<br>        &lt;div className=&quot;terminal-container w-4/6 bg-customBlack h-full p-4&quot;&gt;<br>          {isXTermLoaded &amp;&amp; (<br>            &lt;div<br>              className={`h-full w-full transition-opacity duration-500 ${<br>                inProgress ? &quot;opacity-50&quot; : &quot;opacity-100&quot;<br>              }`}<br>            &gt;<br>              &lt;XTermComponent onProgressChanged={handleProgressChanged} /&gt;<br>            &lt;/div&gt;<br>          )}<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;footer className=&quot;bg-customBlack text-center p-4 text-gray-600&quot;&gt;<br>        &lt;p&gt;© YOUR NAME&lt;/p&gt;<br>      &lt;/footer&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>Here we have used Tailwind CSS styling.</p><p>We define handleProgressChanged as a function whenever the progress changes and we have a long running process going on. In that case we reduce the opacity of the XTermComponent to 50%.</p><p>Now to XTermComponent.tsx</p><pre>import React, { useEffect, useRef } from &quot;react&quot;;<br>import { Terminal } from &quot;xterm&quot;;<br>import { FitAddon } from &quot;xterm-addon-fit&quot;;<br>import { WebLinksAddon } from &quot;xterm-addon-web-links&quot;;<br>import &quot;xterm/css/xterm.css&quot;;<br>interface XTermComponentProps {<br>  onProgressChanged: (inProgress: boolean) =&gt; void;<br>}<br>const XTermComponent: React.FC&lt;XTermComponentProps&gt; = ({<br>  onProgressChanged,<br>}) =&gt; {<br>  const terminalRef = useRef&lt;HTMLDivElement&gt;(null);<br>  const terminal = new Terminal({<br>    cursorBlink: true,<br>    fontSize: 16,<br>    fontFamily: &#39;&quot;Fira Code&quot;, monospace&#39;<br>  });<br>  useEffect(() =&gt; {<br>    let currentLine = &quot;&quot;;<br>    function handleInput(data: string | Uint8Array) {<br>      // Check for Enter key<br>      if (data === &quot;\r&quot;) {<br>        // Process the input when Enter is pressed<br>        processInput(currentLine).then(() =&gt; {<br>          terminal.write(&quot;$ &quot;);<br>          currentLine = &quot;&quot;; // Reset the input buffer<br>        });<br>      } else if (data === &quot;\x7f&quot; || data === &quot;\b&quot;) {<br>        // Handle backspace<br>        if (currentLine.length &gt; 0) {<br>          // Remove the last character from the input buffer<br>          currentLine = currentLine.substring(0, currentLine.length - 1);<br>          // Move the cursor back one space, print a space (to clear the character), and move back again<br>          terminal.write(&quot;\b \b&quot;);<br>        }<br>      } else {<br>        currentLine += data; // Accumulate the input<br>        terminal.write(data); // Echo the input character<br>      }<br>    }<br>    async function processInput(input: string) {<br>      console.log(&quot;User entered:&quot;, input);<br>    }<br>    if (terminalRef.current) {<br>      const webLinksAddon = new WebLinksAddon();<br>      terminal.loadAddon(webLinksAddon);<br>      const fitAddon = new FitAddon();<br>      terminal.loadAddon(fitAddon);<br>      terminal.open(terminalRef.current);<br>      fitAddon.fit();<br>      // Let&#39;s resize the number of rows and columns max every 100ms when the window size changes<br>      let resizeTimeout: string | number | NodeJS.Timeout | undefined;<br>      window.addEventListener(&quot;resize&quot;, () =&gt; {<br>        clearTimeout(resizeTimeout);<br>        resizeTimeout = setTimeout(() =&gt; {<br>          console.log(&quot;Refitting&quot;);<br>          fitAddon.fit();<br>        }, 100); // Adjust the timeout duration as needed<br>      });<br>    }<br>    <br>    terminal.onData(handleInput);<br>    terminal.writeln(&quot;HELLO WORLD.&quot;)<br>    return () =&gt; {<br>      terminal.dispose();<br>    };<br>  }, []);<br>  return &lt;div className=&quot;h-full w-full&quot; ref={terminalRef} /&gt;;<br>};<br>export default XTermComponent;</pre><p>Importantly, we have the components number of rows and columns resizing whenever the window changes.</p><p>So, not only we will use the fitAddon at startup but also whenever the window size changes</p><pre>      terminal.open(terminalRef.current);<br>      fitAddon.fit();<br>      // Let&#39;s resize the number of rows and columns max every 100ms when the window size changes<br>      let resizeTimeout: string | number | NodeJS.Timeout | undefined;<br>      window.addEventListener(&quot;resize&quot;, () =&gt; {<br>        clearTimeout(resizeTimeout);<br>        resizeTimeout = setTimeout(() =&gt; {<br>          console.log(&quot;Refitting&quot;);<br>          fitAddon.fit();<br>        }, 100); // Adjust the timeout duration as needed<br>      });</pre><p>Here, we have implented ENTER and BACKSPACE as special input</p><pre>else if (data === &quot;\x7f&quot; || data === &quot;\b&quot;) {<br>        // Handle backspace<br>        if (currentLine.length &gt; 0) {<br>          // Remove the last character from the input buffer<br>          currentLine = currentLine.substring(0, currentLine.length - 1);<br>          // Move the cursor back one space, print a space (to clear the character), and move back again<br>          terminal.write(&quot;\b \b&quot;);<br>        }<br>      }</pre><p>and only remove a character if the line buffer is not empty.</p><p>Now running the app using npm run build &amp;&amp; npm run start (XtermJs does not play well with hot reloading for me at this point), we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4IZz2W6abjl9Nw-_.png" /></figure><p>Using terminal.write and terminal.writeln we can output more complex welcome messages. But let&#39;s get to the <strong>AI</strong> part and interact with the OpenAI API.</p><p>Before doing that, we need to configure our Assistant in OpenAI.</p><p>In <a href="https://platform.openai.com/assistants">https://platform.openai.com/assistants</a> we create a new assistant</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/0*wFK3BfgD8CmRGjUC.png" /></figure><p>Here we enable Retrieval and add multiple files that contain the information about the resume.</p><p>In the instructions section we write something similar to</p><pre>This GPT offers formal and concise information about Alexander Knips&#39; professional and educational history, skills, and experiences. <br>Use education.md for data about his education.</pre><p>Afterwards you can copy the <strong>assistant_id</strong> from the portal.</p><p>Now we define route.ts in the app/api/openai folder.</p><pre>import { NextRequest, NextResponse } from &quot;next/server&quot;;<br>import OpenAI from &quot;openai&quot;;</pre><pre>export interface OpenAiRequestData {<br>  question: string;<br>}<br>interface Datastore {<br>  threadId: undefined | string;<br>}<br>function delay(ms: number) {<br>  return new Promise((resolve) =&gt; setTimeout(resolve, ms));<br>}<br>const assistantId = &quot;asst_id&quot;;<br>let dataStore: Datastore = { threadId: undefined };<br>export const POST = async (req: NextRequest) =&gt; {<br>  try {<br>    const data: OpenAiRequestData = await req.json();<br>    const openai = new OpenAI();<br>    const threadId = await (async () =&gt; {<br>      if (dataStore.threadId === undefined) {<br>        console.log(&quot;Creating new thread&quot;);<br>        const thread = await openai.beta.threads.create();<br>        dataStore.threadId = thread.id;<br>        return thread.id;<br>      } else {<br>        console.log(&quot;Re-using old thread&quot;);<br>        return dataStore.threadId;<br>      }<br>    })();<br>    await openai.beta.threads.messages.create(threadId, {<br>      role: &quot;user&quot;,<br>      content: data.question,<br>    });<br>    const run = await openai.beta.threads.runs.create(threadId, {<br>      assistant_id: assistantId,<br>    });<br>    await delay(3000);<br>    // Now, you might need to poll for messages<br>    // This is a simplified example; you&#39;ll need to add actual polling logic<br>    let completed = false;<br>    let failed = false;<br>    while (!completed &amp;&amp; !failed) {<br>      let runResponse = await openai.beta.threads.runs.retrieve(<br>      threadId,<br>        run.id<br>      );<br>      console.log(runResponse.status);<br>      // messages = response.; // or however the messages are structured in the response<br>      completed = runResponse.status == &quot;completed&quot;; // Check if the run is completed<br>      failed =<br>        runResponse.status == &quot;failed&quot; ||<br>        runResponse.status == &quot;cancelled&quot; ||<br>        runResponse.status == &quot;cancelling&quot; ||<br>        runResponse.status == &quot;expired&quot; ||<br>        runResponse.status == &quot;requires_action&quot;;<br>      // You might want to add a delay here to avoid hitting the API too frequently<br>      await delay(1000);<br>    }<br>    if (completed) {<br>      const messages = await openai.beta.threads.messages.list(threadId);<br>      const answers = messages.data.filter((x) =&gt; x.role == &quot;assistant&quot;);<br>      console.log(JSON.stringify(messages));<br>      const answerString = answers<br>        .map((x) =&gt;<br>          x.content<br>            .map((y) =&gt; (y.type == &quot;text&quot; ? y.text.value : &quot;&quot;))<br>            .join(&quot;\n&quot;)<br>        )<br>        .join(&quot;\n&quot;)<br>        .replace(/【.*?】/g, &quot;&quot;);<br>      return NextResponse.json({ message: answerString }, { status: 200 });<br>    } else {<br>      return NextResponse.json(<br>        { message: &quot;AI failed to answer&quot; },<br>        { status: 400 }<br>      );<br>    }<br>  } catch (error) {<br>    // Handle errors and send an error response<br>    return NextResponse.json(<br>      { message: &quot;Internal Server Error&quot; },<br>      { status: 500 }<br>    );<br>  }<br>};</pre><p>Here, we define assistantId as a constant. An assistant can also be created on the fly if you wish to do that. But for this use-case this really is not necessary.</p><p>Now, make sure that OPENAI_API_KEY is set in your environment and verify that through echo $OPENAI_API_KEY in your terminal. And install the openai npm package through</p><pre>npm install openai</pre><p>We will retry until the response status is completed or failed (or similar).</p><pre>let completed = false;<br>let failed = false;<br>while (!completed &amp;&amp; !failed) {</pre><p>The code above really does not have proper error handling except for the most obvious case.</p><p>Let’s enhance processInput function to</p><pre>async function processInput(input: string) {<br>    console.log(&quot;User entered:&quot;, input);<br>    if (input.length &gt; 3) await communicateWithAi(input);<br>    else terminal.writeln(&quot;\x1b[33m⚙️ \x1b[3mInput too short for AI to answer\x1b[23m\x1b[0m&quot;);<br>}</pre><pre>async function communicateWithAi(input: string) {<br>  const data = { question: input };<br>  // Make a request to your API route<br>  const resP = fetch(&quot;/api/openai&quot;, {<br>    method: &quot;POST&quot;,<br>    body: JSON.stringify(data),<br>  });<br>  onProgressChanged(true);<br>  try {<br>    const res = await resP;<br>    onProgressChanged(false);<br>    const jsonRes = await res.json();<br>    console.log(JSON.stringify(jsonRes));<br>    const answer = jsonRes.message;<br>    terminal.writeln(&quot;\x1b[32m🤖 \x1b[3m&quot; + answer + &quot;\x1b[23m\x1b[0m&quot;);<br>    terminal.writeln(&quot;&quot;);<br>    console.log(&quot;Assistant responds:&quot;, answer);<br>  } catch {<br>    terminal.writeln(<br>      &quot;\x1b[32m🤖 \x1b[3m Failed to answer this question. Try to ask differently.\x1b[23m\x1b[0m&quot;<br>    );<br>  }<br>}</pre><p>And we call the onProgressChanged function externally to notify the parent if the progress changes. You can use that hook to block user input or show a progess indicator.</p><p>Now we got a running AI in XtermJS</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m3Kn1wGhifScifq8.png" /></figure><p>Full code available at <a href="https://github.com/alexknips/nextjs-cv"><em>https://github.com/alexknips/nextjs-cv</em></a><em>. But please change the resume data to your own ;-).</em></p><p>Happy new year</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oomu9paczgGvfIP1" /><figcaption>Photo by <a href="https://unsplash.com/@goian?utm_source=medium&amp;utm_medium=referral">Ian Schneider</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15fa5e32c4d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Open Telemetry with multiple NestJS and logs in tracing]]></title>
            <link>https://medium.com/@alexanderknips/open-telemetry-with-multiple-nestjs-and-logs-in-tracing-62453e3e6cb2?source=rss-23fa5bd03ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/62453e3e6cb2</guid>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[nestjs]]></category>
            <dc:creator><![CDATA[Alexander Knips]]></dc:creator>
            <pubDate>Fri, 22 Dec 2023 03:15:46 GMT</pubDate>
            <atom:updated>2023-12-22T03:15:46.558Z</atom:updated>
            <content:encoded><![CDATA[<p>The following article will expand further on <a href="https://www.tomray.dev/nestjs-open-telemetry">https://www.tomray.dev/nestjs-open-telemetry</a> and show how it works with a couple of NestJS apps together and also make logs appear in your distributed traces. Follow Tom’s guide for a detailled</p><p>Please refer to <a href="https://github.com/alexknips/nest-open-telemetry">https://github.com/alexknips/nest-open-telemetry</a> for the actual code. Feel free to use the code under the MIT license.</p><p>From there I created multiple NestJS apps to showcase how distributed tracing has real benefits.</p><p>Each NestJS app builds a Docker Container and they can be run together as</p><pre>version: &#39;3.7&#39;<br>services:<br>  jaeger:<br>    image: jaegertracing/all-in-one:latest<br>    ports:<br>      - &quot;16686:16686&quot;<br>      - &quot;4318:4318&quot;<br>      - &quot;4317:4317&quot;<br>    environment:<br>      - LOG_LEVEL=debug<br>    networks:<br>      - jaeger-example<br>  nestapp1:<br>    image: nestapp1<br>    build:<br>      context: nest1<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3001:3000&quot;<br>    environment:<br>      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318<br>    networks:<br>      - jaeger-example<br>    depends_on:<br>      - jaeger<br>      - nestapp2<br>      - nestapp3<br>  nestapp2:<br>    image: nestapp2<br>    build:<br>      context: nest2<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3002:3000&quot;<br>    environment:<br>      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318<br>    networks:<br>      - jaeger-example<br>    depends_on:<br>      - jaeger<br>      - nestapp3<br>  nestapp3:<br>    image: nestapp3<br>    build:<br>      context: nest3<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3003:3000&quot;<br>    environment:<br>      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318<br>    networks:<br>      - jaeger-example<br>    depends_on:<br>      - jaeger<br><br>networks:<br>  jaeger-example:</pre><p>Here we configure one container<em> jaeger </em>for the Jaeger distributed tracing backend. Please be aware that this can be replaced by any other tracing backend of your choice that is compatible with OpenTelemetry, Datadog is another example. I also want to take the chance to remind everyone that OpenTelemetry is a standard and not a service. However te community provides SDKs for the standard. But it does not come with its own tracing backend. We configure a couple of ports for <em>jaeger. 4318</em> is used for OpenTelemetry interaction through <em>http</em> and <em>4317 </em>for OpenTelemetry through <em>grcp</em>. <em>16686 </em>is the port for the Jaeger UI.</p><p>The system of the services can be explained through a container diagram that I created following the <a href="https://c4model.com/">C4 model</a> standard in <a href="https://mermaid.js.org/">Mermaid.JS</a></p><pre>C4Container<br>    title System Contain diagram: OpenTelemetry with multiple NestJS apps<br><br>    Person(user, User, &quot;Some user&quot;, &quot;...&quot;)<br>    System_Ext(tracingBackend, &quot;Distributed Tracing Backend&quot;, &quot;This can be Jaeger, Datadog or any other tracing backend compatible with the OTEL standard.&quot;)<br><br>    Container_Boundary(c1, &quot;Apps&quot;) {<br>        Container(nest1, &quot;NestJS 1&quot;, &quot;NestJS&quot;)<br>        Container(nest2, &quot;NestJS 2&quot;, &quot;NestJS&quot;)<br>        Container(nest3, &quot;NestJS 3&quot;, &quot;NestJS&quot;)<br>        Rel(nest2, nest3, &quot;Uses&quot;, &quot;HTTPS&quot;)<br>        Rel(nest1, nest2, &quot;Uses&quot;, &quot;HTTPS&quot;)<br>        Rel(user, nest1, &quot;Uses&quot;, &quot;HTTPS&quot;)<br>    }<br><br>    Rel(user, nest1, &quot;Uses&quot;, &quot;HTTPS&quot;)<br>    Rel(nest1, tracingBackend, &quot;Uses&quot;, &quot;OTEL&quot;)<br>    Rel(nest2, tracingBackend, &quot;Uses&quot;, &quot;OTEL&quot;)<br>    Rel(nest3, tracingBackend, &quot;Uses&quot;, &quot;OTEL&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gmgN_qyZNEfabt7t-lTcbw.png" /></figure><p>So in this example the User calls the micro-service NestJS-1 which in turn calls NestJS-2 which in turn calls NestJS-3. Let’s not go into whether this is a good design or but how distributed tracing can be set up and help debug performance issues and how one call leads to a long chain of interactions between micro-services.</p><p>Each NestJS micro-service contains a<em> tracer.ts </em>to connect to the tracing backend through the OpenTelemetry protocol.</p><pre>&#39;use strict&#39;;<br><br>import { OTLPTraceExporter } from &#39;@opentelemetry/exporter-trace-otlp-grpc&#39;;<br>import { Resource } from &#39;@opentelemetry/resources&#39;;<br>import { NodeSDK } from &#39;@opentelemetry/sdk-node&#39;;<br>import { SemanticResourceAttributes } from &#39;@opentelemetry/semantic-conventions&#39;;<br>import { NestInstrumentation } from &#39;@opentelemetry/instrumentation-nestjs-core&#39;;<br>import { HttpInstrumentation } from &#39;@opentelemetry/instrumentation-http&#39;;<br>import { ExpressInstrumentation } from &#39;@opentelemetry/instrumentation-express&#39;;<br><br>// Configure the SDK to export telemetry data to the console<br>// Enable all auto-instrumentations from the meta package<br>const exporterOptions = {<br>  url: &#39;http://jaeger:4317&#39;, // grcp<br>};<br><br>const traceExporter = new OTLPTraceExporter(exporterOptions);<br><br>const sdk = new NodeSDK({<br>  traceExporter,<br>  instrumentations: [<br>    // getNodeAutoInstrumentations(),<br>    new NestInstrumentation(),<br>    new HttpInstrumentation(),<br>    new ExpressInstrumentation(),<br>  ],<br>  resource: new Resource({<br>    [SemanticResourceAttributes.SERVICE_NAME]: &#39;nest1&#39;,<br>  }),<br>});<br><br>// initialize the SDK and register with the OpenTelemetry API<br>// this enables the API to record telemetry<br>sdk.start();<br><br>// gracefully shut down the SDK on process exit<br>process.on(&#39;SIGTERM&#39;, () =&gt; {<br>  sdk<br>    .shutdown()<br>    .then(() =&gt; console.log(&#39;Tracing terminated&#39;))<br>    .catch((error) =&gt; console.log(&#39;Error terminating tracing&#39;, error))<br>    .finally(() =&gt; process.exit(0));<br>});<br><br>export default sdk;</pre><p>After the imports for the OpenTelemetry SDK, we configure the exporter options. Here you can also choose the HTTP backend. We then wrap the actual exporter.</p><p>Instrumentations are configured through</p><pre>instrumentations: [<br>    // getNodeAutoInstrumentations(),<br>    new NestInstrumentation(),<br>    new HttpInstrumentation(),<br>    new ExpressInstrumentation(),<br>  ],</pre><p>and the resource defines the name of our micro-service to appear in the tracing. Afterwards the tracing is started and properly awaited and logged.</p><p>The actual startup of the NestJS app is <em>main.ts</em></p><pre>import { NestFactory } from &#39;@nestjs/core&#39;;<br>import { AppModule } from &#39;./app.module&#39;;<br>import tracer from &#39;./tracer&#39;;<br><br>async function bootstrap() {<br>  // Start SDK before nestjs factory create<br>  await tracer.start();<br><br>  const app = await NestFactory.create(AppModule);<br>  await app.listen(3000);<br>}<br>bootstrap();</pre><p>We can now start all apps together through executing</p><pre>docker compose up --build</pre><p>Let’s take a look at Jaeger at <a href="http://localhost:16686/">http://localhost:16686/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tvNNt3GVBeLj6R8_4WPHrw.png" /></figure><p>You can see how the call to Nest1 goes to Nest2 and then to Nest3 to return again to Nest2 and then to Nest1.</p><p>The long bars represent Spans as explained in detail in the OpenTelemetry docs <a href="https://opentelemetry.io/docs/concepts/signals/traces/">https://opentelemetry.io/docs/concepts/signals/traces/</a>.</p><h3>Open Telemetry Logs</h3><p>The OpenTelemetry specification allows us to enhance the traces with logs, also called events and explained in detail in <a href="https://opentelemetry.io/docs/concepts/signals/logs/">https://opentelemetry.io/docs/concepts/signals/logs/</a>.</p><p>While a Span represents a process with a starting and ending time, an Event present a single point in time. As such logs are events.</p><p>In the repo linked above we also show how to enhance the traces with logs. This is then represented as a vertical bar on the trace as seen below. The time shown here as <em>5.28ms</em> is relative to the parent span.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d0kqp-xnLOqbgan0em3_Mg.png" /></figure><p>How do we achieve this?</p><p>We create a custom logger</p><pre>import { Injectable, Scope, ConsoleLogger } from &#39;@nestjs/common&#39;;<br>import api from &#39;@opentelemetry/api&#39;;<br><br>@Injectable({ scope: Scope.TRANSIENT })<br>export class MyLoggerService extends ConsoleLogger {<br>  customLog(message: string) {<br>    const activeSpan = api.trace.getSpan(api.context.active())<br>    activeSpan.addEvent(message)<br>    this.log(message);<br>  }<br>}</pre><p>that takes the log message and injects it into the active span as an event. This is obviously not production-ready but can easily be extended and modified.</p><p>Just make sure there are two different approaches and goals:</p><ol><li>Inject logs into the distributed tracing as seen above</li><li>Give each log a tracing id such that you can find the trace that belong to a specific log message (sometimes in a different system). Pino instrumentation (<a href="https://www.npmjs.com/package/@opentelemetry/instrumentation-pino">https://www.npmjs.com/package/@opentelemetry/instrumentation-pin) </a>achieves this but not the first.</li></ol><p>For production-ready you would probably customize a logger such as pino instead of extending <em>ConsoleLogger</em>.</p><p>Thanks for reading, Alex.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62453e3e6cb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>