Merge pull request #49 from lightstep/bhs/log_kv
Support for modernized KV logging
This commit is contained in:
Коммит
898267ffc4
|
@ -92,18 +92,16 @@ module LightStep
|
|||
def log(event: nil, timestamp: Time.now, **fields)
|
||||
return unless tracer.enabled?
|
||||
|
||||
record = {
|
||||
runtime_guid: tracer.guid,
|
||||
timestamp_micros: LightStep.micros(timestamp)
|
||||
}
|
||||
record[:stable_name] = event.to_s if !event.nil?
|
||||
|
||||
begin
|
||||
record[:payload_json] = JSON.generate(fields, max_nesting: 8)
|
||||
rescue
|
||||
# TODO: failure to encode a payload as JSON should be recorded in the
|
||||
# internal library logs, being careful not to flood them.
|
||||
fields = {} if fields.nil?
|
||||
unless event.nil?
|
||||
fields[:event] = event.to_s
|
||||
end
|
||||
record = {
|
||||
timestamp_micros: LightStep.micros(timestamp),
|
||||
fields: fields.to_a.map {|key, value|
|
||||
{Key: key.to_s, Value: value.to_s}
|
||||
},
|
||||
}
|
||||
|
||||
log_records.push(record)
|
||||
if log_records.size > @max_log_records
|
||||
|
|
|
@ -167,7 +167,7 @@ describe LightStep do
|
|||
end
|
||||
end
|
||||
|
||||
it 'should handle all valid payloads types' do
|
||||
it 'should handle all valid field types' do
|
||||
tracer = init_test_tracer
|
||||
span = tracer.start_span('test_span')
|
||||
file = File.open('./lib/lightstep.rb', 'r')
|
||||
|
@ -253,20 +253,26 @@ describe LightStep do
|
|||
transport: LightStep::Transport::Callback.new(callback: proc { |obj|; result = obj; })
|
||||
)
|
||||
|
||||
single_payload = proc do |fields|
|
||||
reported_fields = proc do |fields|
|
||||
s0 = tracer.start_span('s0')
|
||||
s0.log(event: 'test_event', **fields)
|
||||
s0.log(**fields)
|
||||
s0.finish
|
||||
tracer.flush
|
||||
JSON.generate(JSON.parse(result[:span_records][0][:log_records][0][:payload_json]))
|
||||
# print JSON.parse(result[:span_records][0][:log_records][0][:fields])
|
||||
print result[:span_records][0][:log_records][0][:fields]
|
||||
JSON.generate(result[:span_records][0][:log_records][0][:fields])
|
||||
end
|
||||
|
||||
# NOTE: these comparisons rely on Ruby generating a consistent ordering to
|
||||
# map keys
|
||||
|
||||
expect(single_payload.call({})).to eq(JSON.generate({}))
|
||||
expect(single_payload.call(x: 'y')).to eq(JSON.generate(x: 'y'))
|
||||
expect(single_payload.call(x: 'y', a: 5, true: true)).to eq(JSON.generate(x: 'y', a: 5, true: true))
|
||||
expect(reported_fields.call({})).to eq(JSON.generate([]))
|
||||
expect(reported_fields.call(x: 'y')).to eq(JSON.generate([{Key: 'x', Value: 'y'}]))
|
||||
expect(reported_fields.call(x: 'y', a: 5, true: true)).to eq(JSON.generate([
|
||||
{Key: 'x', Value: 'y'},
|
||||
{Key: 'a', Value: '5'},
|
||||
{Key: 'true', Value: 'true'}
|
||||
]))
|
||||
end
|
||||
|
||||
it 'should report user-specified tracer-level tags' do
|
||||
|
|
Загрузка…
Ссылка в новой задаче