File: common.go

package info (click to toggle)
golang-go.opencensus 0.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,404 kB
  • sloc: makefile: 86; sh: 6
file content (144 lines) | stat: -rw-r--r-- 3,844 bytes parent folder | download | duplicates (4)
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
// Copyright 2019, 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 metric

import (
	"sync"
	"time"

	"go.opencensus.io/internal/tagencoding"

	"go.opencensus.io/metric/metricdata"
)

// baseMetric is common representation for gauge and cumulative metrics.
//
// baseMetric maintains a value for each combination of label values passed to
// Set, Add, or Inc method.
//
// baseMetric should not be used directly, use metric specific type such as
// Float64Gauge or Int64Gauge.
type baseMetric struct {
	vals             sync.Map
	desc             metricdata.Descriptor
	start            time.Time
	keys             []metricdata.LabelKey
	constLabelValues []metricdata.LabelValue
	bmType           baseMetricType
}

type baseMetricType int

const (
	gaugeInt64 baseMetricType = iota
	gaugeFloat64
	derivedGaugeInt64
	derivedGaugeFloat64
	cumulativeInt64
	cumulativeFloat64
	derivedCumulativeInt64
	derivedCumulativeFloat64
)

type baseEntry interface {
	read(t time.Time) metricdata.Point
}

func (bm *baseMetric) startTime() *time.Time {
	switch bm.bmType {
	case cumulativeInt64, cumulativeFloat64, derivedCumulativeInt64, derivedCumulativeFloat64:
		return &bm.start
	default:
		// gauges don't have start time.
		return nil
	}
}

// Read returns the current values of the baseMetric as a metric for export.
func (bm *baseMetric) read() *metricdata.Metric {
	now := time.Now()
	startTime := bm.startTime()
	if startTime == nil {
		startTime = &now
	}
	m := &metricdata.Metric{
		Descriptor: bm.desc,
	}
	bm.vals.Range(func(k, v interface{}) bool {
		entry := v.(baseEntry)
		key := k.(string)
		labelVals := bm.decodeLabelVals(key)
		m.TimeSeries = append(m.TimeSeries, &metricdata.TimeSeries{
			StartTime:   *startTime,
			LabelValues: labelVals,
			Points: []metricdata.Point{
				entry.read(now),
			},
		})
		return true
	})
	return m
}

func (bm *baseMetric) encodeLabelVals(labelVals []metricdata.LabelValue) string {
	vb := &tagencoding.Values{}
	for _, v := range labelVals {
		b := make([]byte, 1, len(v.Value)+1)
		if v.Present {
			b[0] = 1
			b = append(b, []byte(v.Value)...)
		}
		vb.WriteValue(b)
	}
	return string(vb.Bytes())
}

func (bm *baseMetric) decodeLabelVals(s string) []metricdata.LabelValue {
	vals := make([]metricdata.LabelValue, 0, len(bm.keys))
	vb := &tagencoding.Values{Buffer: []byte(s)}
	for range bm.keys {
		v := vb.ReadValue()
		if v[0] == 0 {
			vals = append(vals, metricdata.LabelValue{})
		} else {
			vals = append(vals, metricdata.NewLabelValue(string(v[1:])))
		}
	}
	return vals
}

func (bm *baseMetric) entryForValues(labelVals []metricdata.LabelValue, newEntry func() baseEntry) (interface{}, error) {
	labelVals = append(bm.constLabelValues, labelVals...)
	if len(labelVals) != len(bm.keys) {
		return nil, errKeyValueMismatch
	}
	mapKey := bm.encodeLabelVals(labelVals)
	if entry, ok := bm.vals.Load(mapKey); ok {
		return entry, nil
	}
	entry, _ := bm.vals.LoadOrStore(mapKey, newEntry())
	return entry, nil
}

func (bm *baseMetric) upsertEntry(labelVals []metricdata.LabelValue, newEntry func() baseEntry) error {
	labelVals = append(bm.constLabelValues, labelVals...)
	if len(labelVals) != len(bm.keys) {
		return errKeyValueMismatch
	}
	mapKey := bm.encodeLabelVals(labelVals)
	bm.vals.Delete(mapKey)
	bm.vals.Store(mapKey, newEntry())
	return nil
}