/
component_registry.go
70 lines (60 loc) · 2.32 KB
/
component_registry.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
package controller
import (
"fmt"
"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/featuregate"
)
// ComponentRegistry is a collection of registered components.
type ComponentRegistry interface {
// Get looks up a component by name. It returns an error if the component does not exist or its usage is restricted,
// for example, because of the component's stability level.
Get(name string) (component.Registration, error)
}
type defaultComponentRegistry struct {
minStability featuregate.Stability
}
// NewDefaultComponentRegistry creates a new [ComponentRegistry] which gets
// components registered to github.com/grafana/alloy/internal/component.
func NewDefaultComponentRegistry(minStability featuregate.Stability) ComponentRegistry {
return defaultComponentRegistry{
minStability: minStability,
}
}
// Get retrieves a component using [component.Get]. It returns an error if the component does not exist,
// or if the component's stability is below the minimum required stability level.
func (reg defaultComponentRegistry) Get(name string) (component.Registration, error) {
cr, exists := component.Get(name)
if !exists {
return component.Registration{}, fmt.Errorf("cannot find the definition of component name %q", name)
}
if err := featuregate.CheckAllowed(cr.Stability, reg.minStability, fmt.Sprintf("component %q", name)); err != nil {
return component.Registration{}, err
}
return cr, nil
}
type registryMap struct {
registrations map[string]component.Registration
minStability featuregate.Stability
}
// NewRegistryMap creates a new [ComponentRegistry] which uses a map to store components.
// Currently, it is only used in tests.
func NewRegistryMap(
minStability featuregate.Stability,
registrations map[string]component.Registration,
) ComponentRegistry {
return ®istryMap{
registrations: registrations,
minStability: minStability,
}
}
// Get retrieves a component using [component.Get].
func (m registryMap) Get(name string) (component.Registration, error) {
reg, ok := m.registrations[name]
if !ok {
return component.Registration{}, fmt.Errorf("cannot find the definition of component name %q", name)
}
if err := featuregate.CheckAllowed(reg.Stability, m.minStability, fmt.Sprintf("component %q", name)); err != nil {
return component.Registration{}, err
}
return reg, nil
}