This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstats.rb
177 lines (159 loc) · 5.71 KB
/
stats.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright 2017 OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "opencensus/stats/config"
require "opencensus/stats/recorder"
require "opencensus/stats/view"
require "opencensus/stats/aggregation"
require "opencensus/stats/measure_registry"
require "opencensus/stats/exporters"
module OpenCensus
##
# The Stats module contains support for OpenCensus stats collection.
#
# OpenCensus allows users to create typed measures, record measurements,
# aggregate the collected data, and export the aggregated data.
#
#
module Stats
##
# Internal key for storing the current stats recorder in the thread local
# context.
#
# @private
RECORDER_CONTEXT_KEY = :__recorder_context__
class << self
##
# Sets the current thread-local Recorder, which governs the behavior
# of the recorder creation methods of OpenCensus::Stats::Recorder.
#
# @param [Recorder] context
def recorder_context= context
OpenCensus::Context.set RECORDER_CONTEXT_KEY, context
end
##
# Unsets the current thread-local SpanContext, disabling stats recorder
# creation methods of OpenCensus::Stats::Recorder
def unset_recorder_context
OpenCensus::Context.unset RECORDER_CONTEXT_KEY
end
# Get the current thread-local stats recorder context/
# Returns `nil` if there is no current SpanContext.
#
# @return [Recorder, nil]
def recorder_context
OpenCensus::Context.get RECORDER_CONTEXT_KEY
end
# Get recorder from the stats context. If stats context nil then create
# new recorder and set into stats context.
# @return [Recorder]
def ensure_recorder
self.recorder_context ||= Recorder.new
end
# Create and register int64 type measure into measure registry.
#
# @param [String] name Name of the measure.
# @param [String] unit Unit of the measure. i.e "kb", "s", "ms"
# @param [String] description Detail description
# @return [Measure]
def create_measure_int name:, unit:, description: nil
MeasureRegistry.register(
name: name,
unit: unit,
type: Measure::INT64_TYPE,
description: description
)
end
# Create and register double type measure into measure registry.
#
# @param [String] name Name of the measure.
# @param [String] unit Unit of the measure. i.e "kb", "s", "ms"
# @param [String] description Detail description
# @return [Measure]
def create_measure_double name:, unit:, description: nil
MeasureRegistry.register(
name: name,
unit: unit,
type: Measure::DOUBLE_TYPE,
description: description
)
end
# Get list of registered measures
# @return [Array<Measure>]
def registered_measures
MeasureRegistry.measures
end
# Create measurement value for registered measure.
#
# @param [String] name Name of the registered measure
# @param [Integer, Float] value Value of the measurement
# @param [Tags::TagMap] tags A map of tags to which the value is recorded.
# Tags could either be explicitly passed, or implicitly read from
# current tags context.
# @raise [ArgumentError]
# if given measure is not register.
# if given tags are nil and tags global context is nil.
def create_measurement name:, value:, tags: nil
measure = MeasureRegistry.get name
raise ArgumentError, "#{name} measure is not registered" unless measure
tags ||= OpenCensus::Tags.tag_map_context
raise ArgumentError, "pass tags or set tags global context" unless tags
measure.create_measurement value: value, tags: tags
end
# Create and register a view to current stats recorder context.
#
# @param [String] name
# @param [Measure] measure
# @param [Aggregation] aggregation
# @param [Array<String>] columns
# @param [String] description
def create_and_register_view \
name:,
measure:,
aggregation:,
columns: nil,
description: nil
view = View.new(
name: name,
measure: measure,
aggregation: aggregation,
description: description,
columns: columns
)
ensure_recorder.register_view view
end
# Create aggregation defination instance with type sum.
# @return [Aggregation]
def create_sum_aggregation
Aggregation::Sum.new
end
# Create aggregation defination instance with type count.
# @return [Aggregation]
def create_count_aggregation
Aggregation::Count.new
end
# Create aggregation defination instance with type distribution.
# @param [Array<Integer>,Array<Float>] buckets Value boundries for
# distribution.
# @return [Aggregation]
def create_distribution_aggregation buckets
Aggregation::Distribution.new buckets
end
# Create aggregation defination instance with type last value.
# @return [Aggregation]
def create_last_value_aggregation
Aggregation::LastValue.new
end
end
end
end