opencensus 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
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,32 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module OpenCensus
5
+ module Stats
6
+ module AggregationData
7
+ # # LastValue
8
+ #
9
+ # Represents the last recorded value.
10
+ class LastValue
11
+ # @return [Integer,Float] Last recorded value.
12
+ attr_reader :value
13
+
14
+ # @return [Time] The latest time at new data point was recorded
15
+ attr_reader :time
16
+
17
+ # rubocop:disable Lint/UnusedMethodArgument
18
+
19
+ # Set last value
20
+ # @param [Integer,Float] value
21
+ # @param [Time] time Time of data point was recorded
22
+ # @param [Hash<String,String>] attachments Attachments are not in use
23
+ def add value, time, attachments: nil
24
+ @time = time
25
+ @value = value
26
+ end
27
+
28
+ # rubocop:enable Lint/UnusedMethodArgument
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module OpenCensus
5
+ module Stats
6
+ module AggregationData
7
+ # Sum
8
+ #
9
+ # Accumulate measurement values.
10
+ class Sum
11
+ # @return [Integer,Float] The current sum value.
12
+ attr_reader :value
13
+
14
+ # @return [Time] The latest time at new data point was recorded
15
+ attr_reader :time
16
+
17
+ # @private
18
+ def initialize
19
+ @value = 0
20
+ end
21
+
22
+ # rubocop:disable Lint/UnusedMethodArgument
23
+
24
+ # Add value
25
+ # @param [Integer,Float] value
26
+ # @param [Time] time Time of data point was recorded
27
+ # @param [Hash<String,String>] attachments Attachments are not in use.
28
+ def add value, time, attachments: nil
29
+ @time = time
30
+ @value += value
31
+ end
32
+
33
+ # rubocop:enable Lint/UnusedMethodArgument
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2017 OpenCensus Authors
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ require "opencensus/config"
19
+ require "opencensus/stats/exporters"
20
+
21
+ module OpenCensus
22
+ module Stats
23
+ # Schema of the Trace configuration. See Stats#configure for more info.
24
+ @config = Common::Config.new do |config|
25
+ exporter_logger = ::Logger.new STDOUT, ::Logger::INFO
26
+ default_exporter = Exporters::Logger.new exporter_logger
27
+
28
+ config.add_option! :exporter, default_exporter do |value|
29
+ value.respond_to? :export
30
+ end
31
+ end
32
+
33
+ # Expose the stats config as a subconfig under the main config.
34
+ OpenCensus.configure do |config|
35
+ config.add_alias! :stats, config: @config
36
+ end
37
+
38
+ class << self
39
+ ##
40
+ # Configure OpenCensus Stats. These configuration fields include
41
+ # parameters governing aggregation, exporting.
42
+ #
43
+ # This configuration is also available as the `stats` subconfig under the
44
+ # main configuration `OpenCensus.configure`. If the OpenCensus Railtie is
45
+ # installed in a Rails application, the configuration object is also
46
+ # exposed as `config.opencensus.stats`.
47
+ #
48
+ # Generally, you should configure this once at process initialization,
49
+ # but it can be modified at any time.
50
+ #
51
+ # Supported fields are:
52
+ #
53
+ # * `exporter` The exporter to use. Must be an exporter, an object with
54
+ # an export method that takes an array of ViewData objects. See
55
+ # {OpenCensus::Stats::Exporters}. The initial value is a
56
+ # {OpenCensus::Stats::Exporters::Logger} that logs to STDOUT.
57
+ #
58
+ # @example:
59
+ #
60
+ # OpenCensus::Stats.configure do |config|
61
+ # config.exporter = OpenCensus::Stats::Exporters::Logger.new
62
+ # end
63
+ #
64
+ def configure
65
+ if block_given?
66
+ yield @config
67
+ else
68
+ @config
69
+ end
70
+ end
71
+
72
+ ##
73
+ # Get the current configuration
74
+ # @private
75
+ #
76
+ attr_reader :config
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module OpenCensus
5
+ module Stats
6
+ ##
7
+ # Exemplars are example points that may be used to annotate aggregated
8
+ # Distribution values. They are metadata that gives information about a
9
+ # particular value added to a Distribution bucket.
10
+ #
11
+ class Exemplar
12
+ # Value of the exemplar point. It determines which bucket the exemplar
13
+ # belongs to
14
+ # @return [Integer,Float]
15
+ attr_reader :value
16
+
17
+ # The observation (sampling) time of the above value
18
+ # @return [Time]
19
+ attr_reader :time
20
+
21
+ # Contextual information about the example value
22
+ # @return [Hash<String,String>]
23
+ attr_reader :attachments
24
+
25
+ # Create instance of the exemplar
26
+ # @param [Integer,Float] value
27
+ # @param [Time] time
28
+ # @param [Hash<String,String>] attachments. Attachments are key-value
29
+ # pairs that describe the context in which the exemplar was recored.
30
+ def initialize value:, time:, attachments:
31
+ @value = value
32
+ @time = time
33
+
34
+ raise ArgumentError, "attachments can not be empty" if attachments.nil?
35
+
36
+ @attachments = attachments
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "opencensus/stats/exporters/logger"
4
+
5
+ module OpenCensus
6
+ module Stats
7
+ ##
8
+ # The Exporters module provides integrations for exporting collected stats
9
+ # to an external or local service. Exporter classes may be put inside
10
+ # this module, but are not required to be located here.
11
+ #
12
+ # An exporter is an object that must respond to the following method:
13
+ #
14
+ # def export(views_data)
15
+ #
16
+ # Where `views_data` is an array of {OpenCensus::Stats::ViewData} objects
17
+ # to export.
18
+ #
19
+ # The method return value is not defined.
20
+ #
21
+ # The exporter object may interpret the `export` message in whatever way it
22
+ # deems appropriate. For example, it may write the data to a log file, it
23
+ # may transmit it to a monitoring service, or it may do nothing at all.
24
+ # An exporter may also queue the request for later asynchronous processing,
25
+ # and indeed this is recommended if the export involves time consuming
26
+ # operations such as remote API calls.
27
+ #
28
+ module Exporters
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ require "logger"
5
+ require "json"
6
+
7
+ module OpenCensus
8
+ module Stats
9
+ module Exporters
10
+ ##
11
+ # The Logger exporter exports captured stats view data to a standard
12
+ # Ruby Logger interface.
13
+ #
14
+ class Logger
15
+ ##
16
+ # Create a new Logger exporter
17
+ #
18
+ # @param [#log] logger The logger to write to.
19
+ # @param [Integer] level The log level. This should be a log level
20
+ # defined by the Logger standard library. Default is
21
+ # `::Logger::INFO`.
22
+ #
23
+ def initialize logger, level: ::Logger::INFO
24
+ @logger = logger
25
+ @level = level
26
+ end
27
+
28
+ ##
29
+ # Export the captured stats to the configured logger.
30
+ #
31
+ # @param [Array<ViewData>] views_data The captured stats data.
32
+ #
33
+ def export views_data
34
+ stats_data = views_data.map { |vd| format_view_data(vd) }
35
+ @logger.log @level, stats_data.to_json
36
+ nil
37
+ end
38
+
39
+ private
40
+
41
+ def format_view_data view_data
42
+ {
43
+ view: format_view(view_data.view),
44
+ measure: format_measure(view_data.view.measure),
45
+ aggregation: format_aggregation(view_data)
46
+ }
47
+ end
48
+
49
+ def format_view view
50
+ {
51
+ name: view.name,
52
+ columns: view.columns,
53
+ description: view.description
54
+ }
55
+ end
56
+
57
+ def format_measure measure
58
+ {
59
+ name: measure.name,
60
+ unit: measure.unit,
61
+ type: measure.type,
62
+ description: measure.description
63
+ }
64
+ end
65
+
66
+ def format_aggregation view_data
67
+ {
68
+ type: view_data.view.aggregation.class.name.downcase,
69
+ start_time: view_data.start_time,
70
+ end_time: view_data.end_time,
71
+ data: view_data.data
72
+ }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,57 @@
1
+ # Copyright 2018 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
+
16
+ require "delegate"
17
+
18
+ module OpenCensus
19
+ module Stats
20
+ module Exporters
21
+ ##
22
+ # The Multi exporter multiplexes captured stats to a set of delegate
23
+ # exporters. It is useful if you need to export to more than one
24
+ # destination. You may also use it as a "null" exporter by providing
25
+ # no delegates.
26
+ #
27
+ # Multi delegates to an array of the exporter objects. You can manage
28
+ # the list of exporters using any method of Array. For example:
29
+ #
30
+ # multi = OpenCensus::Stats::Exporters::Multi.new
31
+ # multi.export(views_data) # Does nothing
32
+ # multi << OpenCensus::Stats::Exporters::Logger.new
33
+ # multi.export(views_data) # Exports to the logger
34
+ #
35
+ class Multi < SimpleDelegator
36
+ ##
37
+ # Create a new Multi exporter
38
+ #
39
+ # @param [Array<#export>] delegates An array of exporters
40
+ #
41
+ def initialize *delegates
42
+ super(delegates.flatten)
43
+ end
44
+
45
+ ##
46
+ # Pass the captured stats view data to the delegates.
47
+ #
48
+ # @param [Array<ViewData>] views_data The captured stats.
49
+ #
50
+ def export views_data
51
+ each { |delegate| delegate.export views_data }
52
+ nil
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ require "opencensus/stats/measurement"
5
+
6
+ module OpenCensus
7
+ module Stats
8
+ # Measure
9
+ #
10
+ # The definition of the Measurement. Describes the type of the individual
11
+ # values/measurements recorded by an application. It includes information
12
+ # such as the type of measurement, the units of measurement and descriptive
13
+ # names for the data. This provides th fundamental type used for recording
14
+ # data.
15
+ class Measure
16
+ # Describes the unit used for the Measure.
17
+ # Should follows the format described by
18
+ # http://unitsofmeasure.org/ucum.html
19
+ # Unit name for general counts
20
+ # @return [String]
21
+ UNIT_NONE = "1".freeze
22
+
23
+ # Unit name for bytes
24
+ # @return [String]
25
+ BYTE = "By".freeze
26
+
27
+ # Unit name for Kilobytes
28
+ # @return [String]
29
+ KBYTE = "kb".freeze
30
+
31
+ # Unit name for Seconds
32
+ # @return [String]
33
+ SEC = "s".freeze
34
+
35
+ # Unit name for Milli seconds
36
+ # @return [String]
37
+ MS = "ms".freeze
38
+
39
+ # Unit name for Micro seconds
40
+ # @return [String]
41
+ US = "us".freeze
42
+
43
+ # Unit name for Nano seconds
44
+ # @return [String]
45
+ NS = "ns".freeze
46
+
47
+ # Measure int64 type
48
+ # @return [String]
49
+ INT64_TYPE = "INT64".freeze
50
+
51
+ # Measure double type
52
+ # @return [String]
53
+ DOUBLE_TYPE = "DOUBLE".freeze
54
+
55
+ # @return [String]
56
+ attr_reader :name
57
+
58
+ # @return [String]
59
+ attr_reader :description
60
+
61
+ # Unit type of the measurement. i.e "kb", "ms" etc
62
+ # @return [String]
63
+ attr_reader :unit
64
+
65
+ # Data type of the measure.
66
+ # @return [String] Valid types are {INT64_TYPE}, {DOUBLE_TYPE}.
67
+ attr_reader :type
68
+
69
+ # @private
70
+ # Create instance of the measure.
71
+ def initialize name:, unit:, type:, description: nil
72
+ @name = name
73
+ @unit = unit
74
+ @type = type
75
+ @description = description
76
+ end
77
+
78
+ # Create new measurement
79
+ # @param [Integer, Float] value
80
+ # @param [Hash<String,String>] tags Tags to which the value is recorded
81
+ # @return [Measurement]
82
+ def create_measurement value:, tags:
83
+ Measurement.new measure: self, value: value, tags: tags
84
+ end
85
+
86
+ # Is int64 data type
87
+ # @return [Boolean]
88
+ def int64?
89
+ type == INT64_TYPE
90
+ end
91
+
92
+ # Is float data type
93
+ # @return [Boolean]
94
+ def double?
95
+ type == DOUBLE_TYPE
96
+ end
97
+ end
98
+ end
99
+ end