> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> ClickStack용 Ruby on Rails SDK - ClickHouse Observability Stack

# Ruby on Rails

이 가이드는 다음을 통합합니다:

<table>
  <tbody>
    <tr>
      <td className="pe-2">✖️ 로그</td>
      <td className="pe-2">✖️ ️️메트릭</td>
      <td className="pe-2">✅ 트레이스</td>
    </tr>
  </tbody>
</table>

*로그를 ClickStack으로 보내려면 [OpenTelemetry collector](/ko/clickstack/ingesting-data/collector)를 통해 전송하십시오.*

<div id="getting-started">
  ## 시작하기
</div>

<div id="install-otel-packages">
  ### OpenTelemetry 패키지 설치
</div>

다음 명령으로 OpenTelemetry 패키지를 설치하십시오.

```shell theme={null}
bundle add opentelemetry-sdk opentelemetry-instrumentation-all opentelemetry-exporter-otlp
```

<div id="configure-otel-logger-formatter">
  ### OpenTelemetry + 로거 포매터 구성
</div>

다음으로, OpenTelemetry tracing instrumentation을 초기화하고
Rails 로거의 로그 메시지 포매터를 구성해야 로그가 자동으로
트레이스와 연결되도록 할 수 있습니다. 사용자 지정 포매터가 없으면 로그와 트레이스는
ClickStack에서 자동으로 연관되지 않습니다.

`config/initializers` 폴더에서 `hyperdx.rb`라는 파일을 생성하고
다음을 추가하십시오:

```ruby theme={null}
# config/initializers/hyperdx.rb

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

OpenTelemetry::SDK.configure do |c|
  c.use_all() # 모든 트레이스 계측을 활성화합니다!
end

Rails.application.configure do
  Rails.logger = Logger.new(STDOUT)
  # Rails.logger.log_level = Logger::INFO # 기본값은 DEBUG이며, 프로덕션 환경에서는 INFO 이상으로 설정하는 것을 권장합니다
  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
```

<div id="configure-environment-variables">
  ### 환경 변수 설정
</div>

이제 OpenTelemetry collector를 통해 telemetry를 ClickStack으로 전송할 수 있도록 셸에서 다음 환경 변수를 설정해야 합니다:

<Tabs>
  <Tab title="Managed ClickStack">
    ```shell theme={null}
    export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 \
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
    OTEL_SERVICE_NAME='<NAME_OF_YOUR_APP_OR_SERVICE>' \
    ```
  </Tab>

  <Tab title="ClickStack Open Source">
    ```shell theme={null}
    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>'
    ```
  </Tab>
</Tabs>

*`OTEL_SERVICE_NAME` 환경 변수는 HyperDX 앱에서 서비스를 식별하는 데 사용되며,
원하는 이름으로 자유롭게 지정할 수 있습니다.*

`OTEL_EXPORTER_OTLP_HEADERS` 환경 변수에는 HyperDX 앱의 `Team Settings → API Keys`에서 확인할 수 있는 API Key가 포함됩니다.
