/
observable.go
73 lines (61 loc) · 1.73 KB
/
observable.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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package client
import (
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
// LatencyMs measures the latency in milliseconds for the CloudEvents
// client methods.
LatencyMs = stats.Float64("cloudevents.io/sdk-go/client/latency", "The latency in milliseconds for the CloudEvents client methods.", "ms")
// KeyMethod is the tag used for marking method on a metric.
KeyMethod, _ = tag.NewKey("method")
// KeyResult is the tag used for marking result on a metric.
KeyResult, _ = tag.NewKey("result")
// LatencyView is an OpenCensus view that shows client method latency.
LatencyView = &view.View{
Name: "client/latency",
Measure: LatencyMs,
Description: "The distribution of latency inside of client for CloudEvents.",
Aggregation: view.Distribution(0, .01, .1, 1, 10, 100, 1000, 10000),
TagKeys: LatencyTags(),
}
)
func LatencyTags() []tag.Key {
return []tag.Key{KeyMethod, KeyResult}
}
// Observable represents the the customization used by the Reporter for a given
// measurement and trace for a single method.
type Observable interface {
MethodName() string
LatencyMs() *stats.Float64Measure
}
type observed int32
// Adheres to Observable
var _ Observable = observed(0)
const (
reportSend observed = iota
reportRequest
reportReceive
)
// MethodName implements Observable.MethodName
func (o observed) MethodName() string {
switch o {
case reportSend:
return "send"
case reportRequest:
return "request"
case reportReceive:
return "receive"
default:
return "unknown"
}
}
// LatencyMs implements Observable.LatencyMs
func (o observed) LatencyMs() *stats.Float64Measure {
return LatencyMs
}