-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.rb
79 lines (68 loc) · 2.01 KB
/
trace.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'rubygems'
require 'bundler/setup'
# Require otel-ruby
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp/exporter'
require 'opentelemetry/propagator/xray'
require 'pyroscope'
Pyroscope.configure do |config|
config.application_name = "pyroscope"
config.server_address = "localhoge"
end
OpenTelemetry::SDK.configure do |c|
c.add_span_processor(
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
OpenTelemetry::Exporter::OTLP::Exporter.new(
endpoint: "localhost"
)
)
)
require 'pyroscope/otel'
c.add_span_processor Pyroscope::Otel::SpanProcessor.new(
"hogehoge.cpu",
"localhost"
)
c.id_generator = OpenTelemetry::Propagator::XRay::IDGenerator
c.propagators = [OpenTelemetry::Propagator::XRay::TextMapPropagator.new]
end
class Hoge
@@tracer = OpenTelemetry.tracer_provider.tracer('hogehoge')
@@carrier = {}
@@root_span = nil
def self.with_create_root_span(&block)
begin
span = @@tracer.start_root_span("root")
span.set_attribute("request_id", "111111111111111")
@@root_span = span
OpenTelemetry::Trace.with_span(span) do
OpenTelemetry.propagation.inject(@@carrier)
block.call if block_given?
end
rescue => e
span&.record_exception(e)
span&.status = Status.error("DsTrace Unhandled exception of type: #{e.class}")
end
end
def self.with_create_span(finish_root_span: false, &block)
begin
parent = OpenTelemetry.propagation.extract(@@carrier)
span = @@tracer.start_span("child", with_parent: parent)
OpenTelemetry::Trace.with_span(span, &block)
rescue => e
span&.record_exception(e)
span&.status = Status.error("DsTrace Unhandled exception of type: #{e.class}")
ensure
span&.finish
@@root_span&.finish if finish_root_span
end
end
end
Hoge.with_create_root_span do
p "do something root"
end
Hoge.with_create_span(finish_root_span: true) do
p "do something"
# Hoge.with_create_span do
# p "do something2"
# end
end