# Send OpenTelemetry SDK traces to the collector

[Better Stack collector](https://betterstack.com/docs/logs/collector/) gives you eBPF traces and metrics for all your services without code changes. Send OpenTelemetry SDK traces, logs and metrics through the same collector for more control and flexibility. They show up alongside the eBPF traces.

## Enable OpenTelemetry in Better Stack collector

Open the OpenTelemetry ports on the collector:

[code-tabs]
```sh
[label Kubernetes]
# Enable for existing collector
helm repo update
helm upgrade better-stack-collector better-stack/collector \
  --reuse-values \
  --set collectOtel.grpcPort=4317 \
  --set collectOtel.httpPort=4318

# Deploy new collector with OpenTelemetry forwarding
helm repo add better-stack https://betterstackhq.github.io/collector-helm-chart
helm repo update
helm install better-stack-collector better-stack/collector \
  --set collector.env.COLLECTOR_SECRET="$COLLECTOR_SECRET" \
  --set collectOtel.grpcPort=4317 \
  --set collectOtel.httpPort=4318
```
```sh
[label Docker Compose]
curl -sSL https://raw.githubusercontent.com/BetterStackHQ/collector/main/install.sh | \
  COLLECTOR_SECRET="$COLLECTOR_SECRET" \
  COLLECT_OTEL_GRPC_PORT=4317 \
  COLLECT_OTEL_HTTP_PORT=4318 bash
```
```sh
[label Docker Swarm]
curl -sSL https://raw.githubusercontent.com/BetterStackHQ/collector/refs/heads/main/deploy-to-swarm.sh | \
    MANAGER_NODE=root@swarm-manager COLLECTOR_SECRET="$COLLECTOR_SECRET" \
    COLLECT_OTEL_GRPC_PORT=4317 \
    COLLECT_OTEL_HTTP_PORT=4318 bash
```
[/code-tabs]

Then turn it on in Better Stack:

1. Go to [Sources](https://telemetry.betterstack.com/team/0/sources) → your collector → **Configure** → **Ingest**.
2. Enable the **OpenTelemetry SDK traces** checkbox.

All services can now send OpenTelemetry data to Better Stack via the open ports.

## Instrument your services

Instrument your services with OpenTelemetry:

- In Kubernetes, [auto-instrument services using OpenTelemetry Operator](#automatically-instrument-services-in-kubernetes).
- Anywhere else, follow the [official OpenTelemetry guide](https://opentelemetry.io/docs/languages/) for your service's language. OpenTelemetry offers zero-code and manual SDK instrumentation depending on the language.

[info]
#### Already have your services instrumented with OpenTelemetry?

Skip this step and [send the OpenTelemetry SDK data to Collector](#send-opentelemetry-data-to-better-stack-collector).
[/info]

### Automatically instrument services in Kubernetes

Install [OpenTelemetry Operator](https://opentelemetry.io/docs/platforms/kubernetes/operator/) to your cluster.

Create an `Instrumentation` resource to make sure OpenTelemetry data is sent to Better Stack collector:

```yaml
[label instrumentation.yaml]
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: better-stack
spec:
  exporter:
    endpoint: http://better-stack-collector-otlp.namespace-with-bs-collector.svc:4318
  env:
    - name: OTEL_EXPORTER_OTLP_PROTOCOL
      value: http/protobuf
```

Annotate your workloads to enable auto-instrumentation:

```yaml
[label deployment.yaml]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    metadata:
      annotations:
        instrumentation.opentelemetry.io/inject-python: "better-stack"
```

[info]
#### Use the correct language for your service

Replace `inject-python` with the annotation for your language: `inject-java`, `inject-nodejs`, `inject-dotnet`, or `inject-go`.  
See all available languages in the [Operator documentation](https://opentelemetry.io/docs/platforms/kubernetes/operator/automatic/#configure-automatic-instrumentation).
[/info]

Then [add service and host attributes](#add-service-and-host-attributes) to the same workloads.

## Send OpenTelemetry data to Better Stack collector

Configure your OpenTelemetry SDK to send data to the Better Stack collector. 
In Kubernetes, use the node's IP address to reach the collector's host ports:

[code-tabs]
```yaml
[label HTTP]
env:
  - name: NODE_IP
    valueFrom:
      fieldRef:
        fieldPath: status.hostIP
  - name: OTEL_EXPORTER_OTLP_ENDPOINT
    value: "http://$(NODE_IP):4318"
```
```yaml
[label gRPC]
env:
  - name: NODE_IP
    valueFrom:
      fieldRef:
        fieldPath: status.hostIP
  - name: OTEL_EXPORTER_OTLP_ENDPOINT
    value: "http://$(NODE_IP):4317"
```
[/code-tabs]

For Docker Compose and Docker Swarm, point your services to the Collector via localhost, for example `http://localhost:4318` for HTTP or `http://localhost:4317` for gRPC.

## Add service and host attributes

Set `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` on all services instrumented with OpenTelemetry SDK. `service.name` groups traces and logs by service in Better Stack dashboards, while `host.name` and `container.id` connect SDK-generated telemetry with eBPF-generated data from the same workload.

[code-tabs]
```yaml
[label Kubernetes]
env:
  - name: OTEL_SERVICE_NAME
    value: "my-namespace/Deployment/my-application"
  - name: POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  - name: NODE_NAME
    valueFrom:
      fieldRef:
        fieldPath: spec.nodeName
  - name: OTEL_RESOURCE_ATTRIBUTES
    value: "host.name=$(NODE_NAME),container.id=my-namespace/$(POD_NAME)/my-application"
```
```bash
[label Docker]
export OTEL_SERVICE_NAME="my-application"
export OTEL_RESOURCE_ATTRIBUTES="host.name=my-host-123,container.id=my-application-0a1b2c3d4e"
```
[/code-tabs]

[success]
#### OpenTelemetry traces are now flowing into Better Stack

- View traces in [Live tail](https://telemetry.betterstack.com/team/0/tail ";_blank")
- Check out the [OpenTelemetry Tracing dashboard](https://betterstack.com/dashboards/tracing-open-telemetry) to analyze traces coming from eBPF alongside traces coming from the OpenTelemetry SDK.
[/success]

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
