Class: Watchforge::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/watchforge/tracing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, transaction, transaction_name, op) ⇒ Transaction

Returns a new instance of Transaction.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/watchforge/tracing.rb', line 50

def initialize(client, transaction, transaction_name, op)
  @client = client
  @trace_id = SecureRandom.uuid
  @transaction = transaction
  @transaction_name = transaction_name || transaction
  @op = op || "http.server"
  @spans = []
  @tags = {}
  @start_timestamp = (Time.now.to_f * 1000).to_i
  @status = "ok"
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



48
49
50
# File 'lib/watchforge/tracing.rb', line 48

def op
  @op
end

#spansObject (readonly)

Returns the value of attribute spans.



48
49
50
# File 'lib/watchforge/tracing.rb', line 48

def spans
  @spans
end

#tagsObject (readonly)

Returns the value of attribute tags.



48
49
50
# File 'lib/watchforge/tracing.rb', line 48

def tags
  @tags
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



48
49
50
# File 'lib/watchforge/tracing.rb', line 48

def trace_id
  @trace_id
end

#transactionObject (readonly)

Returns the value of attribute transaction.



48
49
50
# File 'lib/watchforge/tracing.rb', line 48

def transaction
  @transaction
end

#transaction_nameObject (readonly)

Returns the value of attribute transaction_name.



48
49
50
# File 'lib/watchforge/tracing.rb', line 48

def transaction_name
  @transaction_name
end

Instance Method Details

#finish(status = "ok") ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/watchforge/tracing.rb', line 69

def finish(status = "ok")
  @status = status
  finish_timestamp = (Time.now.to_f * 1000).to_i
  spans.each { |span| span.finish if span.finish_timestamp.nil? }
  payload = {
    trace_id: trace_id,
    transaction: transaction,
    transaction_name: transaction_name,
    op: op,
    start_timestamp: Time.at(@start_timestamp / 1000.0).utc.iso8601,
    finish_timestamp: Time.at(finish_timestamp / 1000.0).utc.iso8601,
    duration_ms: finish_timestamp - @start_timestamp,
    status: @status,
    environment: @client.instance_variable_get(:@environment),
    platform: "ruby",
    sdk_name: SDK_NAME,
    sdk_version: VERSION,
    tags: tags,
    spans: spans.map(&:to_h)
  }
  @client.instance_variable_get(:@transport).send_trace(payload)
end

#start_span(op, description = "", data: nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/watchforge/tracing.rb', line 62

def start_span(op, description = "", data: nil)
  parent = spans.last&.span_id
  span = Span.new(op, description, parent_span_id: parent, data: data)
  spans << span
  span
end