opencensus 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/README.md +2 -1
  4. data/lib/opencensus/stats.rb +143 -1
  5. data/lib/opencensus/stats/aggregation.rb +27 -0
  6. data/lib/opencensus/stats/aggregation/count.rb +18 -0
  7. data/lib/opencensus/stats/aggregation/distribution.rb +41 -0
  8. data/lib/opencensus/stats/aggregation/last_value.rb +18 -0
  9. data/lib/opencensus/stats/aggregation/sum.rb +18 -0
  10. data/lib/opencensus/stats/aggregation_data.rb +24 -0
  11. data/lib/opencensus/stats/aggregation_data/count.rb +37 -0
  12. data/lib/opencensus/stats/aggregation_data/distribution.rb +109 -0
  13. data/lib/opencensus/stats/aggregation_data/last_value.rb +32 -0
  14. data/lib/opencensus/stats/aggregation_data/sum.rb +37 -0
  15. data/lib/opencensus/stats/config.rb +79 -0
  16. data/lib/opencensus/stats/exemplar.rb +40 -0
  17. data/lib/opencensus/stats/exporters.rb +31 -0
  18. data/lib/opencensus/stats/exporters/logger.rb +77 -0
  19. data/lib/opencensus/stats/exporters/multi.rb +57 -0
  20. data/lib/opencensus/stats/measure.rb +99 -0
  21. data/lib/opencensus/stats/measure_registry.rb +72 -0
  22. data/lib/opencensus/stats/measurement.rb +35 -0
  23. data/lib/opencensus/stats/recorder.rb +100 -0
  24. data/lib/opencensus/stats/view.rb +57 -0
  25. data/lib/opencensus/stats/view_data.rb +68 -0
  26. data/lib/opencensus/tags.rb +39 -2
  27. data/lib/opencensus/tags/config.rb +74 -0
  28. data/lib/opencensus/tags/formatters.rb +15 -0
  29. data/lib/opencensus/tags/formatters/binary.rb +141 -0
  30. data/lib/opencensus/tags/tag_map.rb +137 -0
  31. data/lib/opencensus/trace/annotation.rb +1 -1
  32. data/lib/opencensus/trace/link.rb +1 -1
  33. data/lib/opencensus/trace/span.rb +1 -1
  34. data/lib/opencensus/trace/span_builder.rb +5 -4
  35. data/lib/opencensus/version.rb +1 -1
  36. metadata +32 -8
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "opencensus/tags/formatters/binary"
4
+
5
+ module OpenCensus
6
+ module Tags
7
+ ##
8
+ # The Formatters module contains several implementations of cross-service
9
+ # context propagation. Each formatter can serialize and deserialize a
10
+ # {OpenCensus::Tags::TagMap} instance.
11
+ #
12
+ module Formatters
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenCensus
4
+ module Tags
5
+ module Formatters
6
+ ##
7
+ # This formatter serializes and deserializes tags context according to
8
+ # the OpenCensus' BinaryEncoding specification. See
9
+ # [documentation](https://github.com/census-instrumentation/opencensus-specs/blob/master/encodings/BinaryEncoding.md).
10
+ #
11
+ # @example Serialize
12
+ #
13
+ # formatter = OpenCensus::Tags::Formatters::Binary.new
14
+ #
15
+ # tag_map = OpenCensus::Tags::TagMap.new({\"key1" => \"val1"})
16
+ # binary = formatter.serialize tag_map # "\x00\x00\x04key1\x04val1"
17
+ #
18
+ # @example Deserialize
19
+ #
20
+ # formatter = OpenCensus::Tags::Formatters::Binary.new
21
+ #
22
+ # binary = "\x00\x00\x04key1\x04val1"
23
+ # tag_map = formatter.deserialize binary
24
+ #
25
+ class Binary
26
+ # Binary format error for tags serialize/deserialize.
27
+ class BinaryFormatterError < StandardError; end
28
+
29
+ # @private
30
+ #
31
+ # Seralization version
32
+ VERSION_ID = 0
33
+
34
+ # @private
35
+ #
36
+ # Tag field id
37
+ TAG_FIELD_ID = 0
38
+
39
+ # @private
40
+ #
41
+ # Serialized tag context limit
42
+ TAG_MAP_SERIALIZED_SIZE_LIMIT = 8192
43
+
44
+ # Serialize TagMap object
45
+ #
46
+ # @param [TagMap] tags_context
47
+ #
48
+ def serialize tags_context
49
+ binary = [int_to_varint(VERSION_ID)]
50
+
51
+ tags_context.each do |key, value|
52
+ binary << int_to_varint(TAG_FIELD_ID)
53
+ binary << int_to_varint(key.length)
54
+ binary << key.encode(Encoding::UTF_8)
55
+ binary << int_to_varint(value ? value.length : 0)
56
+ binary << value.to_s.encode(Encoding::UTF_8)
57
+ end
58
+
59
+ binary = binary.join
60
+ binary.length > TAG_MAP_SERIALIZED_SIZE_LIMIT ? nil : binary
61
+ end
62
+
63
+ # Deserialize binary data into a TagMap object.
64
+ #
65
+ # @param [String] binary
66
+ # @return [TagMap]
67
+ # @raise [BinaryFormatterError] If deserialized version id not valid or
68
+ # tag key, value size in varint more then then unsigned int32.
69
+ #
70
+ def deserialize binary
71
+ return TagMap.new if binary.nil? || binary.empty?
72
+
73
+ io = StringIO.new binary
74
+ version_id = io.getc.unpack("C").first
75
+ unless version_id == VERSION_ID
76
+ raise BinaryFormatterError, "invalid version id"
77
+ end
78
+
79
+ tag_map = TagMap.new
80
+
81
+ loop do
82
+ break if io.eof?
83
+ tag_field_id = io.getc.unpack("C").first
84
+ break unless tag_field_id == TAG_FIELD_ID
85
+
86
+ key_length = varint_to_int io
87
+ key = io.gets key_length
88
+ value_length = varint_to_int io
89
+ value = io.gets value_length
90
+ tag_map[key] = value
91
+ end
92
+
93
+ io.close
94
+ tag_map
95
+ end
96
+
97
+ private
98
+
99
+ # Convert integer to Varint.
100
+ # @see https://developers.google.com/protocol-buffers/docs/encoding#varints
101
+ #
102
+ # @param [Integer] int_val
103
+ # @return [String]
104
+ def int_to_varint int_val
105
+ result = []
106
+ loop do
107
+ bits = int_val & 0x7F
108
+ int_val >>= 7
109
+ if int_val.zero?
110
+ result << bits
111
+ break
112
+ else
113
+ result << (0x80 | bits)
114
+ end
115
+ end
116
+ result.pack "C*"
117
+ end
118
+
119
+ # Convert Varint bytes format to integer
120
+ # @see https://developers.google.com/protocol-buffers/docs/encoding#varints
121
+ #
122
+ # @param [StringIO] io
123
+ # @return [Integer]
124
+ # @raise [BinaryFormatterError] If varint size more then unsigned int32
125
+ #
126
+ def varint_to_int io
127
+ int_val = 0
128
+ shift = 0
129
+
130
+ loop do
131
+ raise BinaryFormatterError, "varint too long" if shift >= 32
132
+ byte = io.getbyte
133
+ int_val |= (byte & 0x7F) << shift
134
+ shift += 7
135
+ return int_val if (byte & 0x80).zero?
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ require "forwardable"
5
+
6
+ module OpenCensus
7
+ module Tags
8
+ # # TagMap
9
+ #
10
+ # Collection of tag key and value.
11
+ # @example
12
+ #
13
+ # tag_map = OpenCensus::Tags::OpenCensus.new
14
+ #
15
+ # # Add or update
16
+ # tag_map["key1"] = "value1"
17
+ # tag_map["key2"] = "value2"
18
+ #
19
+ # # Get value
20
+ # tag_map["key1"] # value1
21
+ #
22
+ # # Delete
23
+ # tag_map.delete "key1"
24
+ #
25
+ # # Iterate
26
+ # tag_map.each do |key, value|
27
+ # p key
28
+ # p value
29
+ # end
30
+ #
31
+ # # Length
32
+ # tag_map.length # 1
33
+ #
34
+ # @example Create tag map from hash
35
+ #
36
+ # tag_map = OpenCensus::Tags::OpenCensus.new({ "key1" => "value1"})
37
+ #
38
+ class TagMap
39
+ extend Forwardable
40
+
41
+ # The maximum length for a tag key and tag value
42
+ MAX_LENGTH = 255
43
+
44
+ # Create a tag map. It is a map of tags from key to value.
45
+ # @param [Hash{String=>String}] tags Tags hash with string key and value.
46
+ #
47
+ def initialize tags = {}
48
+ @tags = {}
49
+
50
+ tags.each do |key, value|
51
+ self[key] = value
52
+ end
53
+ end
54
+
55
+ # Set tag key value
56
+ #
57
+ # @param [String] key Tag key
58
+ # @param [String] value Tag value
59
+ # @raise [InvalidTagError] If invalid tag key or value.
60
+ #
61
+ def []= key, value
62
+ validate_key! key
63
+ validate_value! value
64
+
65
+ @tags[key] = value
66
+ end
67
+
68
+ # Convert tag map to binary string format.
69
+ # @see [documentation](https://github.com/census-instrumentation/opencensus-specs/blob/master/encodings/BinaryEncoding.md#tag-context)
70
+ # @return [String] Binary string
71
+ #
72
+ def to_binary
73
+ Formatters::Binary.new.serialize self
74
+ end
75
+
76
+ # Create a tag map from the binary string.
77
+ # @param [String] data Binary string data
78
+ # @return [TagMap]
79
+ #
80
+ def self.from_binary data
81
+ Formatters::Binary.new.deserialize data
82
+ end
83
+
84
+ # @!method []
85
+ # @see Hash#[]
86
+ # @!method each
87
+ # @see Hash#each
88
+ # @!method delete
89
+ # @see Hash#delete
90
+ # @!method delete_if
91
+ # @see Hash#delete_if
92
+ # @!method length
93
+ # @see Hash#length
94
+ # @!method to_h
95
+ # @see Hash#to_h
96
+ # @!method empty?
97
+ # @see Hash#empty?
98
+ def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
99
+ :to_h, :empty?
100
+
101
+ # Invalid tag error.
102
+ class InvalidTagError < StandardError; end
103
+
104
+ private
105
+
106
+ # Validate tag key.
107
+ # @param [String] key
108
+ # @raise [InvalidTagError] If key is empty, length grater then 255
109
+ # characters or contains non printable characters
110
+ #
111
+ def validate_key! key
112
+ if key.empty? || key.length > MAX_LENGTH || !printable_str?(key)
113
+ raise InvalidTagError, "Invalid tag key #{key}"
114
+ end
115
+ end
116
+
117
+ # Validate tag value.
118
+ # @param [String] value
119
+ # @raise [InvalidTagError] If value length grater then 255 characters
120
+ # or contains non printable characters
121
+ #
122
+ def validate_value! value
123
+ if (value && value.length > MAX_LENGTH) || !printable_str?(value)
124
+ raise InvalidTagError, "Invalid tag value #{value}"
125
+ end
126
+ end
127
+
128
+ # Check string is printable.
129
+ # @param [String] str
130
+ # @return [Boolean]
131
+ #
132
+ def printable_str? str
133
+ str.bytes.none? { |b| b < 32 || b > 126 }
134
+ end
135
+ end
136
+ end
137
+ end
@@ -31,7 +31,7 @@ module OpenCensus
31
31
  ##
