/
observability.go
100 lines (90 loc) · 2.84 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2021 OpenTelemetry 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 collector
import (
"context"
"strconv"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
pointCount = stats.Int64("googlecloudmonitoring/point_count", "Count of metric points written to Cloud Monitoring.", "1")
exemplarAttachmentDropCount = stats.Int64("googlecloudmonitoring/exemplar_attachments_dropped", "Count of exemplar attachments dropped.", "{attachments}")
statusKey = tag.MustNewKey("status")
)
var viewPointCount = &view.View{
Name: pointCount.Name(),
Description: pointCount.Description(),
Measure: pointCount,
Aggregation: view.Sum(),
TagKeys: []tag.Key{statusKey},
}
// MetricViews returns a slice of views for this exporter's metrics.
func MetricViews() []*view.View {
return []*view.View{viewPointCount}
}
func recordExemplarFailure(ctx context.Context, point int) {
stats.Record(ctx, exemplarAttachmentDropCount.M(int64(point)))
}
func recordPointCountDataPoint(ctx context.Context, points int, statusValue string) {
ctx, err := tag.New(ctx, tag.Insert(statusKey, statusValue))
if err != nil {
return
}
stats.Record(ctx, pointCount.M(int64(points)))
}
func statusCodeToString(s *status.Status) string {
// see https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
switch c := s.Code(); c {
case codes.OK:
return "OK"
case codes.Canceled:
return "CANCELLED"
case codes.Unknown:
return "UNKNOWN"
case codes.InvalidArgument:
return "INVALID_ARGUMENT"
case codes.DeadlineExceeded:
return "DEADLINE_EXCEEDED"
case codes.NotFound:
return "NOT_FOUND"
case codes.AlreadyExists:
return "ALREADY_EXISTS"
case codes.PermissionDenied:
return "PERMISSION_DENIED"
case codes.ResourceExhausted:
return "RESOURCE_EXHAUSTED"
case codes.FailedPrecondition:
return "FAILED_PRECONDITION"
case codes.Aborted:
return "ABORTED"
case codes.OutOfRange:
return "OUT_OF_RANGE"
case codes.Unimplemented:
return "UNIMPLEMENTED"
case codes.Internal:
return "INTERNAL"
case codes.Unavailable:
return "UNAVAILABLE"
case codes.DataLoss:
return "DATA_LOSS"
case codes.Unauthenticated:
return "UNAUTHENTICATED"
default:
return "CODE_" + strconv.FormatInt(int64(c), 10)
}
}