opencensus 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +23 -0
  3. data/README.md +14 -3
  4. data/lib/opencensus.rb +12 -0
  5. data/lib/opencensus/common.rb +1 -0
  6. data/lib/opencensus/common/config.rb +1 -0
  7. data/lib/opencensus/config.rb +5 -3
  8. data/lib/opencensus/context.rb +1 -0
  9. data/lib/opencensus/stats.rb +1 -0
  10. data/lib/opencensus/tags.rb +1 -0
  11. data/lib/opencensus/trace.rb +31 -11
  12. data/lib/opencensus/trace/annotation.rb +1 -0
  13. data/lib/opencensus/trace/config.rb +13 -11
  14. data/lib/opencensus/trace/exporters.rb +1 -0
  15. data/lib/opencensus/trace/exporters/logger.rb +4 -3
  16. data/lib/opencensus/trace/exporters/multi.rb +1 -0
  17. data/lib/opencensus/trace/formatters.rb +3 -2
  18. data/lib/opencensus/trace/formatters/binary.rb +1 -0
  19. data/lib/opencensus/trace/formatters/cloud_trace.rb +1 -0
  20. data/lib/opencensus/trace/formatters/trace_context.rb +3 -2
  21. data/lib/opencensus/trace/integrations.rb +1 -0
  22. data/lib/opencensus/trace/integrations/faraday_middleware.rb +57 -21
  23. data/lib/opencensus/trace/integrations/rack_middleware.rb +11 -19
  24. data/lib/opencensus/trace/integrations/rails.rb +1 -0
  25. data/lib/opencensus/trace/link.rb +17 -7
  26. data/lib/opencensus/trace/message_event.rb +13 -3
  27. data/lib/opencensus/trace/samplers.rb +11 -7
  28. data/lib/opencensus/trace/samplers/always_sample.rb +1 -0
  29. data/lib/opencensus/trace/samplers/never_sample.rb +1 -0
  30. data/lib/opencensus/trace/samplers/probability.rb +13 -5
  31. data/lib/opencensus/trace/samplers/rate_limiting.rb +74 -0
  32. data/lib/opencensus/trace/span.rb +11 -5
  33. data/lib/opencensus/trace/span_builder.rb +84 -27
  34. data/lib/opencensus/trace/span_context.rb +76 -40
  35. data/lib/opencensus/trace/status.rb +227 -5
  36. data/lib/opencensus/trace/time_event.rb +1 -0
  37. data/lib/opencensus/trace/trace_context_data.rb +1 -0
  38. data/lib/opencensus/trace/truncatable_string.rb +1 -0
  39. data/lib/opencensus/version.rb +2 -1
  40. metadata +6 -6
  41. data/lib/opencensus/trace/samplers/max_qps.rb +0 -55
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fcd6b186ce0092ad6cf63ec39de790c05ba2a8e5
4
- data.tar.gz: d23b444fb33a05c2333d745e72a956217239a622
2
+ SHA256:
3
+ metadata.gz: 536fa079d66a3bb4bef7a0e70439d5e9cc016f25e86e775a74d7426c4ef3a7e7
4
+ data.tar.gz: '00688752f3c40bd18f08d4b0f311b8bd16435798647b1c09b70ad3c9240fb233'
5
5
  SHA512:
6
- metadata.gz: 59fdc1eaacc8e1f6616fc6948a78338756af8091d0be7ee5bd953270cdd2d48051048f72b56a7e9c030afea97d9f4dfe9356655586ef6dd4e672646715a495bc
7
- data.tar.gz: a9879fbf66e97502c4c35ad4ea99365a17865138bfb4c05eb3701797b4c41990bc3bf48b7431ffb1ca01f4b3c55c824da7a2ec59c9b9bc4d8d66ec9bdc4eaf5e
6
+ metadata.gz: 50ef3104c577f850c1b4dc8b7bbe9da6030eab510f413c9cec5934da88f0b80232a97b77b7a50f5997925b53601f8796089ab7587c011bb28f2cf593171f37ea
7
+ data.tar.gz: 8f4735b8955070ea7cbec12fd9260fe0a6f0647c0702837e7e74691e05b8f8bebaf2e7be92b3065b89e865cc2d2b4f100cf8ff2b6a2a1e57dceafbd9e9746da4
@@ -1,5 +1,28 @@
1
1
  # Release History
