/
api.go
98 lines (82 loc) · 2.88 KB
/
api.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
// Copyright 2019 Authors of Hubble
//
// 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 api
import (
"context"
"strings"
pb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/prometheus/client_golang/prometheus"
)
const (
// DefaultPrometheusNamespace is the default namespace (prefix) used
// for all Hubble related Prometheus metrics
DefaultPrometheusNamespace = "hubble"
)
// Map is a set of metrics with their corresponding options
type Map map[string]Options
// ParseMetricList parses a slice of metric options and returns a map of
// enabled metrics
func ParseMetricList(enabledMetrics []string) (m Map) {
m = Map{}
for _, metric := range enabledMetrics {
s := strings.SplitN(metric, ":", 2)
if len(s) == 2 {
m[s[0]] = ParseOptions(s[1])
} else {
m[s[0]] = Options{}
}
}
return
}
// Handlers is a slice of metric handler
type Handlers []Handler
// Plugin is a metric plugin. A metric plugin is associated a name and is
// responsible to spawn metric handlers of a certain type.
type Plugin interface {
// NewHandler returns a new metric handler of the respective plugin
NewHandler() Handler
// HelpText returns a human readable help text including a description
// of the options
HelpText() string
}
// Handler is a metric handler. It is called upon receival of raw event data
// and is responsible to perform metrics accounting according to the scope of
// the metrics plugin.
type Handler interface {
// Init must initialize the metric handler by validating and parsing
// the options and then registering all required metrics with the
// specifies Prometheus registry
Init(registry *prometheus.Registry, options Options) error
// ProcessFlow must processes a flow event and perform metrics
// accounting
ProcessFlow(ctx context.Context, flow *pb.Flow)
// Status returns the configuration status of the metric handler
Status() string
}
// ProcessFlow processes a flow by calling ProcessFlow it on to all enabled
// metric handlers
func (h Handlers) ProcessFlow(ctx context.Context, flow *pb.Flow) {
for _, mh := range h {
mh.ProcessFlow(ctx, flow)
}
}
var registry = NewRegistry(
logging.DefaultLogger.WithField(logfields.LogSubsys, "hubble"),
)
// DefaultRegistry returns the default registry of all available metric plugins
func DefaultRegistry() *Registry {
return registry
}