github.com/observiq/bindplane-agent@v1.51.0/internal/service/managed.go (about) 1 // Copyright observIQ, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package service provides a service wrapper around the collector regardless of managed or standalone mode. 16 package service 17 18 import ( 19 "context" 20 "fmt" 21 22 "github.com/observiq/bindplane-agent/collector" 23 "github.com/observiq/bindplane-agent/internal/version" 24 "github.com/observiq/bindplane-agent/opamp" 25 "github.com/observiq/bindplane-agent/opamp/observiq" 26 "go.uber.org/zap" 27 ) 28 29 // ManagedCollectorService is a RunnableService that runs the collector being managed by an OpAmp enabled platform 30 type ManagedCollectorService struct { 31 logger *zap.Logger 32 client opamp.Client 33 34 // Config paths 35 managerConfigPath string 36 collectorConfigPath string 37 loggerConfigPath string 38 } 39 40 // NewManagedCollectorService creates a new ManagedCollectorService 41 func NewManagedCollectorService(col collector.Collector, logger *zap.Logger, managerConfigPath, collectorConfigPath, loggerConfigPath string) (*ManagedCollectorService, error) { 42 opampConfig, err := opamp.ParseConfig(managerConfigPath) 43 if err != nil { 44 return nil, fmt.Errorf("failed to parse manager config: %w", err) 45 } 46 47 // Create client Args 48 clientArgs := &observiq.NewClientArgs{ 49 DefaultLogger: logger, 50 Config: *opampConfig, 51 Collector: col, 52 Version: version.Version(), 53 TmpPath: "./tmp", 54 ManagerConfigPath: managerConfigPath, 55 CollectorConfigPath: collectorConfigPath, 56 LoggerConfigPath: loggerConfigPath, 57 } 58 59 // Create new client 60 client, err := observiq.NewClient(clientArgs) 61 if err != nil { 62 return nil, fmt.Errorf("failed to create observIQ client: %w", err) 63 } 64 65 return &ManagedCollectorService{ 66 client: client, 67 logger: logger, 68 managerConfigPath: managerConfigPath, 69 collectorConfigPath: collectorConfigPath, 70 loggerConfigPath: loggerConfigPath, 71 }, nil 72 } 73 74 // Start initiates the OpAmp connection and starts the collector 75 func (m *ManagedCollectorService) Start(ctx context.Context) error { 76 m.logger.Info("Starting in managed mode") 77 78 // Connect to manager platform 79 if err := m.client.Connect(ctx); err != nil { 80 return fmt.Errorf("error during OpAmp connection: %w", err) 81 } 82 83 return nil 84 } 85 86 // Stop stops the collector and disconnects from the platform 87 func (m *ManagedCollectorService) Stop(ctx context.Context) error { 88 m.logger.Info("Shutting down collector") 89 if err := m.client.Disconnect(ctx); err != nil { 90 return fmt.Errorf("error during client disconnect: %w", err) 91 } 92 return nil 93 } 94 95 // Error returns an empty error channel. This will never send errors. 96 func (m *ManagedCollectorService) Error() <-chan error { 97 // send new channel that's never used 98 return make(<-chan error) 99 }