mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
At first, there was a tentative to use BMP collector implementation from bio-rd. However, this current implementation is using GoBGP instead: - BMP is very simple from a protocol point of view. The hard work is mostly around decoding. Both bio-rd and GoBGP can decode, but for testing, GoBGP is able to generate messages as well (this is its primary purpose, I suppose parsing was done for testing purpose). Using only one library is always better. An alternative would be GoBMP, but it also only do parsing. - Logging and metrics can be customized easily (but the work was done for bio-rd, so not a real argument). - bio-rd is an application and there is no API stability (and I did that too) - GoBGP supports FlowSpec, which may be useful in the future for the DDoS part. Again, one library for everything is better (but honestly, GoBGP as a lib is not the best part of it, maybe github.com/jwhited/corebgp would be a better fit while keeping GoBGP for decoding/encoding). There was a huge effort around having a RIB which is efficient memory-wise (data are interned to save memory), performant during reads, while being decent during insertions. We rely on a patched version of Kentik's Patricia trees to be able to apply mutations to the tree. There was several tentatives to implement some kind of graceful restart, but ultimetaly, the design is kept simple: when a BMP connection goes down, routes will be removed after a configurable time. If the connection comes back up, then it is just considered new. It would have been ideal to rely on EoR markers, but the RFC is unclear about them, and they are likely to be per peer, making it difficult to know what to do if one peer is back, but not the other. Remaining tasks: - [ ] Confirm support for LocRIB - [ ] Import data in ClickHouse - [ ] Make data available in the frontend Fix #52
125 lines
3.9 KiB
Go
125 lines
3.9 KiB
Go
// SPDX-FileCopyrightText: 2022 Free Mobile
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
// Package metrics handles metrics for akvorado
|
|
//
|
|
// This is a wrapper around Prometheus Go client.
|
|
package metrics
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"akvorado/common/reporter/logger"
|
|
"akvorado/common/reporter/stack"
|
|
)
|
|
|
|
// Metrics represents the internal state of the metric subsystem.
|
|
type Metrics struct {
|
|
logger logger.Logger
|
|
config Configuration
|
|
registry *prometheus.Registry
|
|
factoryCache map[string]*Factory
|
|
factoryCacheLock sync.RWMutex
|
|
}
|
|
|
|
// New creates a new metric registry and setup the appropriate
|
|
// exporters. The provided prefix is used for system-wide metrics.
|
|
func New(logger logger.Logger, configuration Configuration) (*Metrics, error) {
|
|
reg := prometheus.NewRegistry()
|
|
reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
|
reg.MustRegister(collectors.NewGoCollector(collectors.WithGoCollections(
|
|
collectors.GoRuntimeMemStatsCollection | collectors.GoRuntimeMetricsCollection)))
|
|
m := Metrics{
|
|
logger: logger,
|
|
config: configuration,
|
|
registry: reg,
|
|
factoryCache: make(map[string]*Factory, 0),
|
|
}
|
|
|
|
return &m, nil
|
|
}
|
|
|
|
// HTTPHandler returns an handler to server Prometheus metrics.
|
|
func (m *Metrics) HTTPHandler() http.Handler {
|
|
return promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{
|
|
ErrorLog: promHTTPLogger{m.logger},
|
|
})
|
|
}
|
|
|
|
func getPrefix(module string) (moduleName string) {
|
|
if !strings.HasPrefix(module, stack.ModuleName) {
|
|
moduleName = stack.ModuleName
|
|
} else {
|
|
moduleName = strings.SplitN(module, ".", 2)[0]
|
|
}
|
|
moduleName = strings.ReplaceAll(moduleName, "/", "_")
|
|
moduleName = strings.ReplaceAll(moduleName, ".", "_")
|
|
moduleName = fmt.Sprintf("%s_", moduleName)
|
|
return
|
|
}
|
|
|
|
// Factory returns a factory to register new metrics with promauto. It
|
|
// includes the module as an automatic prefix. This method is expected
|
|
// to be called only from our own module to avoid walking the stack
|
|
// too often. It uses a cache to speedup things a little bit.
|
|
func (m *Metrics) Factory(skipCallstack int) *Factory {
|
|
callStack := stack.Callers()
|
|
call := callStack[1+skipCallstack] // Trial and error, there is a test to check it works
|
|
module := call.FunctionName()
|
|
|
|
// Hotpath
|
|
if factory := func() *Factory {
|
|
m.factoryCacheLock.RLock()
|
|
defer m.factoryCacheLock.RUnlock()
|
|
if factory, ok := m.factoryCache[module]; ok {
|
|
return factory
|
|
}
|
|
return nil
|
|
}(); factory != nil {
|
|
return factory
|
|
}
|
|
|
|
// Slow path
|
|
m.factoryCacheLock.Lock()
|
|
defer m.factoryCacheLock.Unlock()
|
|
moduleName := getPrefix(module)
|
|
factory := Factory{
|
|
prefix: moduleName,
|
|
registry: m.registry,
|
|
}
|
|
m.factoryCache[module] = &factory
|
|
return &factory
|
|
}
|
|
|
|
// Desc allocates and initializes and new metric description. Like for
|
|
// factory, names are prefixed with the module name. Unlike factory,
|
|
// there is no cache.
|
|
func (m *Metrics) Desc(skipCallstack int, name, help string, variableLabels []string) *prometheus.Desc {
|
|
callStack := stack.Callers()
|
|
call := callStack[1+skipCallstack] // Trial and error, there is a test to check it works
|
|
prefix := getPrefix(call.FunctionName())
|
|
name = fmt.Sprintf("%s%s", prefix, name)
|
|
return prometheus.NewDesc(name, help, variableLabels, nil)
|
|
}
|
|
|
|
// Collector register a custom collector.
|
|
func (m *Metrics) Collector(c prometheus.Collector) {
|
|
m.registry.MustRegister(c)
|
|
}
|
|
|
|
// CollectorForCurrentModule register a custom collector and prefix
|
|
// everything with the module name.
|
|
func (m *Metrics) CollectorForCurrentModule(skipCallStack int, c prometheus.Collector) {
|
|
callStack := stack.Callers()
|
|
call := callStack[1+skipCallStack] // Should be the same as above !
|
|
prefix := getPrefix(call.FunctionName())
|
|
prometheus.WrapRegistererWithPrefix(prefix, m.registry).MustRegister(c)
|
|
}
|