-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
file.go
199 lines (180 loc) · 6.66 KB
/
file.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otlpjsonfilereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver"
import (
"context"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerprofiles"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/receiverprofiles"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/emit"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver/internal/metadata"
)
const (
transport = "file"
)
// NewFactory creates a factory for file receiver
func NewFactory() receiver.Factory {
return receiverprofiles.NewFactory(
metadata.Type,
createDefaultConfig,
receiverprofiles.WithMetrics(createMetricsReceiver, metadata.MetricsStability),
receiverprofiles.WithLogs(createLogsReceiver, metadata.LogsStability),
receiverprofiles.WithTraces(createTracesReceiver, metadata.TracesStability),
receiverprofiles.WithProfiles(createProfilesReceiver, metadata.ProfilesStability))
}
type Config struct {
fileconsumer.Config `mapstructure:",squash"`
StorageID *component.ID `mapstructure:"storage"`
ReplayFile bool `mapstructure:"replay_file"`
}
func createDefaultConfig() component.Config {
return &Config{
Config: *fileconsumer.NewConfig(),
}
}
type otlpjsonfilereceiver struct {
input *fileconsumer.Manager
id component.ID
storageID *component.ID
}
func (f *otlpjsonfilereceiver) Start(ctx context.Context, host component.Host) error {
storageClient, err := adapter.GetStorageClient(ctx, host, f.storageID, f.id)
if err != nil {
return err
}
return f.input.Start(storageClient)
}
func (f *otlpjsonfilereceiver) Shutdown(_ context.Context) error {
return f.input.Stop()
}
func createLogsReceiver(_ context.Context, settings receiver.Settings, configuration component.Config, logs consumer.Logs) (receiver.Logs, error) {
logsUnmarshaler := &plog.JSONUnmarshaler{}
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: settings.ID,
Transport: transport,
ReceiverCreateSettings: settings,
})
if err != nil {
return nil, err
}
cfg := configuration.(*Config)
opts := make([]fileconsumer.Option, 0)
if cfg.ReplayFile {
opts = append(opts, fileconsumer.WithNoTracking())
}
input, err := cfg.Config.Build(settings.TelemetrySettings, func(ctx context.Context, token emit.Token) error {
ctx = obsrecv.StartLogsOp(ctx)
var l plog.Logs
l, err = logsUnmarshaler.UnmarshalLogs(token.Body)
if err != nil {
obsrecv.EndLogsOp(ctx, metadata.Type.String(), 0, err)
} else {
logRecordCount := l.LogRecordCount()
if logRecordCount != 0 {
err = logs.ConsumeLogs(ctx, l)
}
obsrecv.EndLogsOp(ctx, metadata.Type.String(), logRecordCount, err)
}
return nil
}, opts...)
if err != nil {
return nil, err
}
return &otlpjsonfilereceiver{input: input, id: settings.ID, storageID: cfg.StorageID}, nil
}
func createMetricsReceiver(_ context.Context, settings receiver.Settings, configuration component.Config, metrics consumer.Metrics) (receiver.Metrics, error) {
metricsUnmarshaler := &pmetric.JSONUnmarshaler{}
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: settings.ID,
Transport: transport,
ReceiverCreateSettings: settings,
})
if err != nil {
return nil, err
}
cfg := configuration.(*Config)
opts := make([]fileconsumer.Option, 0)
if cfg.ReplayFile {
opts = append(opts, fileconsumer.WithNoTracking())
}
input, err := cfg.Config.Build(settings.TelemetrySettings, func(ctx context.Context, token emit.Token) error {
ctx = obsrecv.StartMetricsOp(ctx)
var m pmetric.Metrics
m, err = metricsUnmarshaler.UnmarshalMetrics(token.Body)
if err != nil {
obsrecv.EndMetricsOp(ctx, metadata.Type.String(), 0, err)
} else {
if m.ResourceMetrics().Len() != 0 {
err = metrics.ConsumeMetrics(ctx, m)
}
obsrecv.EndMetricsOp(ctx, metadata.Type.String(), m.MetricCount(), err)
}
return nil
}, opts...)
if err != nil {
return nil, err
}
return &otlpjsonfilereceiver{input: input, id: settings.ID, storageID: cfg.StorageID}, nil
}
func createTracesReceiver(_ context.Context, settings receiver.Settings, configuration component.Config, traces consumer.Traces) (receiver.Traces, error) {
tracesUnmarshaler := &ptrace.JSONUnmarshaler{}
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: settings.ID,
Transport: transport,
ReceiverCreateSettings: settings,
})
if err != nil {
return nil, err
}
cfg := configuration.(*Config)
opts := make([]fileconsumer.Option, 0)
if cfg.ReplayFile {
opts = append(opts, fileconsumer.WithNoTracking())
}
input, err := cfg.Config.Build(settings.TelemetrySettings, func(ctx context.Context, token emit.Token) error {
ctx = obsrecv.StartTracesOp(ctx)
var t ptrace.Traces
t, err = tracesUnmarshaler.UnmarshalTraces(token.Body)
if err != nil {
obsrecv.EndTracesOp(ctx, metadata.Type.String(), 0, err)
} else {
if t.ResourceSpans().Len() != 0 {
err = traces.ConsumeTraces(ctx, t)
}
obsrecv.EndTracesOp(ctx, metadata.Type.String(), t.SpanCount(), err)
}
return nil
}, opts...)
if err != nil {
return nil, err
}
return &otlpjsonfilereceiver{input: input, id: settings.ID, storageID: cfg.StorageID}, nil
}
func createProfilesReceiver(_ context.Context, settings receiver.Settings, configuration component.Config, profiles consumerprofiles.Profiles) (receiverprofiles.Profiles, error) {
profilesUnmarshaler := &pprofile.JSONUnmarshaler{}
cfg := configuration.(*Config)
opts := make([]fileconsumer.Option, 0)
if cfg.ReplayFile {
opts = append(opts, fileconsumer.WithNoTracking())
}
input, err := cfg.Config.Build(settings.TelemetrySettings, func(ctx context.Context, token emit.Token) error {
p, _ := profilesUnmarshaler.UnmarshalProfiles(token.Body)
if p.ResourceProfiles().Len() != 0 {
_ = profiles.ConsumeProfiles(ctx, p)
}
return nil
}, opts...)
if err != nil {
return nil, err
}
return &otlpjsonfilereceiver{input: input, id: settings.ID, storageID: cfg.StorageID}, nil
}