SDK Ruby Tracing

Le traçage avec Instana est automatique, mais si vous souhaitez bénéficier d'une visibilité encore plus grande sur votre code personnalisé, une zone spécifique de l'application ou certains composants internes, vous pouvez utiliser le SDK de traçage Instana Ruby.

Instana Ruby SDK de traçage

Le gem « Instana » ( Ruby ) offre une interface simple d'utilisation ( API ) permettant de tracer n'importe quelle partie de votre application.

Vous pouvez instrumenter une section de code comme suit :

begin
  span = ::Instana.tracer.start_span(:mywork, attributes: { :helpful_kvs => @user.id })
  # The code to be instrumented
  @id = User.find_by_name('john.smith')
rescue => e
  span.record_exception(e)
ensure
  span.finish
end

Vous pouvez également utiliser la in_span méthode `block`, qui capture et consigne automatiquement toutes les exceptions levées :

::Instana.tracer.in_span(:mywork, attributes: { :helpful_kvs => @user.id }) do
  # The code to be instrumented
  @id = User.find_by_name('john.smith')
 end

Les exemples précédents montrent à quel point il est facile d'instrumenter n'importe quel bout de code de votre choix.

Traçage asynchrone

Certaines opérations que vous souhaitez suivre peuvent être asynchrones, ce qui signifie qu'elles peuvent renvoyer un résultat immédiatement tout en continuant à s'exécuter en arrière-plan. Pour suivre ces opérations, vous pouvez utiliser les méthodes log_async_* de suivi correspondantes :

::Instana.tracer.log_entry(:prep_job, { :helpful_kvs => @job.name })

http_ops = {:get => "/", :post => "/post_data"}

cb_block = Proc.new do |response, payload|
  # The callback block that is invoked on HTTP response (payload == t_context)
  #
  # process response
  #
  ::Instana.tracer.log_async_exit(:http_op, :status => response.status, payload)
end

http_ops.each do |op|
  t_context = ::Instana.tracer.log_async_entry(:http_op)

  # Example op that returns immediately
  request_id = connection.async_request(op, cb_block, t_context)

  ::Instana.tracer.log_async_info({:request_id => request_id}, t_context)
end  

Transmission du contexte aux nouvelles unités d'exécution

Le traçage s'effectue entre les unités d'exécution. Si vous créez un nouveau thread, le contexte doit être transféré vers ce nouveau thread, puis récupéré.

# Get the tracing context
t_context = ::Instana.tracer.context

# Spawn new thread
Thread.new do
  # Pickup context in this thread with `t_context` and attach it to a nonrecording span. Use that span as parent for future spans.
  Instana::Trace.with_span(OpenTelemetry::Trace.non_recording_span(t_context)) do
    span = ::Instana.tracer.start_span(:async_thread, attributes: { :async_start => 1 })

    # Continue tracing work as usual
    begin
      span = ::Instana.tracer.start_span(:mywork, attributes: { :helpful_kvs => @user.id })
      # The code to be instrumented
      @id = User.find_by_name('john.smith')
    rescue => e
      span.record_exception(e)
    ensure
      span.finish
    end
  end
end

Traçage des travaux planifiés pour une exécution ultérieure

Les travaux mis en file d'attente à exécuter ultérieurement peuvent être instrumentés en tant que tels :

parent_span = ::Instana.tracer.start_span(:prep_job, attributes: { :job_name => @job.name })

# Get the current tracing context
t_context = ::Instana.tracer.context

# The Async proc (job) that will be executed out of band.
block = Proc.new do
  # This will pickup context and link the two traces (root + job)
  parent_context = OpenTelemetry::Trace.context_with_span(parent_span)
  second_span = ::Instana.tracer.start_span(:my_async_op, attributes: { :helpful_kvs => true }, with_parent: parent_context)

  #
  # Some Asynchronous work to be done
  #
  second_span.set_tags({:job_name => Job.get(id).name})
  # More Asynchronous work
  second_span.finish
end

MyClass.run_in_5_minutes(block)

parent_span.finish

Ajout de balises personnalisées aux spans fournis par Instana

Pour ajouter des balises personnalisées à un élément `span` fourni par l' Instana, utilisez la set_tag méthode et associez le hachage suivant à l'élément `span` actif.

active_span = ::Instana.tracer.current_span

unless active_span.custom?
  custom_tags = {
    key: 'value'
  }

  active.span.set_tag(:sdk, {custom: {tags: custom_tags}})
end