Module: D13n::Metric::Instrumentation::MiddlewareTracing

Included in:
Rack::MetricMiddleware
Defined in:
lib/d13n/metric/instrumentation/middleware_tracing.rb

Constant Summary collapse

STREAM_STARTED_KEY =
'd13n.stream_started'.freeze
CONTENT_TYPE =
'Content-Type'.freeze
CONTENT_LENGTH =
'Content-Length'.freeze

Instance Method Summary collapse

Instance Method Details

#build_stream_options(env, first_middleware) ⇒ Object

stream_name, apdex_started_at, request



62
63
64
65
66
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 62

def build_stream_options(env, first_middleware)
  opts = @stream_options
  opts = merge_first_middleware_options(opts, env) if first_middleware
  opts
end

#call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 5

def call(env)
  first_middleware = stream_started(env)

  state = D13n::Metric::StreamState.st_get

  begin
    D13n::Metric::Stream.start(state, category, build_stream_options(env, first_middleware))
    state.notify_rack_call(env) if first_middleware
    result = (@target == self) ? traced_call(env) : @target.call(env)

    if first_middleware
      capture_response_attributes(state, result)
    end

    result
  rescue Exception => e
    D13n.logger.error(e)
    raise e
  ensure
    D13n::Metric::Stream.stop(state)
  end
end

#capture_response_attributes(state, result) ⇒ Object



53
54
55
56
57
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 53

def capture_response_attributes(state, result)
  capture_response_code(state, result)
  capture_response_content_type(state, result)
  capture_response_content_length(state, result)
end

#capture_response_code(state, result) ⇒ Object



28
29
30
31
32
33
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 28

def capture_response_code(state, result)
  
  if result.is_a?(Array) && state.current_stream
    state.current_stream.http_response_code = result[0]
  end
end

#capture_response_content_length(state, result) ⇒ Object



46
47
48
49
50
51
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 46

def capture_response_content_length(state, result)
  if result.is_a?(Array) && state.current_stream
    _, headers, _ = result
    state.current_stream.response_content_length = headers[CONTENT_LENGTH]
  end
end

#capture_response_content_type(state, result) ⇒ Object



37
38
39
40
41
42
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 37

def capture_response_content_type(state, result)
  if result.is_a?(Array) && state.current_stream
    _, headers, _ = result
    state.current_stream.response_content_type = headers[CONTENT_TYPE]
  end
end

#merge_first_middleware_options(opts, env) ⇒ Object



68
69
70
71
72
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 68

def merge_first_middleware_options(opts, env)
  opts[:apdex_started_at] = parse_request_timestamp(env)
  opts[:request] = ::Rack::Request.new(env) if defined?(::Rack)
  opts
end

#parse_request_timestamp(env) ⇒ Object



74
75
76
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 74

def parse_request_timestamp(env)
  Time.now.to_f
end

#stream_started(env) ⇒ Object



78
79
80
# File 'lib/d13n/metric/instrumentation/middleware_tracing.rb', line 78

def stream_started(env)
  env[STREAM_STARTED_KEY] = true unless env[STREAM_STARTED_KEY]
end