-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
137 lines (118 loc) · 4.53 KB
/
errors.go
File metadata and controls
137 lines (118 loc) · 4.53 KB
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
// Copyright (C) 2022, Rob Lyon <rob@ctxswitch.com>
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 strata
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
)
type StrataError string
const (
// ErrInvalidMetricName is returned when a metric name contains other
// characters other than [a-zA-Z_-].
ErrInvalidMetricName = StrataError("Invalid metric name")
// ErrRegistrationFailed is returned if prometheus is unable to register
// the collector.
ErrRegistrationFailed = StrataError("Unable to register collector")
// ErrAlreadyRegistered is returned if prometheus has already registered
// a collector.
ErrAlreadyRegistered = StrataError("metric is already registered")
// ErrNoMetrics is returned if the context does not contain the metrics
// key.
ErrNoMetrics = StrataError("no metrics found in context")
// ErrNilContext is returned if the context is nil.
ErrNilContext = StrataError("context is nil")
)
// Error implements the error interface for StrataError.
func (e StrataError) Error() string {
return string(e)
}
// ApexInternalErrorMetrics provides internal counters for recovered
// errors from the prometheus collector when PanicOnError is false.
type ApexInternalErrorMetrics struct {
errPanicRecovery *prometheus.CounterVec
errInvalidMetricName *prometheus.CounterVec
errRegistrationFailed *prometheus.CounterVec
errAlreadyRegistered *prometheus.CounterVec
}
// NewApexInternalErrorMetrics defines and registers the internal collectors and
// returns a new ApexInternalErrorMetrics struct.
func NewApexInternalErrorMetrics(prefixes []string, sep rune) *ApexInternalErrorMetrics {
prefix := strings.Join(prefixes, "_")
errPanicRecovery := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: prefix + "_panic_recovery",
}, []string{"name", "type"})
errInvalidMetricName := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: prefix + "_invalid_metric_name",
}, []string{"name", "type"})
errRegistrationFailed := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: prefix + "_registration_failed",
}, []string{"name", "type"})
errAlreadyRegistered := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: prefix + "_already_registered",
}, []string{"name", "type"})
register(errPanicRecovery)
register(errInvalidMetricName)
register(errRegistrationFailed)
register(errAlreadyRegistered)
return &ApexInternalErrorMetrics{
errPanicRecovery: errPanicRecovery,
errInvalidMetricName: errInvalidMetricName,
errRegistrationFailed: errRegistrationFailed,
errAlreadyRegistered: errAlreadyRegistered,
}
}
// PanicRecovery provides a helper function for incrementing the errPanicRecovery
// collector.
func (a *ApexInternalErrorMetrics) PanicRecovery(name string, t string) {
a.errPanicRecovery.With(prometheus.Labels{
"name": name,
"type": t,
}).Inc()
}
// InvalidMeticName provides a helper function for incrementing the errInvalidMetricName
// collector.
func (a *ApexInternalErrorMetrics) InvalidMetricName(name string, t string) {
a.errInvalidMetricName.With(prometheus.Labels{
"name": name,
"type": t,
}).Inc()
}
// RegistrationFailed provides a helper function for incrementing the errRegistrationFailed
// collector.
func (a *ApexInternalErrorMetrics) RegistrationFailed(name string, t string) {
a.errRegistrationFailed.With(prometheus.Labels{
"name": name,
"type": t,
}).Inc()
}
// AlreadyRegistered provides a helper function for incrementing the errAlreadyRegistered
// collector.
func (a *ApexInternalErrorMetrics) AlreadyRegistered(name string, t string) {
a.errAlreadyRegistered.With(prometheus.Labels{
"name": name,
"type": t,
}).Inc()
}
func register(metric prometheus.Collector) {
if err := prometheus.Register(metric); err != nil {
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
panic(err)
}
}
}