|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Adapted from go.opentelemetry.io/collector/internal/sharedcomponent |
| 5 | +// (cannot be imported directly because it is an internal package). |
| 6 | + |
| 7 | +// Package sharedcomponent exposes functionality for components to register |
| 8 | +// against a shared key, such as a configuration object, in order to be reused |
| 9 | +// across multiple pipelines that reference the same component ID. |
| 10 | +// This is particularly useful when the component relies on an expensive shared |
| 11 | +// resource (e.g. cloud-metadata HTTP calls, Kubernetes API connections) that |
| 12 | +// should not be duplicated. |
| 13 | +package sharedcomponent // import "github.com/elastic/beats/v7/x-pack/otel/internal/sharedcomponent" |
| 14 | + |
| 15 | +import ( |
| 16 | + "container/ring" |
| 17 | + "context" |
| 18 | + "sync" |
| 19 | + |
| 20 | + "go.opentelemetry.io/collector/component" |
| 21 | + "go.opentelemetry.io/collector/component/componentstatus" |
| 22 | +) |
| 23 | + |
| 24 | +// NewMap creates a new shared-component map. |
| 25 | +func NewMap[K comparable, V component.Component]() *Map[K, V] { |
| 26 | + return &Map[K, V]{ |
| 27 | + components: map[K]*Component[V]{}, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Map keeps a reference to all created instances for a given shared key such |
| 32 | +// as a component configuration pointer. |
| 33 | +type Map[K comparable, V component.Component] struct { |
| 34 | + lock sync.Mutex |
| 35 | + components map[K]*Component[V] |
| 36 | +} |
| 37 | + |
| 38 | +// LoadOrStore returns the already-created instance for key if one exists, |
| 39 | +// otherwise calls create, stores the result, and returns it. |
| 40 | +func (m *Map[K, V]) LoadOrStore(key K, create func() (V, error)) (*Component[V], error) { |
| 41 | + m.lock.Lock() |
| 42 | + defer m.lock.Unlock() |
| 43 | + if c, ok := m.components[key]; ok { |
| 44 | + return c, nil |
| 45 | + } |
| 46 | + comp, err := create() |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + newComp := &Component[V]{ |
| 51 | + component: comp, |
| 52 | + removeFunc: func() { |
| 53 | + m.lock.Lock() |
| 54 | + defer m.lock.Unlock() |
| 55 | + delete(m.components, key) |
| 56 | + }, |
| 57 | + } |
| 58 | + m.components[key] = newComp |
| 59 | + return newComp, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Len returns the number of components currently held in the map. |
| 63 | +func (m *Map[K, V]) Len() int { |
| 64 | + m.lock.Lock() |
| 65 | + defer m.lock.Unlock() |
| 66 | + return len(m.components) |
| 67 | +} |
| 68 | + |
| 69 | +// Component ensures that the wrapped component is started and stopped only |
| 70 | +// once. When stopped it is removed from the Map. |
| 71 | +type Component[V component.Component] struct { |
| 72 | + component V |
| 73 | + |
| 74 | + startOnce sync.Once |
| 75 | + stopOnce sync.Once |
| 76 | + removeFunc func() |
| 77 | + |
| 78 | + hostWrapper *hostWrapper |
| 79 | +} |
| 80 | + |
| 81 | +// Unwrap returns the original component. |
| 82 | +func (c *Component[V]) Unwrap() V { |
| 83 | + return c.component |
| 84 | +} |
| 85 | + |
| 86 | +// Start starts the underlying component if it has never been started before. |
| 87 | +// Subsequent calls are no-ops for the underlying component but register |
| 88 | +// additional status reporters. |
| 89 | +func (c *Component[V]) Start(ctx context.Context, host component.Host) error { |
| 90 | + if c.hostWrapper == nil { |
| 91 | + var err error |
| 92 | + c.startOnce.Do(func() { |
| 93 | + c.hostWrapper = &hostWrapper{ |
| 94 | + host: host, |
| 95 | + sources: make([]componentstatus.Reporter, 0), |
| 96 | + previousEvents: ring.New(5), |
| 97 | + } |
| 98 | + if statusReporter, ok := host.(componentstatus.Reporter); ok { |
| 99 | + c.hostWrapper.addSource(statusReporter) |
| 100 | + } |
| 101 | + c.hostWrapper.Report(componentstatus.NewEvent(componentstatus.StatusStarting)) |
| 102 | + if err = c.component.Start(ctx, c.hostWrapper); err != nil { |
| 103 | + c.hostWrapper.Report(componentstatus.NewPermanentErrorEvent(err)) |
| 104 | + } |
| 105 | + }) |
| 106 | + return err |
| 107 | + } |
| 108 | + if statusReporter, ok := host.(componentstatus.Reporter); ok { |
| 109 | + c.hostWrapper.addSource(statusReporter) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// Shutdown shuts down the underlying component exactly once, then removes it |
| 115 | +// from the parent Map so the same configuration can be recreated if needed. |
| 116 | +func (c *Component[V]) Shutdown(ctx context.Context) error { |
| 117 | + var err error |
| 118 | + c.stopOnce.Do(func() { |
| 119 | + if c.hostWrapper != nil { |
| 120 | + c.hostWrapper.Report(componentstatus.NewEvent(componentstatus.StatusStopping)) |
| 121 | + } |
| 122 | + err = c.component.Shutdown(ctx) |
| 123 | + if c.hostWrapper != nil { |
| 124 | + if err != nil { |
| 125 | + c.hostWrapper.Report(componentstatus.NewPermanentErrorEvent(err)) |
| 126 | + } else { |
| 127 | + c.hostWrapper.Report(componentstatus.NewEvent(componentstatus.StatusStopped)) |
| 128 | + } |
| 129 | + } |
| 130 | + c.removeFunc() |
| 131 | + }) |
| 132 | + return err |
| 133 | +} |
| 134 | + |
| 135 | +var ( |
| 136 | + _ component.Host = (*hostWrapper)(nil) |
| 137 | + _ componentstatus.Reporter = (*hostWrapper)(nil) |
| 138 | +) |
| 139 | + |
| 140 | +type hostWrapper struct { |
| 141 | + host component.Host |
| 142 | + sources []componentstatus.Reporter |
| 143 | + previousEvents *ring.Ring |
| 144 | + lock sync.Mutex |
| 145 | +} |
| 146 | + |
| 147 | +func (h *hostWrapper) GetExtensions() map[component.ID]component.Component { |
| 148 | + return h.host.GetExtensions() |
| 149 | +} |
| 150 | + |
| 151 | +func (h *hostWrapper) Report(e *componentstatus.Event) { |
| 152 | + h.lock.Lock() |
| 153 | + defer h.lock.Unlock() |
| 154 | + if len(h.sources) > 0 { |
| 155 | + h.previousEvents.Value = e |
| 156 | + h.previousEvents = h.previousEvents.Next() |
| 157 | + } |
| 158 | + for _, s := range h.sources { |
| 159 | + s.Report(e) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func (h *hostWrapper) addSource(s componentstatus.Reporter) { |
| 164 | + h.lock.Lock() |
| 165 | + defer h.lock.Unlock() |
| 166 | + h.previousEvents.Do(func(a any) { |
| 167 | + if e, ok := a.(*componentstatus.Event); ok { |
| 168 | + s.Report(e) |
| 169 | + } |
| 170 | + }) |
| 171 | + h.sources = append(h.sources, s) |
| 172 | +} |
0 commit comments