-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarshaler.go
More file actions
143 lines (117 loc) · 3.95 KB
/
marshaler.go
File metadata and controls
143 lines (117 loc) · 3.95 KB
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
// Copyright observIQ, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package azureblobexporter // import "github.com/observiq/bindplane-otel-contrib/exporter/azureblobexporter"
import (
"bytes"
"compress/gzip"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)
// marshaler marshals into data bytes based on configuration
//
//go:generate mockery --name marshaler --output ./internal/mocks --with-expecter --filename mock_marshaler.go --structname MockMarshaler
type marshaler interface {
// MarshalTraces returns the marshaled json traces data
MarshalTraces(td ptrace.Traces) ([]byte, error)
// MarshalLogs returns the marshaled json logs data
MarshalLogs(ld plog.Logs) ([]byte, error)
// MarshalMetrics returns the marshaled json metrics data
MarshalMetrics(md pmetric.Metrics) ([]byte, error)
// Format returns the file format of the data this marshaler returns
Format() string
}
// newMarshaler creates a new marshaler based on compression type
func newMarshaler(compression compressionType) marshaler {
base := &baseMarshaler{
logsMarshaler: &plog.JSONMarshaler{},
tracesMarshaler: &ptrace.JSONMarshaler{},
metricsMarshaler: &pmetric.JSONMarshaler{},
}
switch compression {
case gzipCompression:
return &gzipMarshaler{
base: base,
}
default:
return base
}
}
// baseMarshaler is the base marshaller that marshals otlp structures into JSON
type baseMarshaler struct {
logsMarshaler plog.Marshaler
tracesMarshaler ptrace.Marshaler
metricsMarshaler pmetric.Marshaler
}
// MarshalTraces returns the marshaled json traces data
func (b *baseMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) {
return b.tracesMarshaler.MarshalTraces(td)
}
// MarshalLogs returns the marshaled json logs data
func (b *baseMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
return b.logsMarshaler.MarshalLogs(ld)
}
// MarshalMetrics returns the marshaled json metrics data
func (b *baseMarshaler) MarshalMetrics(md pmetric.Metrics) ([]byte, error) {
return b.metricsMarshaler.MarshalMetrics(md)
}
// Format returns the file format of the data this marshaler returns
func (b *baseMarshaler) Format() string {
return "json"
}
// gzipMarshaler gzip compresses marshalled data
type gzipMarshaler struct {
base *baseMarshaler
}
// MarshalTraces returns the marshaled json traces data
func (g *gzipMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) {
data, err := g.base.MarshalTraces(td)
if err != nil {
return nil, err
}
return g.compress(data)
}
// MarshalLogs returns the marshaled json logs data
func (g *gzipMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
data, err := g.base.MarshalLogs(ld)
if err != nil {
return nil, err
}
return g.compress(data)
}
// MarshalMetrics returns the marshaled json metrics data
func (g *gzipMarshaler) MarshalMetrics(md pmetric.Metrics) ([]byte, error) {
data, err := g.base.MarshalMetrics(md)
if err != nil {
return nil, err
}
return g.compress(data)
}
// Format returns the file format of the data this marshaler returns
func (g *gzipMarshaler) Format() string {
return "json.gz"
}
// compress applies gzip compression to the data
func (g *gzipMarshaler) compress(data []byte) ([]byte, error) {
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
_, err := writer.Write(data)
if err != nil {
return nil, err
}
if err := writer.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}