/
encoder.go
120 lines (92 loc) · 3.48 KB
/
encoder.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
// Copyright The OpenTelemetry Authors
//
// 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 otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver"
import (
"bytes"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
spb "google.golang.org/genproto/googleapis/rpc/status"
"go.opentelemetry.io/collector/model/otlpgrpc"
)
const (
pbContentType = "application/x-protobuf"
jsonContentType = "application/json"
)
var (
pbEncoder = &protoEncoder{}
jsEncoder = &jsonEncoder{}
jsonMarshaler = &jsonpb.Marshaler{}
)
type encoder interface {
unmarshalTracesRequest(buf []byte) (otlpgrpc.TracesRequest, error)
unmarshalMetricsRequest(buf []byte) (otlpgrpc.MetricsRequest, error)
unmarshalLogsRequest(buf []byte) (otlpgrpc.LogsRequest, error)
marshalTracesResponse(otlpgrpc.TracesResponse) ([]byte, error)
marshalMetricsResponse(otlpgrpc.MetricsResponse) ([]byte, error)
marshalLogsResponse(otlpgrpc.LogsResponse) ([]byte, error)
marshalStatus(rsp *spb.Status) ([]byte, error)
contentType() string
}
type protoEncoder struct{}
func (protoEncoder) unmarshalTracesRequest(buf []byte) (otlpgrpc.TracesRequest, error) {
return otlpgrpc.UnmarshalTracesRequest(buf)
}
func (protoEncoder) unmarshalMetricsRequest(buf []byte) (otlpgrpc.MetricsRequest, error) {
return otlpgrpc.UnmarshalMetricsRequest(buf)
}
func (protoEncoder) unmarshalLogsRequest(buf []byte) (otlpgrpc.LogsRequest, error) {
return otlpgrpc.UnmarshalLogsRequest(buf)
}
func (protoEncoder) marshalTracesResponse(resp otlpgrpc.TracesResponse) ([]byte, error) {
return resp.Marshal()
}
func (protoEncoder) marshalMetricsResponse(resp otlpgrpc.MetricsResponse) ([]byte, error) {
return resp.Marshal()
}
func (protoEncoder) marshalLogsResponse(resp otlpgrpc.LogsResponse) ([]byte, error) {
return resp.Marshal()
}
func (protoEncoder) marshalStatus(resp *spb.Status) ([]byte, error) {
return proto.Marshal(resp)
}
func (protoEncoder) contentType() string {
return pbContentType
}
type jsonEncoder struct{}
func (jsonEncoder) unmarshalTracesRequest(buf []byte) (otlpgrpc.TracesRequest, error) {
return otlpgrpc.UnmarshalJSONTracesRequest(buf)
}
func (jsonEncoder) unmarshalMetricsRequest(buf []byte) (otlpgrpc.MetricsRequest, error) {
return otlpgrpc.UnmarshalJSONMetricsRequest(buf)
}
func (jsonEncoder) unmarshalLogsRequest(buf []byte) (otlpgrpc.LogsRequest, error) {
return otlpgrpc.UnmarshalJSONLogsRequest(buf)
}
func (jsonEncoder) marshalTracesResponse(resp otlpgrpc.TracesResponse) ([]byte, error) {
return resp.MarshalJSON()
}
func (jsonEncoder) marshalMetricsResponse(resp otlpgrpc.MetricsResponse) ([]byte, error) {
return resp.MarshalJSON()
}
func (jsonEncoder) marshalLogsResponse(resp otlpgrpc.LogsResponse) ([]byte, error) {
return resp.MarshalJSON()
}
func (jsonEncoder) marshalStatus(resp *spb.Status) ([]byte, error) {
buf := new(bytes.Buffer)
err := jsonMarshaler.Marshal(buf, resp)
return buf.Bytes(), err
}
func (jsonEncoder) contentType() string {
return jsonContentType
}