> ## 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.

# Configuring the SDK

> Configure the infrastack.ai Javascript SDK to suit your needs

#### What you will learn

* Configuring the infrastack.ai Javascript SDK for your needs

#### Prerequisites

* An application using the infrastack.ai Javascript SDK `@infrastack/otel`

### Overview

Our Javascript SDK provides flexibility to developers to configure the SDK. There are 2 ways to configure the SDK:

* Via environment variables
* Via code

<Warning> What you provide in the code will override what you provide in the environment variables </Warning>

### Setting the API Key

API key is required when you initialize the SDK to use it with infrastack.ai.

To set the API key, you can set the `apiKey` option in the `Infrastack.init` function or set the `INFRASTACK_API_KEY` environment variable.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    apiKey: 'sk-1*****f5af',
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  INFRASTACK_API_KEY=sk-1*****f5af
  ```

  ```bash terminal theme={null}
  export INFRASTACK_API_KEY=sk-1*****f5af
  ```
</CodeGroup>

***

### Setting the service name

The service name is used to identify the service in the [infrastack.ai dashboard](https://app.infrastack.ai).

To set the service name, you can set the `serviceName` option in the `Infrastack.init` function or set the `OTEL_SERVICE_NAME` environment variable.
If you do not provide a service name from either of the options, it will generate a random service name.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  OTEL_SERVICE_NAME=my-service
  ```

  ```bash terminal theme={null}
  export OTEL_SERVICE_NAME=my-service
  ```
</CodeGroup>

***

### Setting the service version

The service version is used to identify the version of the service in the [infrastack.ai dashboard](https://app.infrastack.ai).
It becomes useful to capture deployment information and other versioning information.

If you do not provide a service version from either of the options, it will default to `0.0.1`.

To set the service version, you can set the `serviceVersion` option in the `Infrastack.init` function or set the `OTEL_SERVICE_VERSION` environment variable.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    serviceVersion: '1.0.0',
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  OTEL_SERVICE_VERSION=1.0.0
  ```

  ```bash terminal theme={null}
  export OTEL_SERVICE_VERSION=1.0.0
  ```
</CodeGroup>

***

### Development mode

Development mode is disabled by default. In that case, it will not log anything to the console and will use `BatchSpanProcessor` for span exports.

Visit the Documentation [OpenTelemetry - Picking the right span processor](https://opentelemetry.io/docs/languages/js/instrumentation/#picking-the-right-span-processor) for more information.

To toggle development mode, you can set the `developmentMode` option to `true` in the `Infrastack.init` function or set the `INFRASTACK_DEVELOPMENT_MODE` environment variable to `true`.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    developmentMode: true,
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  INFRASTACK_DEVELOPMENT_MODE=true
  ```

  ```bash terminal theme={null}
  export INFRASTACK_DEVELOPMENT_MODE=true
  ```
</CodeGroup>

<Warning> Development mode is only recommended for development purposes. It is not recommended for production. </Warning>

***

### Disabling the SDK logs

By default, when you set up the SDK and run your application, you will see logs in the console as below:

```
Exporter endpoint is set as: https://collector.infrastack.ai
Found an API Key: sk-1*****f5af
Service name is set as: infrastack-example
Service version is set as: 0.0.1
Application is now instrumented with infrastack.ai
```

To disable them for cleaner startup logs for your application, you can set the `logsEnabled` option to `false` in the `Infrastack.init` function **or** set the `INFRASTACK_LOGS_ENABLED` environment variable to `false`.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    logsEnabled: false,
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  INFRASTACK_LOGS_ENABLED=false
  ```

  ```bash terminal theme={null}
  export INFRASTACK_LOGS_ENABLED=false
  ```
</CodeGroup>

***

### Tags

You can define a set of tags to enrich your spans. It is also recommended to add the tag for your environment (Production, Staging, Development etc.)

All of these tags will be added to every span that is created.

To set the tags, you can set the `tags` option in the `Infrastack.init` function or set the `INFRASTACK_TAGS` environment variable.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack, Tag } from '@infrastack/otel'
  const tags: Tag[] = [{ key: 'environment', value: 'production' }]
  Infrastack.init({
    tags: tags,
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  INFRASTACK_TAGS='[{"key": "environment", "value": "production"}]'
  ```

  ```bash terminal theme={null}
  export INFRASTACK_TAGS='[{"key": "environment", "value": "production"}]'
  ```
</CodeGroup>

***

### Configuring the exporter endpoint

By default, the SDK will use the `infrastack.ai` collector endpoint. However, it can be overriden to use a different endpoint.

To set the endpoint, you can set the `endpoint` option in the `Infrastack.init` function or set the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    endpoint: 'http://localhost:4317',
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
  ```

  ```bash terminal theme={null}
  export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
  ```
</CodeGroup>

***

### Configuring the exporter protocol

By default, the SDK will use the `grpc` protocol. However, it can be overriden to use a different protocol.

To set the protocol, you can set the `protocol` option in the `Infrastack.init` function or set the `OTEL_EXPORTER_OTLP_PROTOCOL` environment variable.

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    protocol: Protocol.HTTP,
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  OTEL_EXPORTER_OTLP_PROTOCOL=http
  ```

  ```bash terminal theme={null}
  export OTEL_EXPORTER_OTLP_PROTOCOL=http
  ```
</CodeGroup>

By doing so, the SDK will use the `http` protocol to export the spans to the collector.

<Warning> If you are using your own collector, you will need to set the correct endpoint and protocol. </Warning>

### Disabling instrumentations

You can disable specific instrumentations by setting the `disabledInstrumentations` option in the `Infrastack.init` function or set the `INFRASTACK_DISABLED_INSTRUMENTATIONS` environment variable.
List of instrumentations can be found [here](https://github.com/infrastackai/infrastack/blob/96334ec455956c16837f276b1a5450ffc57ba17d/packages/javascript/otel/src/configuration/index.ts#L25).

<CodeGroup>
  ```javascript instrumentation.ts theme={null}
  import { Infrastack } from '@infrastack/otel'
  Infrastack.init({
    disabledInstrumentations: [Instrumentation.HTTP],
    serviceName: 'my-service'
  })
  ```

  ```properties .env theme={null}
  INFRASTACK_DISABLED_INSTRUMENTATIONS='["http"]'
  ```

  ```bash terminal theme={null}
  export INFRASTACK_DISABLED_INSTRUMENTATIONS='["http"]'
  ```
</CodeGroup>
