-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
input.go
56 lines (43 loc) · 1.41 KB
/
input.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package file // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/file"
import (
"context"
"fmt"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
type toBodyFunc func([]byte) any
// Input is an operator that monitors files for entries
type Input struct {
helper.InputOperator
fileConsumer *fileconsumer.Manager
toBody toBodyFunc
}
// Start will start the file monitoring process
func (i *Input) Start(persister operator.Persister) error {
return i.fileConsumer.Start(persister)
}
// Stop will stop the file monitoring process
func (i *Input) Stop() error {
return i.fileConsumer.Stop()
}
func (i *Input) emit(ctx context.Context, token []byte, attrs map[string]any) error {
if len(token) == 0 {
return nil
}
ent, err := i.NewEntry(i.toBody(token))
if err != nil {
return fmt.Errorf("create entry: %w", err)
}
for k, v := range attrs {
if err := ent.Set(entry.NewAttributeField(k), v); err != nil {
i.Logger().Error("set attribute", zap.Error(err))
}
}
i.Write(ctx, ent)
return nil
}