From d45763ca2685dabcd3dc6d34fe18783676b1f0a5 Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Thu, 27 Oct 2016 11:51:36 -0400 Subject: [PATCH] namespace span --- lib/lightstep/tracer/client_span.rb | 254 ++++++++++++++-------------- lib/lightstep/tracer/tracer.rb | 5 +- spec/lightstep_spec.rb | 2 +- 3 files changed, 131 insertions(+), 130 deletions(-) diff --git a/lib/lightstep/tracer/client_span.rb b/lib/lightstep/tracer/client_span.rb index 4d10071..a946eff 100644 --- a/lib/lightstep/tracer/client_span.rb +++ b/lib/lightstep/tracer/client_span.rb @@ -1,135 +1,137 @@ # TODO(ngauthier@gmail.com) Separate Span and SpanContext under a getter according # to the spec. Baggage moves to span context. -# FIXME(ngauthier@gmail.com) namespace -class ClientSpan - # ---------------------------------------------------------------------------- - # OpenTracing API - # ---------------------------------------------------------------------------- - attr_reader :tracer +module LightStep + class Span + # ---------------------------------------------------------------------------- + # OpenTracing API + # ---------------------------------------------------------------------------- - # TODO(ngauthier@gmail.com) validate value is string, bool, or number and - # remove value.to_s from all calling code - def set_tag(key, value) - @tags[key] = value - self - end + attr_reader :tracer - # FIXME(ngauthier@gmail.com) accessor - def set_baggage_item(key, value) - @baggage[key] = value - self - end - - # FIXME(ngauthier@gmail.com) accessor - # TODO(ngauthier@gmail.com) remove? Not in spec to get baggage. - def get_baggage_item(key) - @baggage[key] - end - - # TODO(ngauthier@gmail.com) remove, since it's deprecated - def log_event(event, payload = nil) - log(event: event.to_s, payload: payload) - end - - def log(fields) - record = { span_guid: @guid.to_s } - - record[:stable_name] = fields[:event].to_s unless fields[:event].nil? - unless fields[:timestamp].nil? - record[:timestamp_micros] = (fields[:timestamp] * 1000).to_i - end - @tracer.raw_log_record(record, fields[:payload]) - end - - # FIXME(ngauthier@gmail.com) keyword arg? - def finish(fields = nil) - unless fields.nil? - set_end_micros(fields[:endTime] * 1000) unless fields[:endTime].nil? - end - @tracer._finish_span(self) - self - end - - # ---------------------------------------------------------------------------- - # Implemenation specific - # ---------------------------------------------------------------------------- - - def initialize(tracer) - @guid = '' - @operation = '' - @trace_guid = nil - @tags = {} - @baggage = {} - @start_micros = 0 - @end_micros = 0 - @error_flag = false - - @tracer = tracer - @guid = tracer.generate_guid - end - - attr_reader :guid, :operation, :tags, :baggage, :start_micros, :end_micros, :error_flag - attr_accessor :trace_guid - - def finalize - if @end_micros == 0 - # TODO: Notify about that finish() was never called for this span - finish - end - end - - # FIXME(ngauthier@gmail.com) writer - def set_start_micros(start) - @start_micros = start - self - end - - # FIXME(ngauthier@gmail.com) writer - def set_end_micros(micros) - @end_micros = micros - self - end - - # FIXME(ngauthier@gmail.com) writer - def set_operation_name(name) - @operation = name - self - end - - def parent_guid - @tags[:parent_span_guid] - end - - # FIXME(ngauthier@gmail.com) constant prefix - # FIXME(ngauthier@gmail.com) safe url generation? - # FIXME(ngauthier@gmail.com) getter - def generate_trace_url - "https://app.lightstep.com/#{@tracer.access_token}/trace?span_guid=#{@guid}&at_micros=#{start_micros}" - end - - # FIXME(ngauthier@gmail.com) writer - def set_parent(span) - set_tag(:parent_span_guid, span.guid) - @trace_guid = span.trace_guid - self - end - - # FIXME(ngauthier@gmail.com) to_h - def to_thrift - attributes = @tags.map do |key, value| - {"Key" => key.to_s, "Value" => value.to_s} + # TODO(ngauthier@gmail.com) validate value is string, bool, or number and + # remove value.to_s from all calling code + def set_tag(key, value) + @tags[key] = value + self end - rec = { - "runtime_guid" => @tracer.guid.to_s, - "span_guid" => @guid.to_s, - "trace_guid" => @trace_guid.to_s, - "span_name" => @operation.to_s, - "attributes" => attributes, - "oldest_micros" => @start_micros.to_i, - "youngest_micros" => @end_micros.to_i, - "error_flag" => @error_flag - } + # FIXME(ngauthier@gmail.com) accessor + def set_baggage_item(key, value) + @baggage[key] = value + self + end + + # FIXME(ngauthier@gmail.com) accessor + # TODO(ngauthier@gmail.com) remove? Not in spec to get baggage. + def get_baggage_item(key) + @baggage[key] + end + + # TODO(ngauthier@gmail.com) remove, since it's deprecated + def log_event(event, payload = nil) + log(event: event.to_s, payload: payload) + end + + def log(fields) + record = { span_guid: @guid.to_s } + + record[:stable_name] = fields[:event].to_s unless fields[:event].nil? + unless fields[:timestamp].nil? + record[:timestamp_micros] = (fields[:timestamp] * 1000).to_i + end + @tracer.raw_log_record(record, fields[:payload]) + end + + # FIXME(ngauthier@gmail.com) keyword arg? + def finish(fields = nil) + unless fields.nil? + set_end_micros(fields[:endTime] * 1000) unless fields[:endTime].nil? + end + @tracer._finish_span(self) + self + end + + # ---------------------------------------------------------------------------- + # Implemenation specific + # ---------------------------------------------------------------------------- + + def initialize(tracer) + @guid = '' + @operation = '' + @trace_guid = nil + @tags = {} + @baggage = {} + @start_micros = 0 + @end_micros = 0 + @error_flag = false + + @tracer = tracer + @guid = tracer.generate_guid + end + + attr_reader :guid, :operation, :tags, :baggage, :start_micros, :end_micros, :error_flag + attr_accessor :trace_guid + + def finalize + if @end_micros == 0 + # TODO: Notify about that finish() was never called for this span + finish + end + end + + # FIXME(ngauthier@gmail.com) writer + def set_start_micros(start) + @start_micros = start + self + end + + # FIXME(ngauthier@gmail.com) writer + def set_end_micros(micros) + @end_micros = micros + self + end + + # FIXME(ngauthier@gmail.com) writer + def set_operation_name(name) + @operation = name + self + end + + def parent_guid + @tags[:parent_span_guid] + end + + # FIXME(ngauthier@gmail.com) constant prefix + # FIXME(ngauthier@gmail.com) safe url generation? + # FIXME(ngauthier@gmail.com) getter + def generate_trace_url + "https://app.lightstep.com/#{@tracer.access_token}/trace?span_guid=#{@guid}&at_micros=#{start_micros}" + end + + # FIXME(ngauthier@gmail.com) writer + def set_parent(span) + set_tag(:parent_span_guid, span.guid) + @trace_guid = span.trace_guid + self + end + + # FIXME(ngauthier@gmail.com) to_h + def to_thrift + attributes = @tags.map do |key, value| + {"Key" => key.to_s, "Value" => value.to_s} + end + + rec = { + "runtime_guid" => @tracer.guid.to_s, + "span_guid" => @guid.to_s, + "trace_guid" => @trace_guid.to_s, + "span_name" => @operation.to_s, + "attributes" => attributes, + "oldest_micros" => @start_micros.to_i, + "youngest_micros" => @end_micros.to_i, + "error_flag" => @error_flag + } + end end end diff --git a/lib/lightstep/tracer/tracer.rb b/lib/lightstep/tracer/tracer.rb index e295819..5548a91 100644 --- a/lib/lightstep/tracer/tracer.rb +++ b/lib/lightstep/tracer/tracer.rb @@ -5,7 +5,6 @@ require 'lightstep/tracer/transports/transport_http_json' require 'lightstep/tracer/transports/transport_nil' require 'lightstep/tracer/transports/transport_callback' -# FIXME(ngauthier@gmail.com) namespace module LightStep class Tracer FORMAT_TEXT_MAP = 1 @@ -40,7 +39,7 @@ module LightStep # TODO(ngauthier@gmail.com) fields should be tags # TODO(ngauthier@gmail.com) ability to provide a timestamp to be used other than now def start_span(operation_name, fields = nil) - span = ClientSpan.new(self) + span = Span.new(self) span.set_operation_name(operation_name) span.set_start_micros(now_micros) @@ -108,7 +107,7 @@ module LightStep # FIXME(ngauthier@gmail.com) private def _join_from_text_map(operation_name, carrier) - span = ClientSpan.new(self) + span = Span.new(self) span.set_operation_name(operation_name) span.set_start_micros(now_micros) diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index 1bbc3ad..b60e6e9 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -23,7 +23,7 @@ describe LightStep do it 'should return a valid span from start_span' do tracer = init_test_tracer span = tracer.start_span('my_span') - expect(span).to be_an_instance_of ClientSpan + expect(span).to be_an_instance_of LightStep::Span span.finish end