/
event_observability.go
77 lines (64 loc) · 1.89 KB
/
event_observability.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
package cloudevents
import (
"fmt"
"github.com/cloudevents/sdk-go/pkg/cloudevents/observability"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)
var (
// EventMarshalLatencyMs measures the latency in milliseconds for the
// CloudEvents.Event marshal/unmarshalJSON methods.
EventMarshalLatencyMs = stats.Float64(
"cloudevents.io/sdk-go/event/json/latency",
"The latency in milliseconds of (un)marshalJSON methods for CloudEvents.Event.",
"ms")
)
var (
// LatencyView is an OpenCensus view that shows CloudEvents.Event (un)marshalJSON method latency.
EventMarshalLatencyView = &view.View{
Name: "event/json/latency",
Measure: EventMarshalLatencyMs,
Description: "The distribution of latency inside of (un)marshalJSON methods for CloudEvents.Event.",
Aggregation: view.Distribution(0, .01, .1, 1, 10, 100, 1000, 10000),
TagKeys: observability.LatencyTags(),
}
)
type observed int32
// Adheres to Observable
var _ observability.Observable = observed(0)
const (
reportMarshal observed = iota
reportUnmarshal
)
// MethodName implements Observable.MethodName
func (o observed) MethodName() string {
switch o {
case reportMarshal:
return "marshaljson"
case reportUnmarshal:
return "unmarshaljson"
default:
return "unknown"
}
}
// LatencyMs implements Observable.LatencyMs
func (o observed) LatencyMs() *stats.Float64Measure {
return EventMarshalLatencyMs
}
// eventJSONObserved is a wrapper to append version to observed.
type eventJSONObserved struct {
// Method
o observed
// Version
v string
}
// Adheres to Observable
var _ observability.Observable = (*eventJSONObserved)(nil)
// MethodName implements Observable.MethodName
func (c eventJSONObserved) MethodName() string {
return fmt.Sprintf("%s/%s", c.o.MethodName(), c.v)
}
// LatencyMs implements Observable.LatencyMs
func (c eventJSONObserved) LatencyMs() *stats.Float64Measure {
return c.o.LatencyMs()
}