This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 326
/
measure.go
132 lines (112 loc) · 3.11 KB
/
measure.go
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
// 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.
//
package stats
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"go.opencensus.io/stats/internal"
)
// Measure represents a type of metric to be tracked and recorded.
// For example, latency, request Mb/s, and response Mb/s are measures
// to collect from a server.
//
// Each measure needs to be registered before being used.
// Measure constructors such as Int64 and
// Float64 automatically registers the measure
// by the given name.
// Each registered measure needs to be unique by name.
// Measures also have a description and a unit.
type Measure interface {
Name() string
Description() string
Unit() string
subscribe()
subscribed() bool
}
type measure struct {
subs int32 // access atomically
name string
description string
unit string
}
func (m *measure) subscribe() {
atomic.StoreInt32(&m.subs, 1)
}
func (m *measure) subscribed() bool {
return atomic.LoadInt32(&m.subs) == 1
}
// Name returns the name of the measure.
func (m *measure) Name() string {
return m.name
}
// Description returns the description of the measure.
func (m *measure) Description() string {
return m.description
}
// Unit returns the unit of the measure.
func (m *measure) Unit() string {
return m.unit
}
var (
mu sync.RWMutex
measures = make(map[string]Measure)
)
var (
errDuplicate = errors.New("duplicate measure name")
errMeasureNameTooLong = fmt.Errorf("measure name cannot be longer than %v", internal.MaxNameLength)
)
// FindMeasure finds the Measure instance, if any, associated with the given name.
func FindMeasure(name string) Measure {
mu.RLock()
m := measures[name]
mu.RUnlock()
return m
}
func register(m Measure) (Measure, error) {
key := m.Name()
mu.Lock()
defer mu.Unlock()
if stored, ok := measures[key]; ok {
return stored, errDuplicate
}
measures[key] = m
return m, nil
}
// Measurement is the numeric value measured when recording stats. Each measure
// provides methods to create measurements of their kind. For example, Int64Measure
// provides M to convert an int64 into a measurement.
type Measurement struct {
v float64
m Measure
}
// Value returns the value of the Measurement as a float64.
func (m Measurement) Value() float64 {
return m.v
}
// Measure returns the Measure from which this Measurement was created.
func (m Measurement) Measure() Measure {
return m.m
}
func checkName(name string) error {
if len(name) > internal.MaxNameLength {
return errMeasureNameTooLong
}
if !internal.IsPrintable(name) {
return errors.New("measure name needs to be an ASCII string")
}
return nil
}