> ## Documentation Index
> Fetch the complete documentation index at: https://docs.infrastack.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate OpenTelemetry for Express.js with infrastack.ai

> Learn how to instrument your Express.js application with infrastack.ai and OpenTelemetry for comprehensive observability and performance insights.

export const JsIcon = ({size = "24"}) => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width={size} height={size} fill="#708090"><path d="M 43.335938 4 L 6.667969 4 C 5.195313 4 4 5.195313 4 6.667969 L 4 43.332031 C 4 44.804688 5.195313 46 6.667969 46 L 43.332031 46 C 44.804688 46 46 44.804688 46 43.335938 L 46 6.667969 C 46 5.195313 44.804688 4 43.335938 4 Z M 27 36.183594 C 27 40.179688 24.65625 42 21.234375 42 C 18.140625 42 15.910156 39.925781 15 38 L 18.144531 36.097656 C 18.75 37.171875 19.671875 38 21 38 C 22.269531 38 23 37.503906 23 35.574219 L 23 23 L 27 23 Z M 35.675781 42 C 32.132813 42 30.121094 40.214844 29 38 L 32 36 C 32.816406 37.335938 33.707031 38.613281 35.589844 38.613281 C 37.171875 38.613281 38 37.824219 38 36.730469 C 38 35.425781 37.140625 34.960938 35.402344 34.199219 L 34.449219 33.789063 C 31.695313 32.617188 29.863281 31.148438 29.863281 28.039063 C 29.863281 25.179688 32.046875 23 35.453125 23 C 37.878906 23 39.621094 23.84375 40.878906 26.054688 L 37.910156 27.964844 C 37.253906 26.789063 36.550781 26.328125 35.453125 26.328125 C 34.335938 26.328125 33.628906 27.039063 33.628906 27.964844 C 33.628906 29.109375 34.335938 29.570313 35.972656 30.28125 L 36.925781 30.691406 C 40.171875 32.078125 42 33.496094 42 36.683594 C 42 40.117188 39.300781 42 35.675781 42 Z" /></svg>;

export const GithubIcon = ({size = "24"}) => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width={size} height={size} fill="#708090"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" /></svg>;

#### What you will learn

* How to set up your environment
* How to <Tooltip tip="To make an application observable, it must be instrumented: That is, code from the system's components must emit traces, metrics, and logs."> **instrument** </Tooltip> your application using the infrastack.ai SDK or the official OpenTelemetry SDK

#### Prerequisites

