All Products
Search
Document Center

Simple Log Service:Import trace data from Ruby applications to Simple Log Service by using OpenTelemetry SDK for Ruby

Last Updated:Aug 25, 2023

This topic describes how to import trace data from Ruby applications to Simple Log Service by using OpenTelemetry SDK for Ruby.

Prerequisites

  • A trace instance is created. For more information, see Create a trace instance.

  • A Ruby development environment is set up. The Ruby version is 2.0 or later.

  • OpenTelemetry SDK for Ruby is installed.

    If OpenTelemetry SDK for Ruby is not installed, you can run the following commands to install the SDK:

    gem install opentelemetry-api
    gem install opentelemetry-sdk
    gem install opentelemetry-exporter-otlp

Procedure

  1. Configure environment variables.

    Replace the variables in the following code with the actual values. For more information about the variables, see Variables.

    export OTEL_RESOURCE_ATTRIBUTES=sls.otel.project=${project},sls.otel.instanceid=${instance},sls.otel.akid=${akid},sls.otel.aksecret=${aksecret},service.name=${service},service.version=${version},host.name=${host}

    export OTEL_RESOURCE_ATTRIBUTES=sls.otel.project=${project},sls.otel.instanceid=${instance},sls.otel.akid=${akid},sls.otel.aksecret=${aksecret},service.namespace=${service.namespace},service.name=${service},service.version=${version},host.name=${host}
    Table 1. Variables

    Variable

    Description

    Example

    ${service}

    The name of the service. Specify the value based on your business requirements.

    payment

    ${version}

    The version of the service. We recommend that you specify the version in the va.b.c format.

    v0.1.2

    ${service.namespace}

    The namespace to which the service belongs.

    order

    ${project}

    The name of the Simple Log Service project.

    test-project

    ${instance}

    The ID of the trace instance. For more information, see Create a trace instance.

    test-traces

    ${akid}

    The AccessKey ID of your Alibaba Cloud account.

    We recommend that you use the AccessKey pair of a RAM user that has only the write permissions on the Simple Log Service project. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to grant the write permissions on a specified project to a RAM user, see Use custom policies to grant permissions to a RAM user. For more information about how to obtain an AccessKey pair, see AccessKey pair.

    None

    ${aksecret}

    The AccessKey secret of your Alibaba Cloud account.

    We recommend that you use the AccessKey pair of a RAM user that has only the write permissions on the Simple Log Service project.

    None

    ${host}

    The hostname.

    localhost

  2. Configure instrumentation.

    Replace the ${endpoint} variable in the following code with the actual value. For more information about the variables, see Variables. For more information about sample code, see opentelemetry-ruby.

    require 'opentelemetry/sdk'
    require 'opentelemetry-exporter-otlp'
    
    # Configure the sdk with default export and context propagation formats
    # see SDK#configure for customizing the setup
    OpenTelemetry::SDK.configure do |c|
      c.add_span_processor(
        OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
          OpenTelemetry::Exporter::OTLP::Exporter.new(
            endpoint: 'https://${endpoint}/opentelemetry/v1/traces'
          )
        )
      )
    end
    
    # To start a trace you need to get a Tracer from the TracerProvider
    tracer = OpenTelemetry.tracer_provider.tracer('my_app_or_gem', '0.1.0')
    
    tracer.in_span('foo') do |span|
      # set an attribute
      span.set_attribute('tform', 'osx')
      # add an event
      span.add_event('event in bar')
      # create bar as child of foo
      tracer.in_span('bar') do |child_span|
        # inspect the span
        pp child_span
      end
    end
    
    sleep 10
    Table 2. Variables

    Variable

    Description

    Example

    ${endpoint}

    The endpoint of the Simple Log Service project. Format: ${project}.${region-endpoint}.

    • ${project}: the name of the Simple Log Service project.

    • ${region-endpoint}: the Simple Log Service endpoint for the region where the project resides. You can access Simple Log Service by using an internal or public endpoint. An internal endpoint can be accessed over the classic network or a virtual private cloud (VPC). A public endpoint can be accessed over the Internet. For more information, see Endpoints.

    https://test-project.cn-hangzhou.log.aliyuncs.com

What to do next