2
2
 
3
+ ### 0.4.0 / 2018-10-22
4
+
5
+ This is an important update that includes a number of fixes and brings the
6
+ library up to date with the latest OpenCensus specification.
7
+
8
+ Backward-incompatible changes:
9
+
10
+ * Local parent span's sampling decision is propagated to children by default, to match the current sampling spec.
11
+ * It is no longer possible to change a sampling decision after a SpanBuilder has been created, to match the current sampling spec.
12
+ * Renamed MaxQPS sampler to RateLimiting, and modified the implementation to match the current spec.
13
+ * Probability sampler defaults to 1 in 10,000.
14
+ * `true` or `false` may be passed as a sampler, as shortcuts for AlwaysSample or NeverSample.
15
+ * Standard samplers are now thread-safe.
16
+ * HTTP status codes are properly translated to OpenCensus status codes, in particular for spans generated by the Faraday and Rack middleware.
17
+ * Faraday and Rack middleware produce attributes matching the OpenCensus HTTP spec instead of the Stackdriver Trace service spec. The Stackdriver exporter now translates these attributes accordingly.
18
+ * Update standard trace context HTTP header to "traceparent" following updated spec.
19
+
20
+ Other fixes:
21
+
22
+ * Allow requiring "opencensus/trace" directly.
23
+ * Return SpanContext objects when block not given.
24
+ * FaradayMiddleware closes span if Faraday raises an exception.
25
+
3
26
  ### 0.3.1 / 2018-04-13
4
27
 
5
28
  * Clean unneeded files from the gem
