opencensus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rubocop.yml +48 -0
  4. data/.travis.yml +16 -0
  5. data/AUTHORS +1 -0
  6. data/CODE_OF_CONDUCT.md +43 -0
  7. data/CONTRIBUTING.md +34 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE +201 -0
  10. data/README.md +180 -0
  11. data/Rakefile +20 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/docs/.gitignore +3 -0
  15. data/docs/404.html +24 -0
  16. data/docs/Gemfile +31 -0
  17. data/docs/_config.yml +39 -0
  18. data/docs/_layouts/default.html +65 -0
  19. data/docs/index.md +151 -0
  20. data/lib/opencensus.rb +21 -0
  21. data/lib/opencensus/common.rb +24 -0
  22. data/lib/opencensus/common/config.rb +521 -0
  23. data/lib/opencensus/config.rb +54 -0
  24. data/lib/opencensus/context.rb +72 -0
  25. data/lib/opencensus/stats.rb +26 -0
  26. data/lib/opencensus/tags.rb +25 -0
  27. data/lib/opencensus/trace.rb +181 -0
  28. data/lib/opencensus/trace/annotation.rb +60 -0
  29. data/lib/opencensus/trace/config.rb +119 -0
  30. data/lib/opencensus/trace/exporters.rb +26 -0
  31. data/lib/opencensus/trace/exporters/logger.rb +149 -0
  32. data/lib/opencensus/trace/formatters.rb +29 -0
  33. data/lib/opencensus/trace/formatters/binary.rb +66 -0
  34. data/lib/opencensus/trace/formatters/cloud_trace.rb +102 -0
  35. data/lib/opencensus/trace/formatters/trace_context.rb +124 -0
  36. data/lib/opencensus/trace/integrations.rb +24 -0
  37. data/lib/opencensus/trace/integrations/faraday_middleware.rb +176 -0
  38. data/lib/opencensus/trace/integrations/rack_middleware.rb +127 -0
  39. data/lib/opencensus/trace/integrations/rails.rb +121 -0
  40. data/lib/opencensus/trace/link.rb +90 -0
  41. data/lib/opencensus/trace/message_event.rb +80 -0
  42. data/lib/opencensus/trace/samplers.rb +50 -0
  43. data/lib/opencensus/trace/samplers/always_sample.rb +34 -0
  44. data/lib/opencensus/trace/samplers/max_qps.rb +55 -0
  45. data/lib/opencensus/trace/samplers/never_sample.rb +34 -0
  46. data/lib/opencensus/trace/samplers/probability.rb +69 -0
  47. data/lib/opencensus/trace/span.rb +196 -0
  48. data/lib/opencensus/trace/span_builder.rb +560 -0
  49. data/lib/opencensus/trace/span_context.rb +308 -0
  50. data/lib/opencensus/trace/status.rb +49 -0
  51. data/lib/opencensus/trace/time_event.rb +38 -0
  52. data/lib/opencensus/trace/trace_context_data.rb +22 -0
  53. data/lib/opencensus/trace/truncatable_string.rb +61 -0
  54. data/lib/opencensus/version.rb +18 -0
  55. data/opencensus.gemspec +32 -0
  56. metadata +210 -0
