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,90 @@
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
+ module Trace
17
+ ##
18
+ # A pointer from the current span to another span in the same trace or in
19
+ # a different trace. For example, this can be used in batching operations,
20
+ # where a single batch handler processes multiple requests from different
21
+ # traces or when the handler receives a request from a different project.
22
+ #
23
+ class Link
24
+ # The relationship of the two spans is unknown, or known but other
25
+ # than parent-child.
26
+ TYPE_UNSPECIFIED = :TYPE_UNSPECIFIED
27
+
28
+ # The linked span is a child of the current span.
29
+ CHILD_LINKED_SPAN = :CHILD_LINKED_SPAN
30
+
31
+ # The linked span is a parent of the current span.
32
+ PARENT_LINKED_SPAN = :PARENT_LINKED_SPAN
33
+
34
+ ##
35
+ # A unique identifier for a trace. All spans from the same trace share
36
+ # the same `trace_id`. The ID is a 16-byte represented as a hexadecimal
37
+ # string.
38
+ #
39
+ # @return [String]
40
+ #
41
+ attr_reader :trace_id
42
+
43
+ ##
44
+ # A unique identifier for a span within a trace, assigned when the span
45
+ # is created. The ID is a 16-byte represented as a hexadecimal string.
46
+ #
47
+ # @return [String]
48
+ #
49
+ attr_reader :span_id
50
+
51
+ ##
52
+ # The relationship of the current span relative to the linked span. You
53
+ # should use the type constants provided by this class.
54
+ #
55
+ # @return [Symbol]
56
+ #
57
+ attr_reader :type
58
+
59
+ ##
60
+ # A set of attributes on the link.
61
+ #
62
+ # @return [Hash<String, (TruncatableString, Integer, Boolean)>]
63
+ #
64
+ attr_reader :attributes
65
+
66
+ ##
67
+ # The number of attributes that were discarded. Attributes can be
68
+ # discarded because their keys are too long or because there are too
69
+ # many attributes. If this value is 0, then no attributes were dropped.
70
+ #
71
+ # @return [Integer]
72
+ #
73
+ attr_reader :dropped_attributes_count
74
+
75
+ ##
76
+ # Create a Link object.
77
+ #
78
+ # @private
79
+ #
80
+ def initialize trace_id, span_id, type: nil,
81
+ attributes: {}, dropped_attributes_count: 0
82
+ @trace_id = trace_id
83
+ @span_id = span_id
84
+ @type = type
85
+ @attributes = attributes
86
+ @dropped_attributes_count = dropped_attributes_count
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,80 @@
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
+ # An event describing a message sent/received between Spans.
21
+ #
22
+ class MessageEvent < TimeEvent
23
+ # Unknown event type.
24
+ TYPE_UNSPECIFIED = :TYPE_UNSPECIFIED
25
+
26
+ # Indicates a sent message.
27
+ SENT = :SENT
28
+
29
+ # Indicates a received message.
30
+ RECEIVED = :RECEIVED
31
+
32
+ ##
33
+ # The type of MessageEvent. Indicates whether the message was sent or
34
+ # received. You should use the type constants provided by this class.
35
+ #
36
+ # @return [Symbol]
37
+ #
38
+ attr_reader :type
39
+
40
+ ##
41
+ # An identifier for the MessageEvent's message that can be used to match
42
+ # SENT and RECEIVED MessageEvents. For example, this field could
43
+ # represent a sequence ID for a streaming RPC. It is recommended to be
44
+ # unique within a Span.
45
+ #
46
+ # @return [Integer]
47
+ #
48
+ attr_reader :id
49
+
50
+ ##
51
+ # The number of uncompressed bytes sent or received.
52
+ #
53
+ # @return [Integer]
54
+ #
55
+ attr_reader :uncompressed_size
56
+
57
+ ##
58
+ # The number of compressed bytes sent or received. If zero, assumed to
59
+ # be the same size as uncompressed.
60
+ #
61
+ # @return [Integer, nil]
62
+ #
63
+ attr_reader :compressed_size
64
+
65
+ ##
66
+ # Create a new MessageEvent object.
67
+ #
68
+ # @private
69
+ #
70
+ def initialize type, id, uncompressed_size, compressed_size: nil,
71
+ time: nil
72
+ super time: time
73
+ @type = type
74
+ @id = id
75
+ @uncompressed_size = uncompressed_size
76
+ @compressed_size = compressed_size
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,50 @@
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/samplers/always_sample"
16
+ require "opencensus/trace/samplers/never_sample"
17
+ require "opencensus/trace/samplers/probability"
18
+ require "opencensus/trace/samplers/max_qps"
19
+
20
+ module OpenCensus
21
+ module Trace
22
+ ##
23
+ # A sampler determines whether a given request's latency trace should
24
+ # actually be reported. It is usually not necessary to trace every
25
+ # request, especially for an application serving heavy traffic. You may
26
+ # use a sampler to decide, for a given request, whether to report its
27
+ # trace.
28
+ #
29
+ # The OpenCensus specification defines three samplers: AlwaysSample,
30
+ # NeverSample, and Probability. The Ruby implementation also provides a
31
+ # fourth, MaxQPS, based on the Stackdriver library.
32
+ #
33
+ # A sampler is a Proc that takes a hash of environment information and
34
+ # returns a boolean indicating whether or not to sample the current
35
+ # request. Alternately, it could be an object that duck-types the Proc
36
+ # interface by implementing the `call` method. The hash passed to `call`
37
+ # may contain the following keys, all of which are optional. Samplers must
38
+ # adjust their behavior to account for the availability or absence of any
39
+ # environment information:
40
+ #
41
+ # * `span_context` The SpanContext that created the span being sampled.
42
+ # * `rack_env` The hash of Rack environment information
43
+ #
44
+ # Applications may set a default sampler in the config. In addition, the
45
+ # sampler may be overridden whenever a span is created.
46
+ #
47
+ module Samplers
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,34 @@
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
+ module Trace
17
+ module Samplers
18
+ ##
19
+ # The AlwaysSample sampler always returns true.
20
+ #
21
+ class AlwaysSample
22
+ ##
23
+ # Implements the sampler contract. Checks to see whether a sample
24
+ # should be taken at this time.
25
+ #
26
+ # @return [boolean] Whether to sample at this time.
27
+ #
28
+ def call _opts = {}
29
+ true
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,55 @@
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
+ module Trace
17
+ module Samplers
18
+ ##
19
+ # The MaxQPS sampler delays a minimum amount of time between each sample,
20
+ # enforcing a maximum QPS across traces that use this sampler.
21
+ #
22
+ class MaxQPS
23
+ ##
24
+ # Create a sampler for the given QPS.
25
+ #
26
+ # @param [Number] qps Samples per second. Default is 0.1.
27
+ #
28
+ def initialize qps = 0.1
29
+ @delay_secs = 1.0 / qps
30
+ @last_time = ::Time.now.to_f - @delay_secs
31
+ end
32
+
33
+ ##
34
+ # Implements the sampler contract. Checks to see whether a sample
35
+ # should be taken at this time.
36
+ #
37
+ # @return [boolean] Whether to sample at this time.
38
+ #
39
+ def call _opts = {}
40
+ time = ::Time.now.to_f
41
+ delays = (time - @last_time) / @delay_secs
42
+ if delays >= 2.0
43
+ @last_time = time - @delay_secs
44
+ true
45
+ elsif delays >= 1.0
46
+ @last_time += @delay_secs
47
+ true
48
+ else
49
+ false
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,34 @@
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
+ module Trace
17
+ module Samplers
18
+ ##
19
+ # The NeverSample sampler always returns false.
20
+ #
21
+ class NeverSample
22
+ ##
23
+ # Implements the sampler contract. Checks to see whether a sample
24
+ # should be taken at this time.
25
+ #
26
+ # @return [boolean] Whether to sample at this time.
27
+ #
28
+ def call _opts = {}
29
+ false
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,69 @@
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
+ module Trace
17
+ module Samplers
18
+ ##
19
+ # The Probability sampler uses a random number generator against a
20
+ # configured rate to determine whether or not to sample.
21
+ #
22
+ class Probability
23
+ ##
24
+ # The default sampling probability.
25
+ #
26
+ DEFAULT_RATE = 0.1
27
+
28
+ ##
29
+ # Create a sampler for the given probability.
30
+ #
31
+ # @param [Number] rate Probability that we will sample. This value must
32
+ # be between 0 and 1.
33
+ # @param [#rand] rng The random number generator to use. Default is a
34
+ # new Random instance.
35
+ #
36
+ def initialize rate, rng: nil
37
+ if rate > 1 || rate < 0
38
+ raise ArgumentError, "Invalid rate - must be between 0 and 1."
39
+ end
40
+ @rate = rate
41
+ @rng = rng || Random.new
42
+ end
43
+
44
+ ##
45
+ # Implements the sampler contract. Checks to see whether a sample
46
+ # should be taken at this time.
47
+ #
48
+ # @param [Hash] opts The options to sample with.
49
+ # @option opts [SpanContext] :span_context If provided, the span context
50
+ # will be used to generate a deterministic value in place of the
51
+ # pseudo-random number generator. #
52
+ # @return [boolean] Whether to sample at this time.
53
+ #
54
+ def call opts = {}
55
+ span_context = opts[:span_context]
56
+ return true if span_context && span_context.sampled?
57
+ value =
58
+ if span_context
59
+ (span_context.trace_id.to_i(16) % 0x10000000000000000).to_f /
60
+ 0x10000000000000000
61
+ else
62
+ @rng.rand
63
+ end
64
+ value <= @rate
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,196 @@
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
+ module Trace
17
+ ##
18
+ # Span represents a span in a trace record. Spans are contained in
19
+ # a trace and arranged in a forest. That is, each span may be a root span
20
+ # or have a parent span, and may have zero or more children.
21
+ #
22
+ class Span
23
+ ##
24
+ # A unique identifier for a trace. All spans from the same trace share
25
+ # the same `trace_id`. The ID is a 16-byte value represented as a
26
+ # hexadecimal string.
27
+ #
28
+ # @return [String]
29
+ #
30
+ attr_reader :trace_id
31
+
32
+ ##
33
+ # A unique identifier for a span within a trace, assigned when the span
34
+ # is created. The ID is an 8-byte value represented as a hexadecimal
35
+ # string.
36
+ #
37
+ # @return [String]
38
+ #
39
+ attr_reader :span_id
40
+
41
+ ##
42
+ # The `span_id` of this span's parent span. If this is a root span, then
43
+ # this field must be empty. The ID is an 8-byte value represented as a
44
+ # hexadecimal string.
45
+ #
46
+ # @return [String]
47
+ #
48
+ attr_reader :parent_span_id
49
+
50
+ ##
51
+ # The name of this span.
52
+ #
53
+ # @return [TruncatableString]
54
+ #
55
+ attr_reader :name
56
+
57
+ ##
58
+ # The starting timestamp of this span in UTC.
59
+ #
60
+ # @return [Time]
61
+ #
62
+ attr_reader :start_time
63
+
64
+ ##
65
+ # The ending timestamp of this span in UTC.
66
+ #
67
+ # @return [Time]
68
+ #
69
+ attr_reader :end_time
70
+
71
+ ##
72
+ # The properties of this span.
73
+ #
74
+ # @return [Hash<String, (TruncatableString, Integer, Boolean)>]
75
+ #
76
+ attr_reader :attributes
77
+
78
+ ##
79
+ # The number of attributes that were discarded. Attributes can be
80
+ # discarded because their keys are too long or because there are too
81
+ # many attributes. If this value is 0, then no attributes were dropped.
82
+ attr_reader :dropped_attributes_count
83
+
84
+ ##
85
+ # A stack trace captured at the start of the span.
86
+ #
87
+ # @return [Array<Thread::Backtrace::Location>]
88
+ #
89
+ attr_reader :stack_trace
90
+
91
+ ##
92
+ # The number of stack frames that were dropped because there were too many
93
+ # stack frames. If this value is 0, then no stack frames were dropped.
94
+ #
95
+ # @return [Integer]
96
+ #
97
+ attr_reader :dropped_frames_count
98
+
99
+ ##
100
+ # The included time events.
101
+ #
102
+ # @return [Array<TimeEvent>]
103
+ #
104
+ attr_reader :time_events
105
+
106
+ ##
107
+ # The number of dropped annotations in all the included time events.
108
+ # If the value is 0, then no annotations were dropped.
109
+ #
110
+ # @return [Integer]
111
+ #
112
+ attr_reader :dropped_annotations_count
113
+
114
+ ##
115
+ # The number of dropped message events in all the included time events.
116
+ # If the value is 0, then no message events were dropped.
117
+ #
118
+ # @return [Integer]
119
+ #
120
+ attr_reader :dropped_message_events_count
121
+
122
+ ##
123
+ # The included links.
124
+ #
125
+ # @return [Array<Link>]
126
+ #
127
+ attr_reader :links
128
+
129
+ ##
130
+ # The number of dropped links after the maximum size was enforced
131
+ # If the value is 0, then no links were dropped.
132
+ #
133
+ # @return [Integer]
134
+ #
135
+ attr_reader :dropped_links_count
136
+
137
+ ##
138
+ # An optional final status for this span.
139
+ #
140
+ # @return [Status, nil]
141
+ attr_reader :status
142
+
143
+ ##
144
+ # A highly recommended but not required flag that identifies when a trace
145
+ # crosses a process boundary. True when the parent_span belongs to the
146
+ # same process as the current span.
147
+ #
148
+ # @return [Boolean]
149
+ #
150
+ attr_reader :same_process_as_parent_span
151
+
152
+ ##
153
+ # An optional number of child spans that were generated while this span
154
+ # was active. If set, allows an implementation to detect missing child
155
+ # spans.
156
+ #
157
+ # @return [Integer, nil]
158
+ #
159
+ attr_reader :child_span_count
160
+
161
+ ##
162
+ # Create an empty Span object.
163
+ #
164
+ # @private
165
+ #
166
+ def initialize trace_id, span_id, name, start_time, end_time,
167
+ parent_span_id: nil, attributes: {},
168
+ dropped_attributes_count: 0, stack_trace: [],
169
+ dropped_frames_count: 0, time_events: [],
170
+ dropped_annotations_count: 0,
171
+ dropped_message_events_count: 0, links: [],
172
+ dropped_links_count: 0, status: nil,
173
+ same_process_as_parent_span: true,
174
+ child_span_count: nil
175
+ @name = name
176
+ @trace_id = trace_id
177
+ @span_id = span_id
178
+ @parent_span_id = parent_span_id
179
+ @start_time = start_time
180
+ @end_time = end_time
181
+ @attributes = attributes
182
+ @dropped_attributes_count = dropped_attributes_count
183
+ @stack_trace = stack_trace
184
+ @dropped_frames_count = dropped_frames_count
185
+ @time_events = time_events
186
+ @dropped_annotations_count = dropped_annotations_count
187
+ @dropped_message_events_count = dropped_message_events_count
188
+ @links = links
189
+ @dropped_links_count = dropped_links_count
190
+ @status = status
191
+ @same_process_as_parent_span = same_process_as_parent_span
192
+ @child_span_count = child_span_count
193
+ end
194
+ end
195
+ end
196
+ end