32
32
  # A set of attributes on the annotation.
33
33
  #
34
- # @return [Hash<String, (TruncatableString, Integer, Boolean)>]
34
+ # @return [Hash<String, (TruncatableString, Integer, Float, Boolean)>]
35
35
  #
36
36
  attr_reader :attributes
37
37
 
@@ -69,7 +69,7 @@ module OpenCensus
69
69
  ##
70
70
  # A set of attributes on the link.
71
71
  #
72
- # @return [Hash<String, (TruncatableString, Integer, Boolean)>]
72
+ # @return [Hash<String, (TruncatableString, Integer, Float, Boolean)>]
73
73
  #
74
74
  attr_reader :attributes
75
75
 
@@ -101,7 +101,7 @@ module OpenCensus
101
101
  ##
102
102
  # The properties of this span.
103
103
  #
104
- # @return [Hash<String, (TruncatableString, Integer, Boolean)>]
104
+ # @return [Hash<String, (TruncatableString, Integer, Float, Boolean)>]
105
105
  #
106
106
  attr_reader :attributes
107
107
 
@@ -208,11 +208,11 @@ module OpenCensus
208
208
  # the ID of the logged-in user, or any other relevant information.
209
209
  #
210
210
  # Keys must be strings.
211
- # Values may be String, TruncatableString, Integer, or Boolean.
211
+ # Values may be String, TruncatableString, Integer, Float or Boolean.
212
212
  # The valid integer range is 64-bit signed `(-2^63..2^63-1)`.
