Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Jul 11, 2024
1 parent 5beeb47 commit 6d71a35
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 279 deletions.
5 changes: 3 additions & 2 deletions lib/datadog/tracing/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ def to_hash
span_id: @id,
trace_id: @trace_id,
type: @type,
span_links: @links.map(&:to_hash),
events: @events.map(&:to_hash)
span_links: @links.map(&:to_hash)
}

if stopped?
h[:start] = start_time_nano
h[:duration] = duration_nano
end

h[:meta]['events'] = @events.map(&:to_hash).to_json unless @events.empty?

h
end

Expand Down
6 changes: 6 additions & 0 deletions lib/datadog/tracing/span_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def initialize(
@attributes = attributes&.map { |key, val| [key, val.to_s] }&.to_h || {}
@time_unix_nano = time_unix_nano || Core::Utils::Time.now.to_f * 1e9
end

def to_hash
h = { :name => @name, :time_unix_nano => @time_unix_nano }
h[:attributes] = @attributes unless @attributes.empty?
h
end
end
end
end
4 changes: 2 additions & 2 deletions lib/datadog/tracing/transport/serializable_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def to_msgpack(packer = nil)
packer.write_map_header(number_of_elements_to_write) # Set header with how many elements in the map
end

# convert span events to tags
span.set_tag('events', span.events.map(&:to_hash).to_json) if span.events.count
# serialize span events as meta tags
span.set_tag('events', span.events.map(&:to_hash).to_json) if span.events.count > 0

# DEV: We use strings as keys here, instead of symbols, as
# DEV: MessagePack will ultimately convert them to strings.
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/tracing/span_operation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Datadog

attr_accessor status: untyped

def initialize: (untyped name, ?child_of: untyped?, ?events: untyped?, ?on_error: untyped?, ?parent_id: ::Integer, ?resource: untyped, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?trace_id: untyped?, ?type: untyped?, ?links: untyped?) -> void
def initialize: (untyped name, ?child_of: untyped?, ?events: untyped?, ?on_error: untyped?, ?parent_id: ::Integer, ?resource: untyped, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?trace_id: untyped?, ?type: untyped?, ?links: untyped?, ?span_events: untyped?) -> void

def name=: (untyped name) -> untyped

Expand Down
59 changes: 59 additions & 0 deletions spec/datadog/tracing/span_event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'
require 'support/object_helpers'

require 'datadog/tracing/span_event'

RSpec.describe Datadog::Tracing::SpanEvent do
subject(:span_event) { described_class.new(name, attributes: attributes, time_unix_nano: time_unix_nano) }

let(:name) { nil }
let(:attributes) { nil }
let(:time_unix_nano) { nil }

describe '::new' do
context 'by default' do
let(:name) { 'This happened!' }

it do
expect(span_event.name).to eq(name)
expect(span_event.attributes).to eq({})
expect(span_event.time_unix_nano / 1e9).to be_within(1).of(Time.now.to_f)
end
end

context 'given' do
context ':attributes' do
let(:attributes) { { tag: 'value' } }
it { is_expected.to have_attributes(attributes: attributes) }
end

context ':time_unix_nano' do
let(:time_unix_nano) { 30000 }
it { is_expected.to have_attributes(time_unix_nano: time_unix_nano) }
end
end
end

describe '#to_hash' do
subject(:to_hash) { span_event.to_hash }
let(:name) { 'Another Event!' }

context 'with required fields' do
it { is_expected.to eq({ name: name, time_unix_nano: span_event.time_unix_nano }) }
end

context 'with timestamp' do
let(:time_unix_nano) { 25 }
it { is_expected.to include(time_unix_nano: 25) }
end

context 'when attributes is set' do
let(:attributes) { { 'event.name' => 'test_event', 'event.id' => 1, 'nested' => [true, [2, 3], 'val'] } }
it {
is_expected.to include(
attributes: { 'event.name' => 'test_event', 'event.id' => '1', 'nested' => '[true, [2, 3], "val"]' }
)
}
end
end
end
274 changes: 0 additions & 274 deletions spec/datadog/tracing/span_spec.rb

This file was deleted.

Loading

0 comments on commit 6d71a35

Please sign in to comment.