data/README.md CHANGED
@@ -98,7 +98,7 @@ additional custom spans to the request trace:
98
98
  ```ruby
99
99
  OpenCensus::Trace.in_span "my_task" do |span|
100
100
  # Do stuff...
101
-
101
+
102
102
  OpenCensus::Trace.in_span "my_subtask" do |subspan|
103
103
  # Do other stuff
104
104
  end
@@ -112,8 +112,15 @@ module for more info.
112
112
  ### Exporting traces
113
113
 
114
114
  By default, OpenCensus will log request trace data as JSON. To export traces to
115
- your favorite analytics backend, install an export plugin. Plugins are
116
- currently being developed for Stackdriver, Zipkin, and other services.
115
+ your favorite analytics backend, install an export plugin.
116
+
117
+ The provided exporters are:
118
+
119
+ | Class | Description |
120
+ | ----- | ----------- |
121
+ | [LoggerExporter][logger-exporter] | Exporter JSON encoded spans to a standard Ruby Logger interface |
122
+ | [StackdriverExporter][stackdriver-exporter] | Report traces to Google Cloud Stackdriver Trace |
123
+ | [ZipkinExporter][zipkin-exporter] | Report collected spans to a Zipkin server |
117
124
 
118
125
  You may also create your own
119
126
  [Exporter](http://www.rubydoc.info/gems/opencensus/OpenCensus/Trace/Exporters)
@@ -193,3 +200,7 @@ This library is licensed under Apache 2.0. Full license text is available in
193
200
  ## Disclaimer
194
201
 
195
202
  This is not an official Google product.
203
+
204
+ [logger-exporter]: https://www.rubydoc.info/gems/opencensus/OpenCensus/Trace/Exporters/Logger
205
+ [stackdriver-exporter]: https://github.com/census-ecosystem/opencensus-ruby-exporter-stackdriver
206
+ [zipkin-exporter]: https://github.com/census-ecosystem/opencensus-ruby-exporter-zipkin
@@ -12,6 +12,18 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
16
+ ##
17
+ # OpenCensus is a vendor-agnostic single distribution of libraries to provide
18
+ # metrics collection and tracing for your services. See https://opencensus.io/
19
+ # for general information on OpenCensus.
20
+ #
21
+ # The OpenCensus module provides a namespace for the Ruby implementation of
22
+ # OpenCensus, including the core libraries for OpenCensus metrics and tracing.
23
+ #
24
+ module OpenCensus
25
+ end
26
+
15
27
  require "opencensus/common"
16
28
  require "opencensus/config"
17
29
  require "opencensus/context"
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "opencensus/common/config"
16
17
 
17
18
  module OpenCensus
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  module Common
17
18
  ##
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "opencensus/common/config"
16
17
 
17
18
  module OpenCensus
@@ -20,8 +21,9 @@ module OpenCensus
20
21
 
21
22
  class << self
22
23
  ##
23
- # Configure OpenCensus. Most configuration parameters are part of
24
- # subconfigurations that live under this main configuration.
24
+ # Configure OpenCensus. Most configuration parameters are defined in
25
+ # subconfigurations that live under this main configuration. See, for
26
+ # example, {OpenCensus::Trace.configure}.
25
27
  #
26
28
  # If the OpenCensus Railtie is installed in a Rails application, the
27
29
  # toplevel configuration object is also exposed as `config.opencensus`.
@@ -33,7 +35,7 @@ module OpenCensus
33
35
  #
34
36
  # OpenCensus.configure do |config|
35
37
  # config.trace.default_sampler =
36
- # OpenCensus::Trace::Samplers::AlwaysSample.new
38
+ # OpenCensus::Trace::Samplers::RateLimiting.new
37
39
  # config.trace.default_max_attributes = 16
38
40
  # end
39
41
  #
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  ##
17
18
  # The Context module provides per-thread storage.
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  ##
17
18
  # The Stats module contains support for OpenCensus stats collection.
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  ##
17
18
  # The Tags module contains support for OpenCensus tags. Tags are key-value
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
16
+ require "opencensus/context"
15
17
  require "opencensus/trace/annotation"
16
18
  require "opencensus/trace/config"
17
19
  require "opencensus/trace/exporters"
@@ -77,7 +79,7 @@ module OpenCensus
77
79
  # Starts tracing a request in the current thread, by creating a new
78
80
  # SpanContext and setting it as the current thread-local context.
79
81
  # Generally you should call this when beginning the handling of a
80
- # request. If there is a rack environment or a provided Trace-Context
82
+ # request. If there is a rack environment or a provided traceparent
81
83
  # header, pass it in so the SpanContext is constructed accordingly.
82
84
  #
83
85
  # If you pass a block, this method will yield the SpanContext to the
@@ -103,13 +105,15 @@ module OpenCensus
103
105
  ensure
104
106
  unset_span_context
105
107
  end
108
+ else
109
+ span_context
106
110
  end
107
111
  end
108
112
 
109
113
  ##
110
114
  # Create a new span in the current thread-local context.
111
115
  # You must pass a name for the span. All other span attributes should
112
- # be set using the SpanBuilder methods.
116
+ # be set using {OpenCensus::Trace::SpanBuilder} methods.
113
117
  #
114
118
  # The span will be started automatically with the current timestamp.
115
119
  # However, you are responsible for finishing the span yourself.
@@ -123,10 +127,18 @@ module OpenCensus
123
127
  #
124
128
  # Will throw an exception if there is no current SpanContext.
125
129
  #
126
- # @param [String] name Name of the span
127
- # @param [Symbol] kind Kind of span. Defaults to unspecified.
128
- # @param [Sampler] sampler Span-scoped sampler. If not provided,
129
- # defaults to the trace configuration's default sampler.
130
+ # @param [String] name Name of the span. Required.
131
+ # @param [Symbol] kind Kind of span. Optional. Defaults to unspecified.
132
+ # Other allowed values are {OpenCensus::Trace::SpanBuilder::SERVER}
133
+ # and {OpenCensus::Trace::SpanBuilder::CLIENT}.
134
+ # @param [Sampler,Boolean,nil] sampler Span-scoped sampler. Optional.
135
+ # If provided, the sampler may be a sampler object as defined in the
136
+ # {OpenCensus::Trace::Samplers} module docs, or the values `true` or
137
+ # `false` as shortcuts for {OpenCensus::Trace::Samplers::AlwaysSample}
138
+ # or {OpenCensus::Trace::Samplers::NeverSample}, respectively. If no
139
+ # span-scoped sampler is provided, the local parent span's sampling
140
+ # decision is used. If there is no local parent span, the configured
141
+ # default sampler is used to make a sampling decision.
130
142
  #
131
143
  # @return [SpanBuilder] A SpanBuilder object that you can use to
132
144
  # set span attributes and create children.
@@ -144,7 +156,7 @@ module OpenCensus
144
156
  ##
145
157
  # Create a new span in this context.
146
158
  # You must pass a name for the span. All other span attributes should
147
- # be set using the SpanBuilder methods.
159
+ # be set using {OpenCensus::Trace::SpanBuilder} methods.
148
160
  #
149
161
  # The span will be started automatically with the current timestamp. The
150
162
  # SpanBuilder will then be passed to the block you provide. The span will
@@ -152,10 +164,18 @@ module OpenCensus
152
164
  # the thread-local SpanContext will be updated so calls to `start_span`
153
165
  # will create subspans.
154
166
  #
155
- # @param [String] name Name of the span
156
- # @param [Symbol] kind Kind of span. Defaults to unspecified.
157
- # @param [Sampler] sampler Span-scoped sampler. If not provided,
158
- # defaults to the trace configuration's default sampler.
167
+ # @param [String] name Name of the span. Required.
168
+ # @param [Symbol] kind Kind of span. Optional. Defaults to unspecified.
169
+ # Other allowed values are {OpenCensus::Trace::SpanBuilder::SERVER}
170
+ # and {OpenCensus::Trace::SpanBuilder::CLIENT}.
171
+ # @param [Sampler,Boolean,nil] sampler Span-scoped sampler. Optional.
172
+ # If provided, the sampler may be a sampler object as defined in the
173
+ # {OpenCensus::Trace::Samplers} module docs, or the values `true` or
174
+ # `false` as shortcuts for {OpenCensus::Trace::Samplers::AlwaysSample}
175
+ # or {OpenCensus::Trace::Samplers::NeverSample}, respectively. If no
176
+ # span-scoped sampler is provided, the local parent span's sampling
177
+ # decision is used. If there is no local parent span, the configured
178
+ # default sampler is used to make a sampling decision.
159
179
  #
160
180
  def in_span name, kind: nil, skip_frames: 0, sampler: nil
161
181
  span = start_span name, kind: kind, skip_frames: skip_frames + 1,
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "opencensus/trace/time_event"
16
17
 
17
18
  module OpenCensus
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
16
+ require "opencensus/config"
15
17
  require "opencensus/trace/exporters"
16
18
  require "opencensus/trace/formatters"
17
19
  require "opencensus/trace/samplers"
@@ -21,16 +23,16 @@ module OpenCensus
21
23
  # Schema of the Trace configuration. See Trace#configure for more info.
22
24
  @config = Common::Config.new do |config|
23
25
  default_sampler = Samplers::AlwaysSample.new
26
+ exporter_logger = ::Logger.new STDOUT, ::Logger::INFO
27
+ default_exporter = Exporters::Logger.new exporter_logger
28
+ default_formatter = Formatters::TraceContext.new
29
+
24
30
  config.add_option! :default_sampler, default_sampler do |value|
25
31
  value.respond_to? :call
26
32
  end
27
- default_exporter =
28
- Exporters::Logger.new ::Logger.new(STDOUT, ::Logger::INFO)
29
33
  config.add_option! :exporter, default_exporter do |value|
30
34
  value.respond_to? :export
31
35
  end
32
- default_formatter =
33
- Formatters::TraceContext.new
34
36
  config.add_option! :http_formatter, default_formatter do |value|
35
37
  value.respond_to?(:serialize) &&
36
38
  value.respond_to?(:deserialize) &&
@@ -67,7 +69,7 @@ module OpenCensus
67
69
  #
68
70
  # OpenCensus::Trace.configure do |config|
69
71
  # config.default_sampler =
70
- # OpenCensus::Trace::Samplers::AlwaysSample.new
72
+ # OpenCensus::Trace::Samplers::RateLimiting.new
71
73
  # config.default_max_attributes = 16
72
74
  # end
73
75
  #
@@ -75,17 +77,17 @@ module OpenCensus
75
77
  #
76
78
  # * `default_sampler` The default sampler to use. Must be a sampler,
77
79
  # an object with a `call` method that takes a single options hash.
78
- # See OpenCensus::Trace::Samplers. The initial value is a Probability
79
- # sampler with a default rate.
80
+ # See {OpenCensus::Trace::Samplers}. The initial value is an instance
81
+ # of {OpenCensus::Trace::Samplers::AlwaysSample}.
80
82
  # * `exporter` The exporter to use. Must be an exporter, an object with
81
83
  # an export method that takes an array of Span objects. See
82
- # OpenCensus::Trace::Exporters. The initial value is a Logger exporter
83
- # that logs to STDOUT.
84
+ # {OpenCensus::Trace::Exporters}. The initial value is a
85
+ # {OpenCensus::Trace::Exporters::Logger} that logs to STDOUT.
84
86
  # * `http_formatter` The trace context propagation formatter to use.
85
87
  # Must be a formatter, an object with `serialize`, `deserialize`,
86
88
  # `header_name`, and `rack_header_name` methods. See
87
- # OpenCensus::Trace::Formatter. The initial value is a TraceContext
88
- # formatter.
89
+ # {OpenCensus::Trace::Formatters}. The initial value is a
90
+ # {OpenCensus::Trace::Formatters::TraceContext}.
89
91
  # * `default_max_attributes` The maximum number of attributes to add to
90
92
  # a span. Initial value is 32. Use 0 for no maximum.
91
93
  # * `default_max_stack_frames` The maximum number of stack frames to
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "opencensus/trace/exporters/logger"
16
17
  require "opencensus/trace/exporters/multi"
17
18
 
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "logger"
16
17
  require "json"
17
18
 
@@ -27,9 +28,9 @@ module OpenCensus
27
28
  # Create a new Logger exporter
28
29
  #
29
30
  # @param [#log] logger The logger to write to.
30
- # @param [Symbol] level The log level. This should be a log level
31
- # defined by {https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html
32
- # Logger Standard Library}.
31
+ # @param [Integer] level The log level. This should be a log level
32
+ # defined by the Logger standard library. Default is
33
+ # `::Logger::INFO`.
33
34
  #
34
35
  def initialize logger, level: ::Logger::INFO
35
36
  @logger = logger
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "delegate"
16
17
 
17
18
  module OpenCensus
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  require "opencensus/trace/formatters/binary"
16
17
  require "opencensus/trace/formatters/cloud_trace"
17
18
  require "opencensus/trace/formatters/trace_context"
@@ -20,8 +21,8 @@ module OpenCensus
20
21
  module Trace
21
22
  ##
22
23
  # The Formatters module contains several implementations of cross-service
23
- # SpanContext propagation. Each formatter can serialize and deserialize a
24
- # TraceContextData instance.
24
+ # context propagation. Each formatter can serialize and deserialize a
25
+ # {OpenCensus::Trace::TraceContextData} instance.
25
26
  #
26
27
  module Formatters
27
28
  end
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  module Trace
17
18
  module Formatters
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  module Trace
17
18
  module Formatters
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  module Trace
17
18
  module Formatters
@@ -41,7 +42,7 @@ module OpenCensus
41
42
  #
42
43
  # @private
43
44
  #
44
- HEADER_NAME = "Trace-Context".freeze
45
+ HEADER_NAME = "traceparent".freeze
45
46
 
46
47
  ##
47
48
  # The rack environment header used for the TraceContext header
@@ -49,7 +50,7 @@ module OpenCensus
49
50
  #
50
51
  # @private
51
52
  #
52
- RACK_HEADER_NAME = "HTTP_TRACE_CONTEXT".freeze
53
+ RACK_HEADER_NAME = "HTTP_TRACEPARENT".freeze
53
54
 
54
55
  ##
55
56
  # Returns the name of the header used for context propagation.
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+
15
16
  module OpenCensus
16
17
  module Trace
17
18
  ##
@@ -12,8 +12,9 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require "faraday"
16
15
 
16
+ require "faraday"
17
+ require "uri"
17
18
  require "opencensus"
18
19
 
19
20
  module OpenCensus
@@ -88,8 +89,11 @@ module OpenCensus
88
89
  # }
89
90
  # end
90
91
  #
91
- class FaradayMiddleware < Faraday::Middleware
92
- ## The default name for Faraday spans
92
+ class FaradayMiddleware < ::Faraday::Middleware
93
+ ##
94
+ # Fallback span name
95
+ # @return [String]
96
+ #
93
97
  DEFAULT_SPAN_NAME = "faraday_request".freeze
94
98
 
95
99
  ##
@@ -101,8 +105,8 @@ module OpenCensus
101
105
  # current thread-local span context.
102
106
  # @param [String, #call] span_name The name of the span to create.
103
107
  # Can be a string or a callable that takes a faraday request env
104
- # and returns a string. Optional: If omitted, uses
105
- # `DEFAULT_SPAN_NAME`
108
+ # and returns a string. Optional: If omitted, uses the request path
109
+ # as recommended in the OpenCensus spec.
106
110
  # @param [#call] sampler The sampler to use when creating spans.
107
111
  # Optional: If omitted, uses the sampler in the current config.
108
112
  # @param [#serialize,#header_name] formatter The formatter to use when
@@ -113,7 +117,7 @@ module OpenCensus
113
117
  formatter: nil
114
118
  @app = app
115
119
  @span_context = span_context || OpenCensus::Trace
116
- @span_name = span_name || DEFAULT_SPAN_NAME
120
+ @default_span_name = span_name
117
121
  @sampler = sampler
118
122
  @formatter = formatter || OpenCensus::Trace.config.http_formatter
119
123
  end
@@ -128,36 +132,60 @@ module OpenCensus
128
132
  return @app.call request_env
129
133
  end
130
134
 
131
- span_name = request_env[:span_name] || @span_name
135
+ span_name = extract_span_name(request_env) || @default_span_name ||
136
+ DEFAULT_SPAN_NAME
132
137
  span_name = span_name.call request_env if span_name.respond_to? :call
133
138
 
134
139
  span = span_context.start_span span_name, sampler: @sampler
135
140
  start_request span, request_env
136
- @app.call(request_env).on_complete do |response_env|
137
- finish_request span, response_env
141
+ begin
142
+ @app.call(request_env).on_complete do |response_env|
143
+ finish_request span, response_env
144
+ end
145
+ rescue StandardError => e
146
+ span.set_status 2, e.message
147
+ raise
148
+ ensure
138
149
  span_context.end_span span
139
150
  end
140
151
  end
141
152
 
142
153
  protected
143
154
 
155
+ ##
156
+ # @private Get the span name from the request object
157
+ #
158
+ def extract_span_name env
159
+ name = env[:span_name]
160
+ return name if name
161
+ uri = build_uri env
162
+ return nil unless uri
163
+ path = uri.path.to_s
164
+ path.start_with?("/") ? path : "/#{path}"
165
+ end
166
+
144
167
  ##
145
168
  # @private Set span attributes from request object
146
169
  #
147
170
  def start_request span, env
148
171
  span.kind = SpanBuilder::CLIENT
149
172
  req_method = env[:method]
150
- span.put_attribute "/http/method", req_method if req_method
151
- url = env[:url]
152
- span.put_attribute "/http/url", url if url
173
+ span.put_attribute "http.method", req_method.upcase if req_method
174
+ uri = build_uri env
175
+ if uri
176
+ span.put_attribute "http.host", uri.hostname.to_s
177
+ span.put_attribute "http.path", uri.path.to_s
178
+ end
153
179
  body = env[:body]
154
- body_size = body.bytesize if body.respond_to? :bytesize
155
- span.put_attribute "/rpc/request/size", body_size if body_size
180
+ body_size = body.respond_to?(:bytesize) ? body.bytesize : 0
181
+ span.put_message_event SpanBuilder::SENT, 1, body_size
156
182
 
157
183
  formatter = env[:formatter] || @formatter
158
- trace_context = formatter.serialize span.context.trace_context
159
- headers = env[:request_headers] ||= {}
160
- headers[formatter.header_name] = trace_context
184
+ if formatter.respond_to? :header_name
185
+ headers = env[:request_headers] ||= {}
186
+ trace_context = formatter.serialize span.context.trace_context
187
+ headers[formatter.header_name] = trace_context
188
+ end
161
189
  end
162
190
 
163
191
  ##
@@ -166,12 +194,20 @@ module OpenCensus
166
194
  def finish_request span, env
167
195
  status = env[:status].to_i
168
196
  if status > 0
169
- span.set_status status
170
- span.put_attribute "/rpc/status_code", status
197
+ span.set_http_status status
198
+ span.put_attribute "http.status_code", status
171
199
  end
172
200
  body = env[:body]
173
- body_size = body.bytesize if body.respond_to? :bytesize
174
- span.put_attribute "/rpc/response/size", body_size if body_size
201
+ body_size = body.respond_to?(:bytesize) ? body.bytesize : 0
202
+ span.put_message_event SpanBuilder::RECEIVED, 1, body_size
203
+ end
204
+
205
+ private
206
+
207
+ def build_uri env
208
+ ::URI.parse env[:url]
209
+ rescue ::URI::InvalidURIError
210
+ nil
175
211
  end
176
212
  end
177
213
  end