Ruby トレース SDK
Instana でのトレースは自動的に行われますが、カスタムコードや特定のアプリケーション領域、社内コンポーネントをさらに可視化したい場合は、Instana Ruby Tracing SDK を使用できます。
Instana Ruby Tracing SDK
Instana Ruby gemは、アプリケーションの任意の部分をトレースするための、使いやすいAPIを提供します。
コードのセクションは次のように記述する:
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
あるいは、 in_span ブロックメソッドを使うこともできる。このメソッドは、発生した例外を自動的に捕捉し、ログに記録する:
::Instana.tracer.in_span(:mywork, attributes: { :helpful_kvs => @user.id }) do
# The code to be instrumented
@id = User.find_by_name('john.smith')
end
前述の例は、好きなコードの任意の部分をインスツルメンテーションすることがいかに簡単かを示している。
非同期トレース
トレースしたい操作の中には非同期的なものもある。 このような操作をトレースするには、 log_async_* 関連のトレースメソッドを使用できます:
::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
新規スレッドへのコンテキストの伝達
トレースはスレッド・ローカルです。 新しいスレッドを作成した場合、コンテキストはその新しいスレッドに運ばれ、ピックアップされなければならない。
# 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
後で実行されるようにスケジュールされたジョブのトレース
後で実行するためにキューに入れられたジョブは、以下のようにインスツルメントできます。
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
Instana 提供の Spans へのカスタム・タグの追加
カスタム・タグを Instana 提供のスパンに追加するには、set_tag メソッドを使用して、以下のハッシュをアクティブ・スパンに付加します。
active_span = ::Instana.tracer.current_span
unless active_span.custom?
custom_tags = {
key: 'value'
}
active.span.set_tag(:sdk, {custom: {tags: custom_tags}})
end