/
marshaler.go
67 lines (60 loc) · 2.1 KB
/
marshaler.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package awss3exporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awss3exporter"
import (
"errors"
"fmt"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"
)
type marshaler interface {
MarshalTraces(td ptrace.Traces) ([]byte, error)
MarshalLogs(ld plog.Logs) ([]byte, error)
MarshalMetrics(md pmetric.Metrics) ([]byte, error)
format() string
}
var (
ErrUnknownMarshaler = errors.New("unknown marshaler")
)
func newMarshalerFromEncoding(encoding *component.ID, fileFormat string, host component.Host, logger *zap.Logger) (marshaler, error) {
marshaler := &s3Marshaler{logger: logger}
e, ok := host.GetExtensions()[*encoding]
if !ok {
return nil, fmt.Errorf("unknown encoding %q", encoding)
}
// cast with ok to avoid panics.
marshaler.logsMarshaler, _ = e.(plog.Marshaler)
marshaler.metricsMarshaler, _ = e.(pmetric.Marshaler)
marshaler.tracesMarshaler, _ = e.(ptrace.Marshaler)
marshaler.fileFormat = fileFormat
return marshaler, nil
}
func newMarshaler(mType MarshalerType, logger *zap.Logger) (marshaler, error) {
marshaler := &s3Marshaler{logger: logger}
switch mType {
case OtlpProtobuf:
marshaler.logsMarshaler = &plog.ProtoMarshaler{}
marshaler.tracesMarshaler = &ptrace.ProtoMarshaler{}
marshaler.metricsMarshaler = &pmetric.ProtoMarshaler{}
marshaler.fileFormat = "binpb"
case OtlpJSON:
marshaler.logsMarshaler = &plog.JSONMarshaler{}
marshaler.tracesMarshaler = &ptrace.JSONMarshaler{}
marshaler.metricsMarshaler = &pmetric.JSONMarshaler{}
marshaler.fileFormat = "json"
case SumoIC:
sumomarshaler := newSumoICMarshaler()
marshaler.logsMarshaler = &sumomarshaler
marshaler.fileFormat = "json.gz"
case Body:
exportbodyMarshaler := newbodyMarshaler()
marshaler.logsMarshaler = &exportbodyMarshaler
marshaler.fileFormat = exportbodyMarshaler.format()
default:
return nil, ErrUnknownMarshaler
}
return marshaler, nil
}