* An [infrastack.ai](https://app.infrastack.ai) account to get your API key
* An Express.js application

<CardGroup cols={1}>
  <Card title="Follow the example project on GitHub" href="https://github.com/infrastackai/infrastack/tree/main/examples/expressjs" icon={<GithubIcon />} />
</CardGroup>

### Getting started

<Tabs>
  <Tab title="Using the Infrastack SDK">
    You can use the Infrastack SDK to instrument your Express.js application with a single line of code.

    <Steps>
      <Step title="Install the NPM package">
        <CodeGroup>
          ```bash npm theme={null}
          npm install @infrastack/otel
          ```

          ```bash yarn theme={null}
          yarn add @infrastack/otel
          ```

          ```bash pnpm theme={null}
          pnpm add @infrastack/otel
          ```
        </CodeGroup>
      </Step>

      <Step title="Prepare your environment">
        Export your API key as an environment variable.

        ```bash theme={null}
        export INFRASTACK_API_KEY=sk-1*************************f5af
        ```

        <Info>Or you can set the environment variables in your `.env` file by using the [dotenv](https://www.npmjs.com/package/dotenv) library.</Info>
      </Step>

      <Step title="Create instrumentation.ts">
        ```javascript instrumentation.ts theme={null}
        import { Infrastack } from "@infrastack/otel"; 
        Infrastack.init({
          serviceName:"YOUR_SERVICE_NAME",
        });
        ```

        <Info>If you don't provide a service name, we will create a random name for you.</Info>
      </Step>

      <Step title="Build and run your application">
        Run your Express.js application with the node required command.

        ```bash theme={null}
        npx tsc && node -r ./dist/instrumentation.js dist/index.js
        ```

        <Tip>After creating some traffic, you can check your data from the [infrastack.ai](https://app.infrastack.ai) dashboard.</Tip>
      </Step>
    </Steps>
  </Tab>

  <Tab title="Using the OpenTelemetry SDK">
    You can use the [OpenTelemetry Javascript SDK](https://opentelemetry.io/docs/languages/js/) to instrument your Express.js application.

    <Steps>
      <Step title="Install the NPM packages">
        <CodeGroup>
          ```bash npm theme={null}
          npm install --save @opentelemetry/sdk-node
          npm install --save @opentelemetry/auto-instrumentations-node
          npm install --save @opentelemetry/exporter-trace-otlp-grpc
          ```

          ```bash yarn theme={null}
          yarn add @opentelemetry/sdk-node
          yarn add @opentelemetry/auto-instrumentations-node
          yarn add @opentelemetry/exporter-trace-otlp-grpc
          ```

          ```bash pnpm theme={null}
          pnpm add @opentelemetry/sdk-node
          pnpm add @opentelemetry/auto-instrumentations-node
          pnpm add @opentelemetry/exporter-trace-otlp-grpc
          ```
        </CodeGroup>
      </Step>

      <Step title="Prepare your environment">
        ```bash theme={null}
        export OTEL_EXPORTER_OTLP_ENDPOINT="https://collector.infrastack.ai"
        export OTEL_EXPORTER_OTLP_HEADERS="infrastack-api-key={{YOUR_INFRASTACK_API_KEY}}"
        ```

        <Info>Or you can set the environment variables in your `.env` file by using the [dotenv](https://www.npmjs.com/package/dotenv) library.</Info>
      </Step>

      <Step title="Create instrumentation.ts">
        Here, you will create a file that will be used to instrument your application. You will have lower level control over the instrumentation process.

        ```javascript instrumentation.ts theme={null}
        import { NodeSDK } from '@opentelemetry/sdk-node';
        import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
        import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
        import { Resource } from '@opentelemetry/resources';
        import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
        import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base';
        import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';

        // Exporter options with compression set to Gzip
        const exporterOptions = {
          compression: CompressionAlgorithm.GZIP,
        };

        // Trace exporter initialization
        const traceExporter = new OTLPTraceExporter(exporterOptions);

        // SDK initialization with exporter, auto-instrumentation, and resource attributes
        const sdk = new NodeSDK({
          traceExporter,
          instrumentations: [getNodeAutoInstrumentations()],
          resource: new Resource({
            [ATTR_SERVICE_NAME]: "{{YOUR_SERVICE_NAME}}",
            [ATTR_SERVICE_VERSION]: "{{YOUR_SERVICE_VERSION}}",
          }),
        });

        // Start the SDK and register it with the OpenTelemetry API
        sdk.start();

        // Graceful shutdown of SDK on process exit
        process.on("SIGTERM", () => {
          sdk
            .shutdown()
            .then(() => console.log("Tracing terminated"))
            .catch((error) => console.log("Error terminating tracing", error))
            .finally(() => process.exit(0));
        });
        ```
      </Step>

      <Step title="Build and run your application">
        Run your Express.js application with the [node required](https://nodejs.org/api/cli.html#cli_r_require_module) command.

        ```bash theme={null}
        npx tsc && node -r ./dist/instrumentation.js dist/index.js
        ```

        <Tip>After creating some traffic, you can check your data from the [infrastack.ai](https://app.infrastack.ai) dashboard.</Tip>
      </Step>
    </Steps>
  </Tab>
</Tabs>

### Next steps

<CardGroup cols={2}>
  <Card title="Javascript SDK Reference" href="/sdk-reference/javascript/configuration" icon={<JsIcon />} />

  <Card title="Explore the Express.js example project" href="https://github.com/infrastackai/infrastack/tree/main/examples/expressjs" icon={<GithubIcon />} />
</CardGroup>
