@@ -24,187 +24,217 @@ import (
2424	"google.golang.org/grpc/stats" 
2525)
2626
27- // DefaultMetrics are the default metrics registered through global instruments  
28- // registry. This is written to at initialization time only, and is read 
29- // only  after initialization. 
27+ // DefaultMetrics are the default metrics registered through global metrics  
28+ // registry. This is written to at initialization time only, and is read only  
29+ // after initialization. 
3030var  DefaultMetrics  =  stats .NewMetrics ()
3131
3232// MetricDescriptor is the data for a registered metric. 
3333type  MetricDescriptor  struct  {
34- 	// Name is the name of this metric. 
34+ 	// Name is the name of this metric. This name must be unique across whole 
35+ 	// binary (including any per call metrics). See 
36+ 	// https://github.com/grpc/proposal/blob/master/A79-non-per-call-metrics-architecture.md#metric-instrument-naming-conventions 
37+ 	// for metric naming conventions. 
3538	Name  stats.Metric 
3639	// Description is the description of this metric. 
3740	Description  string 
38- 	// Unit is the unit of this metric. 
41+ 	// Unit is the unit of this metric (e.g. entries, milliseconds) . 
3942	Unit  string 
40- 	// Labels are the required label keys for this metric. 
43+ 	// Labels are the required label keys for this metric. These are intended to 
44+ 	// metrics emitted from a stats handler. 
4145	Labels  []string 
42- 	// OptionalLabels are the optional label keys for this 
43- 	// metric. 
46+ 	// OptionalLabels are the optional label keys for this metric. These are 
47+ 	// intended to attached to metrics emitted from a stats handler if 
48+ 	// configured. 
4449	OptionalLabels  []string 
4550	// Default is whether this metric is on by default. 
4651	Default  bool 
52+ 	// Type is the type of metric. This is set by the metric registry, and not 
53+ 	// intended to be set by a component registering a metric. 
54+ 	Type  MetricType 
4755}
4856
49- // Int64CountHandle is a typed handle for a float count instrument . This handle 
50- // is passed at the recording point in order to know which instrument  to record 
57+ // Int64CountHandle is a typed handle for a float count metric . This handle 
58+ // is passed at the recording point in order to know which metric  to record 
5159// on. 
5260type  Int64CountHandle  struct  {
5361	MetricDescriptor  * MetricDescriptor 
5462}
5563
56- // RecordInt64Count records the int64 count value on the metrics recorder 
57- // provided. 
58- func  (h  Int64CountHandle ) RecordInt64Count (recorder  MetricsRecorder , incr  int64 , labels  ... string ) {
64+ // Record records the int64 count value on the metrics recorder provided. 
65+ func  (h  Int64CountHandle ) Record (recorder  MetricsRecorder , incr  int64 , labels  ... string ) {
5966	recorder .RecordIntCount (h , incr , labels ... )
6067}
6168
62- // Float64CountHandle is a typed handle for a float count instrument. This handle 
63- // is passed at the recording point in order to know which instrument to record 
64- // on. 
69+ // Float64CountHandle is a typed handle for a float count metric. This handle is 
70+ // passed at the recording point in order to know which metric to record on. 
6571type  Float64CountHandle  struct  {
6672	MetricDescriptor  * MetricDescriptor 
6773}
6874
69- // RecordFloat64Count records the float64 count value on the metrics recorder 
70- // provided. 
71- func  (h  Float64CountHandle ) RecordFloat64Count (recorder  MetricsRecorder , incr  float64 , labels  ... string ) {
75+ // Record records the float64 count value on the metrics recorder provided. 
76+ func  (h  Float64CountHandle ) Record (recorder  MetricsRecorder , incr  float64 , labels  ... string ) {
7277	recorder .RecordFloatCount (h , incr , labels ... )
7378}
7479
75- // Int64HistoHandle is a typed handle for an int histogram instrument. This 
76- // handle is passed at the recording point in order to know which instrument to 
77- // record on. 
80+ // Int64HistoHandle is a typed handle for an int histogram metric. This handle 
81+ // is passed at the recording point in order to know which metric to record on. 
7882type  Int64HistoHandle  struct  {
7983	MetricDescriptor  * MetricDescriptor 
8084}
8185
82- // RecordInt64Histo records the int64 histo value on the metrics recorder 
83- // provided. 
84- func  (h  Int64HistoHandle ) RecordInt64Histo (recorder  MetricsRecorder , incr  int64 , labels  ... string ) {
86+ // Record records the int64 histo value on the metrics recorder provided. 
87+ func  (h  Int64HistoHandle ) Record (recorder  MetricsRecorder , incr  int64 , labels  ... string ) {
8588	recorder .RecordIntHisto (h , incr , labels ... )
8689}
8790
88- // Float64HistoHandle is a typed handle for a float histogram instrument . This 
89- // handle is passed at the recording point in order to know which instrument  to 
91+ // Float64HistoHandle is a typed handle for a float histogram metric . This 
92+ // handle is passed at the recording point in order to know which metric  to 
9093// record on. 
9194type  Float64HistoHandle  struct  {
9295	MetricDescriptor  * MetricDescriptor 
9396}
9497
95- // RecordFloat64Histo records the float64 histo value on the metrics recorder 
96- // provided. 
97- func  (h  Float64HistoHandle ) RecordFloat64Histo (recorder  MetricsRecorder , incr  float64 , labels  ... string ) {
98+ // Record records the float64 histo value on the metrics recorder provided. 
99+ func  (h  Float64HistoHandle ) Record (recorder  MetricsRecorder , incr  float64 , labels  ... string ) {
98100	recorder .RecordFloatHisto (h , incr , labels ... )
99101}
100102
101- // Int64GaugeHandle is a typed handle for an int gauge instrument. This handle 
102- // is passed at the recording point in order to know which instrument to record 
103- // on. 
103+ // Int64GaugeHandle is a typed handle for an int gauge metric. This handle is 
104+ // passed at the recording point in order to know which metric to record on. 
104105type  Int64GaugeHandle  struct  {
105106	MetricDescriptor  * MetricDescriptor 
106107}
107108
108- // RecordInt64Histo records the int64 histo value on the metrics recorder 
109- // provided. 
110- func  (h  Int64GaugeHandle ) RecordInt64Gauge (recorder  MetricsRecorder , incr  int64 , labels  ... string ) {
109+ // Record records the int64 histo value on the metrics recorder provided. 
110+ func  (h  Int64GaugeHandle ) Record (recorder  MetricsRecorder , incr  int64 , labels  ... string ) {
111111	recorder .RecordIntGauge (h , incr , labels ... )
112112}
113113
114- // registeredInsts  are the registered instrument  descriptor names. 
115- var  registeredInsts  =  make (map [stats.Metric ]bool )
114+ // registeredMetrics  are the registered metric  descriptor names. 
115+ var  registeredMetrics  =  make (map [stats.Metric ]bool )
116116
117- // MetricsRegistry is  all the registered metrics. 
117+ // MetricsRegistry are  all of  the registered metrics. 
118118// 
119119// This is written to only at init time, and read only after that. 
120120var  MetricsRegistry  =  make (map [stats.Metric ]* MetricDescriptor )
121121
122- func  registerInst (name  stats.Metric , def  bool ) {
123- 	if  registeredInsts [name ] {
124- 		log .Panicf ("instrument  %v already registered" , name )
122+ func  registerMetric (name  stats.Metric , def  bool ) {
123+ 	if  registeredMetrics [name ] {
124+ 		log .Panicf ("metric  %v already registered" , name )
125125	}
126- 	registeredInsts [name ] =  true 
126+ 	registeredMetrics [name ] =  true 
127127	if  def  {
128128		DefaultMetrics  =  DefaultMetrics .Add (name )
129129	}
130130}
131131
132- // RegisterInt64Count registers the instrument  description onto the global 
133- // registry.  It returns a typed handle to use to recording data. 
132+ // RegisterInt64Count registers the metric  description onto the global registry.  
133+ // It returns a typed handle to use to recording data. 
134134// 
135135// NOTE: this function must only be called during initialization time (i.e. in 
136- // an init() function), and is not thread-safe. If multiple instruments  are 
136+ // an init() function), and is not thread-safe. If multiple metrics  are 
137137// registered with the same name, this function will panic. 
138- func  RegisterInt64Count (descriptor  MetricDescriptor )  Int64CountHandle  {
139- 	registerInst (descriptor .Name , descriptor .Default )
138+ func  RegisterInt64Count (descriptor  MetricDescriptor ) Int64CountHandle  {
139+ 	registerMetric (descriptor .Name , descriptor .Default )
140+ 	descriptor .Type  =  MetricTypeIntCount 
140141	handle  :=  Int64CountHandle {
141142		MetricDescriptor : & descriptor ,
142143	}
143144	MetricsRegistry [descriptor .Name ] =  & descriptor 
144145	return  handle 
145146}
146147
147- // RegisterFloat64Count registers the instrument  description onto the global 
148+ // RegisterFloat64Count registers the metric  description onto the global 
148149// registry. It returns a typed handle to use to recording data. 
149150// 
150151// NOTE: this function must only be called during initialization time (i.e. in 
151- // an init() function), and is not thread-safe. If multiple instruments  are 
152+ // an init() function), and is not thread-safe. If multiple metrics  are 
152153// registered with the same name, this function will panic. 
153154func  RegisterFloat64Count (descriptor  MetricDescriptor ) Float64CountHandle  {
154- 	registerInst (descriptor .Name , descriptor .Default )
155+ 	registerMetric (descriptor .Name , descriptor .Default )
156+ 	descriptor .Type  =  MetricTypeFloatCount 
155157	handle  :=  Float64CountHandle {
156158		MetricDescriptor : & descriptor ,
157159	}
158160	MetricsRegistry [descriptor .Name ] =  & descriptor 
159161	return  handle 
160162}
161163
162- // RegisterInt64Histo registers the instrument  description onto the global 
163- // registry.  It returns a typed handle to use to recording data. 
164+ // RegisterInt64Histo registers the metric  description onto the global registry.  
165+ // It returns a typed handle to use to recording data. 
164166// 
165167// NOTE: this function must only be called during initialization time (i.e. in 
166- // an init() function), and is not thread-safe. If multiple instruments  are 
168+ // an init() function), and is not thread-safe. If multiple metrics  are 
167169// registered with the same name, this function will panic. 
168170func  RegisterInt64Histo (descriptor  MetricDescriptor ) Int64HistoHandle  {
169- 	registerInst (descriptor .Name , descriptor .Default )
171+ 	registerMetric (descriptor .Name , descriptor .Default )
172+ 	descriptor .Type  =  MetricTypeIntHisto 
170173	handle  :=  Int64HistoHandle {
171174		MetricDescriptor : & descriptor ,
172175	}
173176	MetricsRegistry [descriptor .Name ] =  & descriptor 
174177	return  handle 
175178}
176179
177- // RegisterFloat64Histo registers the instrument  description onto the global 
180+ // RegisterFloat64Histo registers the metric  description onto the global 
178181// registry. It returns a typed handle to use to recording data. 
179182// 
180183// NOTE: this function must only be called during initialization time (i.e. in 
181- // an init() function), and is not thread-safe. If multiple instruments  are 
184+ // an init() function), and is not thread-safe. If multiple metrics  are 
182185// registered with the same name, this function will panic. 
183186func  RegisterFloat64Histo (descriptor  MetricDescriptor ) Float64HistoHandle  {
184- 	registerInst (descriptor .Name , descriptor .Default )
187+ 	registerMetric (descriptor .Name , descriptor .Default )
188+ 	descriptor .Type  =  MetricTypeFloatHisto 
185189	handle  :=  Float64HistoHandle {
186190		MetricDescriptor : & descriptor ,
187191	}
188192	MetricsRegistry [descriptor .Name ] =  & descriptor 
189193	return  handle 
190194}
191195
192- // RegisterInt64Gauge registers the instrument  description onto the global 
193- // registry.  It returns a typed handle to use to recording data. 
196+ // RegisterInt64Gauge registers the metric  description onto the global registry.  
197+ // It returns a typed handle to use to recording data. 
194198// 
195199// NOTE: this function must only be called during initialization time (i.e. in 
196- // an init() function), and is not thread-safe. If multiple instruments  are 
200+ // an init() function), and is not thread-safe. If multiple metrics  are 
197201// registered with the same name, this function will panic. 
198202func  RegisterInt64Gauge (descriptor  MetricDescriptor ) Int64GaugeHandle  {
199- 	registerInst (descriptor .Name , descriptor .Default )
203+ 	registerMetric (descriptor .Name , descriptor .Default )
204+ 	descriptor .Type  =  MetricTypeIntGauge 
200205	handle  :=  Int64GaugeHandle {
201206		MetricDescriptor : & descriptor ,
202207	}
203208	MetricsRegistry [descriptor .Name ] =  & descriptor 
204209	return  handle 
205210}
206211
207- // Will take a list, write comments and rewrite tests/cleanup and then I think ready to send out... 
208- // How do I even test this really? 
209- // Internal only clear...I don't think it's worth it just for default set to do it in internal... 
212+ // MetricType is the type of metric. 
213+ type  MetricType  int 
214+ 
215+ const  (
216+ 	MetricTypeIntCount  MetricType  =  iota 
217+ 	MetricTypeFloatCount 
218+ 	MetricTypeIntHisto 
219+ 	MetricTypeFloatHisto 
220+ 	MetricTypeIntGauge 
221+ )
210222
223+ // clearMetricsRegistryForTesting clears the global data of the metrics 
224+ // registry. It returns a closure to be invoked that sets the metrics registry 
225+ // to its original state. Only called in testing functions. 
226+ func  clearMetricsRegistryForTesting () func () {
227+ 	oldDefaultMetrics  :=  DefaultMetrics 
228+ 	oldRegisteredMetrics  :=  registeredMetrics 
229+ 	oldMetricsRegistry  :=  MetricsRegistry 
230+ 
231+ 	DefaultMetrics  =  stats .NewMetrics ()
232+ 	registeredMetrics  =  make (map [stats.Metric ]bool )
233+ 	MetricsRegistry  =  make (map [stats.Metric ]* MetricDescriptor )
234+ 
235+ 	return  func () {
236+ 		DefaultMetrics  =  oldDefaultMetrics 
237+ 		registeredMetrics  =  oldRegisteredMetrics 
238+ 		MetricsRegistry  =  oldMetricsRegistry 
239+ 	}
240+ }
0 commit comments