Ruby トレース SDK
Instana によるトレースは自動的に行われますが、カスタムコード、特定のアプリケーション領域、または社内コンポーネントについてさらに詳細な可視性を確保したい場合は、 Instana Ruby のトレース 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