github.com/observiq/bindplane-agent@v1.51.0/internal/processor/snapshotprocessor/processor.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 snapshotprocessor 16 17 import ( 18 "context" 19 20 "github.com/observiq/bindplane-agent/internal/report" 21 "github.com/observiq/bindplane-agent/internal/report/snapshot" 22 "go.opentelemetry.io/collector/pdata/plog" 23 "go.opentelemetry.io/collector/pdata/pmetric" 24 "go.opentelemetry.io/collector/pdata/ptrace" 25 "go.uber.org/zap" 26 ) 27 28 // getSnapshotReporter is function for retrieving the SnapshotReporter. 29 // Meant to be overridden for tests. 30 var getSnapshotReporter func() *report.SnapshotReporter = report.GetSnapshotReporter 31 32 type snapshotProcessor struct { 33 logger *zap.Logger 34 enabled bool 35 snapShotter snapshot.Snapshotter 36 processorID string 37 } 38 39 // newSnapshotProcessor creates a new snapshot processor 40 func newSnapshotProcessor(logger *zap.Logger, cfg *Config, processorID string) *snapshotProcessor { 41 return &snapshotProcessor{ 42 logger: logger, 43 enabled: cfg.Enabled, 44 snapShotter: getSnapshotReporter(), 45 processorID: processorID, 46 } 47 } 48 49 func (sp *snapshotProcessor) processTraces(_ context.Context, td ptrace.Traces) (ptrace.Traces, error) { 50 if sp.enabled { 51 newTraces := ptrace.NewTraces() 52 td.CopyTo(newTraces) 53 sp.snapShotter.SaveTraces(sp.processorID, newTraces) 54 } 55 56 return td, nil 57 } 58 59 func (sp *snapshotProcessor) processLogs(_ context.Context, ld plog.Logs) (plog.Logs, error) { 60 if sp.enabled { 61 newLogs := plog.NewLogs() 62 ld.CopyTo(newLogs) 63 sp.snapShotter.SaveLogs(sp.processorID, newLogs) 64 } 65 66 return ld, nil 67 } 68 69 func (sp *snapshotProcessor) processMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) { 70 if sp.enabled { 71 newMetrics := pmetric.NewMetrics() 72 md.CopyTo(newMetrics) 73 sp.snapShotter.SaveMetrics(sp.processorID, newMetrics) 74 } 75 76 return md, nil 77 }