213
213
  #
214
214
  # @param [String, Symbol] key
215
- # @param [String, TruncatableString, Integer, boolean] value
215
+ # @param [String, TruncatableString, Integer, Float, Boolean] value
216
216
  #
217
217
  def put_attribute key, value
218
218
  @attributes[key.to_s] = value
@@ -282,7 +282,8 @@ module OpenCensus
282
282
  # {OpenCensus::Trace::SpanBuilder::CHILD_LINKED_SPAN},
283
283
  # {OpenCensus::Trace::SpanBuilder::PARENT_LINKED_SPAN}, and
284
284
  # {OpenCensus::Trace::SpanBuilder::TYPE_UNSPECIFIED}.
285
- # @param [String] attributes Key-value pairs providing additional
285
+ # @param [Hash<String, (TruncatableString, Integer, Float, Boolean)>]
286
+ # attributes Key-value pairs providing additional
286
287
  # properties of the link. Keys must be strings, and values are
287
288
  # restricted to the same types as attributes (see #put_attribute).
288
289
  #
@@ -514,7 +515,7 @@ module OpenCensus
514
515
  else
515
516
  truncatable_string v.to_s
516
517
  end
517
- when true, false, TruncatableString
518
+ when true, false, TruncatableString, Float
518
519
  v
519
520
  else
