Class: Datadog::CI::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/recorder.rb

Overview

Common behavior for CI tests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



22
23
24
25
# File 'lib/datadog/ci/recorder.rb', line 22

def initialize
  @environment_tags = Ext::Environment.tags(ENV).freeze
  @local_context = Context::Local.new
end

Instance Attribute Details

#environment_tagsObject (readonly)

Returns the value of attribute environment_tags.



20
21
22
# File 'lib/datadog/ci/recorder.rb', line 20

def environment_tags
  @environment_tags
end

Instance Method Details

#active_spanObject



84
85
86
87
# File 'lib/datadog/ci/recorder.rb', line 84

def active_span
  tracer_span = Datadog::Tracing.active_span
  Span.new(tracer_span) if tracer_span
end

#active_testObject



76
77
78
# File 'lib/datadog/ci/recorder.rb', line 76

def active_test
  @local_context.active_test
end

#deactivate_test(test) ⇒ Object



80
81
82
# File 'lib/datadog/ci/recorder.rb', line 80

def deactivate_test(test)
  @local_context.deactivate_test!(test)
end

#trace(span_type, span_name, tags: {}, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/datadog/ci/recorder.rb', line 59

def trace(span_type, span_name, tags: {}, &block)
  span_options = {
    resource: span_name,
    span_type: span_type
  }

  if block
    Datadog::Tracing.trace(span_name, **span_options) do |tracer_span, trace|
      block.call(build_span(tracer_span, tags))
    end
  else
    tracer_span = Datadog::Tracing.trace(span_name, **span_options)

    build_span(tracer_span, tags)
  end
end

#trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &block) ⇒ Object

Creates a new span for a CI test



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
# File 'lib/datadog/ci/recorder.rb', line 28

def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &block)
  span_options = {
    resource: test_name,
    service: service_name,
    span_type: Ext::AppTypes::TYPE_TEST
  }

  tags[Ext::Test::TAG_NAME] = test_name

  if block
    Datadog::Tracing.trace(operation_name, **span_options) do |tracer_span, trace|
      set_trace_origin(trace)

      test = build_test(tracer_span, tags)

      @local_context.activate_test!(test) do
        block.call(test)
      end
    end
  else
    tracer_span = Datadog::Tracing.trace(operation_name, **span_options)
    trace = Datadog::Tracing.active_trace

    set_trace_origin(trace)

    test = build_test(tracer_span, tags)
    @local_context.activate_test!(test)
    test
  end
end