-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathtracer.rb
69 lines (64 loc) · 2.59 KB
/
tracer.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
# frozen_string_literal: true
# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
module OpenTelemetry
module Trace
# No-op implementation of Tracer.
class Tracer
# This is a helper for the default use-case of extending the current trace with a span.
#
# With this helper:
#
# OpenTelemetry.tracer.in_span('do-the-thing') do ... end
#
# Equivalent without helper:
#
# OpenTelemetry::Trace.with_span(tracer.start_span('do-the-thing')) do ... end
#
# On exit, the Span that was active before calling this method will be reactivated. If an
# exception occurs during the execution of the provided block, it will be recorded on the
# span and reraised.
#
# @param name [String] the name of the span
# @param attributes [optional Hash] attributes to attach to the span {String => String,
# Numeric, Boolean, Array<String, Numeric, Boolean>}
# @param links [optional Array] an array of OpenTelemetry::Trace::Link instances
# @param start_timestamp [optional Integer] nanoseconds since Epoch
# @param kind [optional Symbol] One of :internal, :server, :client, :producer, :consumer
#
# @yield [span, context] yields the newly created span and a context containing the
# span to the block.
def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
span = nil
span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind)
Trace.with_span(span) { |s, c| yield s, c }
rescue Exception => e # rubocop:disable Lint/RescueException
span&.record_exception(e)
span&.status = Status.error("Unhandled exception of type: #{e.class}")
raise e
ensure
span&.finish
end
def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
Span::INVALID
end
# Used when a caller wants to manage the activation/deactivation and lifecycle of
# the Span and its parent manually.
#
# Parent context can be either passed explicitly, or inferred from currently activated span.
#
# @param [optional Context] with_parent Explicitly managed parent context
#
# @return [Span]
def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
span = OpenTelemetry::Trace.current_span(with_parent)
if span.context.valid?
span
else
Span::INVALID
end
end
end
end
end