520
521
  truncatable_string v.to_s
@@ -15,5 +15,5 @@
15
15
 
16
16
  module OpenCensus
17
17
  ## Current OpenCensus version
18
- VERSION = "0.4.0".freeze
18
+ VERSION = "0.5.0".freeze
19
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opencensus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-10-23 00:00:00.000000000 Z
12
+ date: 2019-10-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '1.16'
20
+ version: '1.17'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '1.16'
27
+ version: '1.17'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -158,7 +158,32 @@ files:
158
158
  - lib/opencensus/config.rb
159
159
  - lib/opencensus/context.rb
160
160
  - lib/opencensus/stats.rb
161
+ - lib/opencensus/stats/aggregation.rb
162
+ - lib/opencensus/stats/aggregation/count.rb
163
+ - lib/opencensus/stats/aggregation/distribution.rb
164
+ - lib/opencensus/stats/aggregation/last_value.rb
165
+ - lib/opencensus/stats/aggregation/sum.rb
166
+ - lib/opencensus/stats/aggregation_data.rb
167
+ - lib/opencensus/stats/aggregation_data/count.rb
168
+ - lib/opencensus/stats/aggregation_data/distribution.rb
169
+ - lib/opencensus/stats/aggregation_data/last_value.rb
170
+ - lib/opencensus/stats/aggregation_data/sum.rb
171
+ - lib/opencensus/stats/config.rb
172
+ - lib/opencensus/stats/exemplar.rb
173
+ - lib/opencensus/stats/exporters.rb
174
+ - lib/opencensus/stats/exporters/logger.rb
175
+ - lib/opencensus/stats/exporters/multi.rb
176
+ - lib/opencensus/stats/measure.rb
177
+ - lib/opencensus/stats/measure_registry.rb
178
+ - lib/opencensus/stats/measurement.rb
179
+ - lib/opencensus/stats/recorder.rb
180
+ - lib/opencensus/stats/view.rb
181
+ - lib/opencensus/stats/view_data.rb
161
182
  - lib/opencensus/tags.rb
183
+ - lib/opencensus/tags/config.rb
184
+ - lib/opencensus/tags/formatters.rb
185
+ - lib/opencensus/tags/formatters/binary.rb
186
+ - lib/opencensus/tags/tag_map.rb
162
187
  - lib/opencensus/trace.rb
163
188
  - lib/opencensus/trace/annotation.rb
164
189
  - lib/opencensus/trace/config.rb
@@ -207,8 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
232
  - !ruby/object:Gem::Version
208
233
  version: '0'
209
234
  requirements: []
210
- rubyforge_project:
211
- rubygems_version: 2.7.6
235
+ rubygems_version: 3.0.3
212
236
  signing_key:
213
237
  specification_version: 4
214
238
  summary: A stats collection and distributed tracing framework