OpenTelemetry Collector ElasticBeanStalk
Tags: opentelemetry, otelcoll, observabilidade, elasticbeanstalk
Published: 2024-06-28 14:27:15

Este guia descreve os passos para implementar o OpenTelemetry Collector no Elastic Beanstalk  utilizando [.ebextension](https://docs.aws.amazon.com/pt_br/elasticbeanstalk/latest/dg/customize-containers-ec2.html). O processo inclui baixar o binário do OpenTelemetry Collector, configurar um serviço do Linux, copiar o arquivo de configuração e iniciar o serviço.

### 1. Criar a Pasta `.ebextensions`

Primeiro, crie um diretório chamada `.ebextensions` no diretório raiz da sua aplicação.

### 2. Baixar o Binário do OpenTelemetry Collector

Crie um arquivo de configuração chamado `01_download_collector.config` no diretório `.ebextensions` para baixar o binário do OpenTelemetry Collector.

```yaml
files:
  "/opt/otelcol-contrib/otelcol-contrib":
    mode: "000755"
    owner: ec2-user
    group: ec2-user
    source: https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.103.1/otelcol-contrib_0.103.1_linux_arm64.tar.gz

commands:
  extract_otel_collector:
    command: "tar -xvzf /opt/otelcol-contrib/otelcol-contrib -C /opt/otelcol-contrib/"
```

### 3. Criar o Serviço Linux para OtelColl

Crie um arquivo de configuração chamado `02_create_service.config` para configurar um serviço systemd para o OpenTelemetry Collector.

```yaml
files:
  "/etc/systemd/system/otelcol-contrib.service":
    mode: "000644"
    owner: root
    group: root
    content: |
      [Unit]
      Description=OpenTelemetry Collector Contrib
      After=network.target

      [Service]
      User=ec2-user
      ExecStart=/opt/otelcol-contrib/otelcol-contrib --config=/etc/otelcol-contrib/config.yaml
      Restart=always

      [Install]
      WantedBy=multi-user.target

commands:
  reload_systemd:
    command: "sudo systemctl daemon-reload"
  enable_service:
    command: "sudo systemctl enable otelcol-contrib"
```

### 4. Copiar o Arquivo de Configuração

Crie um arquivo de configuração chamado `03_copy_config.config` para copiar o arquivo de configuração do OpenTelemetry Collector.

```yaml
files:
  "/etc/otelcol-contrib/config.yaml":
    mode: "000644"
    owner: ec2-user
    group: ec2-user
    content: |
      receivers:
        otlp:
          protocols:
            grpc:
              endpoint: "0.0.0.0:4317"
            http:
              endpoint: "0.0.0.0:4318"

        prometheus:
          config:
            scrape_configs:
              - job_name: otelcoll-metrics
                scrape_interval: 60s
                static_configs:
                  - targets: ['localhost:8888']

      processors:
        batch:
          send_batch_max_size: 1000
          timeout: 60s
          send_batch_size: 800

        memory_limiter:
          check_interval: 1s
          limit_percentage: 70
          spike_limit_percentage: 30

        resourcedetection:
          detectors: [env, elastic_beanstalk, ec2]
          timeout: 2s
          override: false

        attributes/logs:
          actions:
            - action: insert
              key: loki.attribute.labels
              value: event.domain, event.name
            - action: insert
              key: loki.format
              value: json

        resource/logs:
          attributes:
            - action: insert
              key: loki.resource.labels
              value: service.name

      exporters:
        loki:
          endpoint: http://ENPOINT:3100/loki/api/v1/push
          tls:
            insecure: true

        otlphttp/tempo:
          endpoint: http://ENPOINT:4318
          tls:
            insecure: true

        prometheusremotewrite:
          endpoint: http://ENPOINT:9009/api/v1/push
          resource_to_telemetry_conversion:
            enabled: true
          target_info:
            enabled: false

      service:
        telemetry:
          logs:
            level: info
          metrics:
            address: 0.0.0.0:8888

      pipelines:
        traces:
          receivers: [otlp]
          processors: [resourcedetection, batch, memory_limiter]
          exporters: [otlphttp/tempo]

        metrics:
          receivers: [otlp, prometheus]
          processors: [resourcedetection, batch, memory_limiter]
          exporters: [prometheusremotewrite]

        logs:
          receivers: [otlp]
          processors: [resourcedetection, resource/logs, attributes/logs, batch, memory_limiter]
          exporters: [loki]
```

### 5. Iniciar o Serviço

Crie um arquivo de configuração chamado `04_start_service.config` para iniciar o serviço do OpenTelemetry Collector.

```yaml
commands:
  start_otel_service:
    command: |
      sudo systemctl start otelcol-contrib
      sudo systemctl status otelcol-contrib
```

### Resumo

Esta abordagem garante que o coletor inicie corretamente e comece a enviar dados para o Grafana após o lançamento da instância.

### Referencia
- https://docs.aws.amazon.com/pt_br/elasticbeanstalk/latest/dg/customize-containers-ec2.html