@@ -0,0 +1,54 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "opencensus/common/config"
16
+
17
+ module OpenCensus
18
+ # The OpenCensus overall configuration.
19
+ @config = Common::Config.new
20
+
21
+ class << self
22
+ ##
23
+ # Configure OpenCensus. Most configuration parameters are part of
24
+ # subconfigurations that live under this main configuration.
25
+ #
26
+ # If the OpenCensus Railtie is installed in a Rails application, the
27
+ # toplevel configuration object is also exposed as `config.opencensus`.
28
+ #
29
+ # Generally, you should configure this once at process initialization,
30
+ # but it can be modified at any time.
31
+ #
32
+ # Example:
33
+ #
34
+ # OpenCensus.configure do |config|
35
+ # config.trace.default_sampler =
36
+ # OpenCensus::Trace::Samplers::AlwaysSample.new
37
+ # config.trace.default_max_attributes = 16
38
+ # end
39
+ #
40
+ def configure
41
+ if block_given?
42
+ yield @config
43
+ else
44
+ @config
45
+ end
46
+ end
47
+
48
+ ##
49
+ # Get the current configuration.
50
+ # @private
51
+ #
52
+ attr_reader :config
53
+ end
54
+ end
@@ -0,0 +1,72 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OpenCensus
16
+ ##
17
+ # The Context module provides per-thread storage.
18
+ #
19
+ module Context
20
+ ##
21
+ # Thread local storage key under which all OpenCensus context data is
22
+ # stored.
23
+ #
24
+ # @private
25
+ #
26
+ THREAD_KEY = :__opencensus_context__
27
+
28
+ class << self
29
+ ##
30
+ # Store a value in the context.
31
+ #
32
+ # @param [String, Symbol] key The name of the context value to store.
33
+ # @param [Object] value The value associated with the key.
34
+ def set key, value
35
+ storage[key] = value
36
+ end
37
+
38
+ ##
39
+ # Return a value from the context. Returns nil if no value is set.
40
+ #
41
+ # @param [String, Symbol] key The name of the context value to fetch.
42
+ # @return [Object, nil] The fetched value.
43
+ #
44
+ def get key
45
+ storage[key]
46
+ end
47
+
48
+ ##
49
+ # Unsets a value from the context.
50
+ #
51
+ # @param [String, Symbol] key The name of the context value to unset.
52
+ # @return [Object, nil] The value of the context value just unset.
53
+ #
54
+ def unset key
55
+ storage.delete key
56
+ end
57
+
58
+ ##
59
+ # Clears all values from the context.
60
+ #
61
+ def reset!
62
+ Thread.current[THREAD_KEY] = {}
63
+ end
64
+
65
+ private
66
+
67
+ def storage
68
+ Thread.current[THREAD_KEY] ||= {}
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OpenCensus
16
+ ##
17
+ # The Stats module contains support for OpenCensus stats collection.
18
+ #
19
+ # OpenCensus allows users to create typed measures, record measurements,
20
+ # aggregate the collected data, and export the aggregated data.
21
+ #
22
+ # TODO: implement stats
23
+ #
24
+ module Stats
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OpenCensus
16
+ ##
17
+ # The Tags module contains support for OpenCensus tags. Tags are key-value
18
+ # pairs. Tags provide additional cardinality to the OpenCensus instrumentation
19
+ # data.
20
+ #
21
+ # TODO: implement tags
22
+ #
23
+ module Tags
24
+ end
25
+ end
@@ -0,0 +1,181 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "opencensus/trace/annotation"
16
+ require "opencensus/trace/config"
17
+ require "opencensus/trace/exporters"
18
+ require "opencensus/trace/formatters"
19
+ require "opencensus/trace/integrations"
20
+ require "opencensus/trace/link"
21
+ require "opencensus/trace/message_event"
22
+ require "opencensus/trace/samplers"
23
+ require "opencensus/trace/span_builder"
24
+ require "opencensus/trace/span_context"
25
+ require "opencensus/trace/span"
26
+ require "opencensus/trace/status"
27
+ require "opencensus/trace/trace_context_data"
28
+ require "opencensus/trace/truncatable_string"
29
+
30
+ module OpenCensus
31
+ ##
32
+ # # OpenCensus Trace API
33
+ #
34
+ # This is a Ruby implementation of OpenCensus Trace, providing a common API
35
+ # for latency trace tools.
36
+ #
37
+ module Trace
38
+ ##
39
+ # Internal key for storing the current SpanContext in the thread local
40
+ # Context
41
+ #
42
+ # @private
43
+ #
44
+ SPAN_CONTEXT_KEY = :__span_context__
45
+
46
+ class << self
47
+ ##
48
+ # Sets the current thread-local SpanContext, which governs the behavior
49
+ # of the span creation class methods of OpenCensus::Trace.
50
+ #
51
+ # @param [SpanContext] span_context
52
+ #
53
+ def span_context= span_context
54
+ OpenCensus::Context.set SPAN_CONTEXT_KEY, span_context
55
+ end
56
+
57
+ ##
58
+ # Unsets the current thread-local SpanContext, disabling span creation
59
+ # class methods of OpenCensus::Trace
60
+ #
61
+ def unset_span_context
62
+ OpenCensus::Context.unset SPAN_CONTEXT_KEY
63
+ end
64
+
65
+ ##
66
+ # Returns the current thread-local SpanContext, which governs the
67
+ # behavior of the span creation class methods of OpenCensus::Trace.
68
+ # Returns `nil` if there is no current SpanContext.
69
+ #
70
+ # @return [SpanContext, nil]
71
+ #
72
+ def span_context
73
+ OpenCensus::Context.get SPAN_CONTEXT_KEY
74
+ end
75
+
76
+ ##
77
+ # Starts tracing a request in the current thread, by creating a new
78
+ # SpanContext and setting it as the current thread-local context.
79
+ # Generally you should call this when beginning the handling of a
80
+ # request. If there is a rack environment or a provided Trace-Context
81
+ # header, pass it in so the SpanContext is constructed accordingly.
82
+ #
83
+ # If you pass a block, this method will yield the SpanContext to the
84
+ # block. When the block finishes, the span context will automatically
85
+ # be unset. If you do not pass a block, this method will return the
86
+ # SpanContext. You must then call `unset_span_context` yourself at the
87
+ # end of the request
88
+ #
89
+ # @param [TraceContextData] trace_context The request's incoming trace
90
+ # context (optional)
91
+ #
92
+ def start_request_trace trace_context: nil
93
+ span_context = SpanContext.create_root trace_context: trace_context
94
+ self.span_context = span_context
95
+ if block_given?
96
+ begin
97
+ yield span_context
98
+ ensure
99
+ unset_span_context
100
+ end
101
+ end
102
+ end
103
+
104
+ ##
105
+ # Create a new span in the current thread-local context.
106
+ # You must pass a name for the span. All other span attributes should
107
+ # be set using the SpanBuilder methods.
108
+ #
109
+ # The span will be started automatically with the current timestamp.
110
+ # However, you are responsible for finishing the span yourself.
111
+ # Furthermore, the current thread-local SpanContext will be updated so
112
+ # subsequent calls to `start_span` will create spans within the new span.
113
+ #
114
+ # You should always match `start_span` calls with a corresponding call to
115
+ # `end_span`, which finishes the span and updates the SpanContext
116
+ # accordingly. If you want this done automatically, consider using
117
+ # the `in_span` method.
118
+ #
119
+ # Will throw an exception if there is no current SpanContext.
120
+ #
121
+ # @param [String] name Name of the span
122
+ # @param [Sampler] sampler Span-scoped sampler. If not provided,
123
+ # defaults to the trace configuration's default sampler.
124
+ #
125
+ # @return [SpanBuilder] A SpanBuilder object that you can use to
126
+ # set span attributes and create children.
127
+ #
128
+ def start_span name, skip_frames: 0, sampler: nil
129
+ context = span_context
130
+ raise "No currently active span context" unless context
131
+ span = context.start_span name, skip_frames: skip_frames + 1,
132
+ sampler: sampler
133
+ self.span_context = span.context
134
+ span
135
+ end
136
+
137
+ ##
138
+ # Create a new span in this context.
139
+ # You must pass a name for the span. All other span attributes should
140
+ # be set using the SpanBuilder methods.
141
+ #
142
+ # The span will be started automatically with the current timestamp. The
143
+ # SpanBuilder will then be passed to the block you provide. The span will
144
+ # be finished automatically at the end of the block. Within the block,
145
+ # the thread-local SpanContext will be updated so calls to `start_span`
146
+ # will create subspans.
147
+ #
148
+ # @param [String] name Name of the span
149
+ # @param [Sampler] sampler Span-scoped sampler. If not provided,
150
+ # defaults to the trace configuration's default sampler.
151
+ #
152
+ def in_span name, skip_frames: 0, sampler: nil
153
+ span = start_span name, skip_frames: skip_frames + 1, sampler: sampler
154
+ begin
155
+ yield span
156
+ ensure
157
+ end_span span
158
+ end
159
+ end
160
+
161
+ ##
162
+ # Finish the given span, which must be the span that created the
163
+ # current thread-local SpanContext. Also updates the thread-local
164
+ # SpanContext so subsequent calls no longer create subspans of the
165
+ # finished span.
166
+ #
167
+ # @param [SpanBuilder] span The expected currently active span to finish.
168
+ #
169
+ def end_span span
170
+ context = span_context
171
+ raise "No currently active span context" unless context
172
+ unless span.equal? context.this_span
173
+ raise "The given span doesn't match the currently active span"
174
+ end
175
+ span.finish!
176
+ self.span_context = context.parent
177
+ span
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,60 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "opencensus/trace/time_event"
16
+
17
+ module OpenCensus
18
+ module Trace
19
+ ##
20
+ # A text annotation with a set of attributes.
21
+ #
22
+ class Annotation < TimeEvent
23
+ ##
24
+ # A user-supplied message describing the event.
25
+ #
26
+ # @return [TruncatableString]
27
+ #
28
+ attr_reader :description
29
+
30
+ ##
31
+ # A set of attributes on the annotation.
32
+ #
33
+ # @return [Hash<String, (TruncatableString, Integer, Boolean)>]
34
+ #
35
+ attr_reader :attributes
36
+
37
+ ##
38
+ # The number of attributes that were discarded. Attributes can be
39
+ # discarded because their keys are too long or because there are too
40
+ # many attributes. If this value is 0, then no attributes were dropped.
41
+ #
42
+ # @return [Integer]
43
+ #
44
+ attr_reader :dropped_attributes_count
45
+
46
+ ##
47
+ # Create an Annotation object.
48
+ #
49
+ # @private
50
+ #
51
+ def initialize description, attributes: {}, dropped_attributes_count: 0,
52
+ time: nil
53
+ super time: time
54
+ @description = description
55
+ @attributes = attributes
56
+ @dropped_attributes_count = dropped_attributes_count
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,119 @@
1
+ # Copyright 2017 OpenCensus Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "opencensus/trace/exporters"
16
+ require "opencensus/trace/formatters"
17
+ require "opencensus/trace/samplers"
18
+
19
+ module OpenCensus
20
+ module Trace
21
+ # Schema of the Trace configuration. See Trace#configure for more info.
22
+ @config = Common::Config.new do |config|
23
+ default_sampler =
24
+ Samplers::Probability.new Samplers::Probability::DEFAULT_RATE
25
+ config.add_option! :default_sampler, default_sampler do |value|
26
+ value.respond_to? :call
27
+ end
28
+ default_exporter =
29
+ Exporters::Logger.new ::Logger.new(STDOUT, ::Logger::INFO)
30
+ config.add_option! :exporter, default_exporter do |value|
31
+ value.respond_to? :export
32
+ end
33
+ default_formatter =
34
+ Formatters::TraceContext.new
35
+ config.add_option! :http_formatter, default_formatter do |value|
36
+ value.respond_to?(:serialize) &&
37
+ value.respond_to?(:deserialize) &&
38
+ value.respond_to?(:header_name) &&
39
+ value.respond_to?(:rack_header_name)
40
+ end
41
+ config.add_option! :default_max_attributes, 32
42
+ config.add_option! :default_max_stack_frames, 32
43
+ config.add_option! :default_max_annotations, 32
44
+ config.add_option! :default_max_message_events, 128
45
+ config.add_option! :default_max_links, 128
46
+ config.add_option! :default_max_string_length, 1024
47
+ end
48
+
49
+ # Expose the trace config as a subconfig under the main config.
50
+ OpenCensus.configure do |config|
51
+ config.add_alias! :trace, config: @config
52
+ end
53
+
54
+ class << self
55
+ ##
56
+ # Configure OpenCensus Trace. These configuration fields include
57
+ # parameters governing sampling, span creation, and exporting.
58
+ #
59
+ # This configuration is also available as the `trace` subconfig under the
60
+ # main configuration `OpenCensus.configure`. If the OpenCensus Railtie is
61
+ # installed in a Rails application, the configuration object is also
62
+ # exposed as `config.opencensus.trace`.
63
+ #
64
+ # Generally, you should configure this once at process initialization,
65
+ # but it can be modified at any time.
66
+ #
67
+ # Example:
68
+ #
69
+ # OpenCensus::Trace.configure do |config|
70
+ # config.default_sampler =
71
+ # OpenCensus::Trace::Samplers::AlwaysSample.new
72
+ # config.default_max_attributes = 16
73
+ # end
74
+ #
75
+ # Supported fields are:
76
+ #
77
+ # * `default_sampler` The default sampler to use. Must be a sampler,
78
+ # an object with a `call` method that takes a single options hash.
79
+ # See OpenCensus::Trace::Samplers. The initial value is a Probability
80
+ # sampler with a default rate.
81
+ # * `exporter` The exporter to use. Must be an exporter, an object with
82
+ # an export method that takes an array of Span objects. See
83
+ # OpenCensus::Trace::Exporters. The initial value is a Logger exporter
84
+ # that logs to STDOUT.
85
+ # * `http_formatter` The trace context propagation formatter to use.
86
+ # Must be a formatter, an object with `serialize`, `deserialize`,
87
+ # `header_name`, and `rack_header_name` methods. See
88
+ # OpenCensus::Trace::Formatter. The initial value is a TraceContext
89
+ # formatter.
90
+ # * `default_max_attributes` The maximum number of attributes to add to
91
+ # a span. Initial value is 32. Use 0 for no maximum.
92
+ # * `default_max_stack_frames` The maximum number of stack frames to
93
+ # represent in a span's stack trace. Initial value is 32. Use 0 for
94
+ # no maximum.
95
+ # * `default_max_annotations` The maximum number of annotations to add
96
+ # to a span. Initial value is 32. Use 0 for no maximum.
97
+ # * `default_max_message_events` The maximum number of message events
98
+ # to add to a span. Initial value is 128. Use 0 for no maximum.
99
+ # * `default_max_links` The maximum number of links to add to a span.
100
+ # Initial value is 128. Use 0 for no maximum.
101
+ # * `default_max_string_length` The maximum length of string fields.
102
+ # Initial value is 1024. Use 0 for no maximum.
103
+ #
104
+ def configure
105
+ if block_given?
106
+ yield @config
107
+ else
108
+ @config
109
+ end
110
+ end
111
+
112
+ ##
113
+ # Get the current configuration
114
+ # @private
115
+ #
116
+ attr_reader :config
117
+ end
118
+ end
119
+ end