Ruby on Rails

[Edit this page](https://github.com/ClickHouse/clickhouse-docs/blob/main/docs/use-cases/observability/clickstack/ingesting-data/sdks/ruby.md)

This guide integrates:

|  |  |  |
| --- | --- | --- |
| ✖️ Logs ✖️ ️️Metrics ✅ Traces | | |

*To send logs to ClickStack, please send logs via the [OpenTelemetry collector](/docs/use-cases/observability/clickstack/ingesting-data/otel-collector).*

## Getting started[​](#getting-started "Direct link to Getting started")

### Install OpenTelemetry packages[​](#install-otel-packages "Direct link to Install OpenTelemetry packages")

Use the following command to install the OpenTelemetry package.

```
bundle add opentelemetry-sdk opentelemetry-instrumentation-all opentelemetry-exporter-otlp
```

### Configure OpenTelemetry + logger formatter[​](#configure-otel-logger-formatter "Direct link to Configure OpenTelemetry + logger formatter")

Next, you'll need to initialize the OpenTelemetry tracing instrumentation
and configure the log message formatter for Rails logger so that logs can be
tied back to traces automatically. Without the custom formatter, logs won't
be automatically correlated together in ClickStack.

In `config/initializers` folder, create a file called `hyperdx.rb` and add the
following to it:

```
# config/initializers/hyperdx.rb

require 'opentelemetry-exporter-otlp'
require 'opentelemetry/instrumentation/all'
require 'opentelemetry/sdk'

OpenTelemetry::SDK.configure do |c|
  c.use_all() # enables all trace instrumentation!
end

Rails.application.configure do
  Rails.logger = Logger.new(STDOUT)
  # Rails.logger.log_level = Logger::INFO # default is DEBUG, but you might want INFO or above in production
  Rails.logger.formatter = proc do |severity, time, progname, msg|
    span_id = OpenTelemetry::Trace.current_span.context.hex_span_id
    trace_id = OpenTelemetry::Trace.current_span.context.hex_trace_id
    if defined? OpenTelemetry::Trace.current_span.name
      operation = OpenTelemetry::Trace.current_span.name
    else
      operation = 'undefined'
    end

    { "time" => time, "level" => severity, "message" => msg, "trace_id" => trace_id, "span_id" => span_id,
      "operation" => operation }.to_json + "\n"
  end

  Rails.logger.info "Logger initialized !! 🐱"
end
```

### Configure environment variables[​](#configure-environment-variables "Direct link to Configure environment variables")

Afterwards you'll need to configure the following environment variables in your shell to ship telemetry to ClickStack via the OpenTelemetry collector:

- Managed ClickStack- ClickStack Open Source

```
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_SERVICE_NAME='<NAME_OF_YOUR_APP_OR_SERVICE>' \
```

```
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_SERVICE_NAME='<NAME_OF_YOUR_APP_OR_SERVICE>' \
OTEL_EXPORTER_OTLP_HEADERS='authorization=<YOUR_INGESTION_API_KEY>'
```

*The `OTEL_SERVICE_NAME` environment variable is used to identify your service
in the HyperDX app, it can be any name you want.*

The `OTEL_EXPORTER_OTLP_HEADERS` environment variable contains the API Key available via HyperDX app in `Team Settings → API Keys`.

- [Getting started](#getting-started)
  - [Install OpenTelemetry packages](#install-otel-packages)- [Configure OpenTelemetry + logger formatter](#configure-otel-logger-formatter)- [Configure environment variables](#configure-environment-variables)

Was this page helpful?