| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver" |
| 5 |
|
| 6 |
import ( |
| 7 |
spb "google.golang.org/genproto/googleapis/rpc/status" |
| 8 |
"google.golang.org/protobuf/encoding/protojson" |
| 9 |
"google.golang.org/protobuf/proto" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/pdata/plog/plogotlp" |
| 12 |
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" |
| 13 |
"go.opentelemetry.io/collector/pdata/pprofile/pprofileotlp" |
| 14 |
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
pbContentType = "application/x-protobuf" |
| 19 |
jsonContentType = "application/json" |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
pbEncoder = &protoEncoder{} |
| 24 |
jsEncoder = &jsonEncoder{} |
| 25 |
) |
| 26 |
|
| 27 |
type encoder interface { |
| 28 |
unmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) |
| 29 |
unmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) |
| 30 |
unmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) |
| 31 |
unmarshalProfilesRequest(buf []byte) (pprofileotlp.ExportRequest, error) |
| 32 |
|
| 33 |
marshalTracesResponse(ptraceotlp.ExportResponse) ([]byte, error) |
| 34 |
marshalMetricsResponse(pmetricotlp.ExportResponse) ([]byte, error) |
| 35 |
marshalLogsResponse(plogotlp.ExportResponse) ([]byte, error) |
| 36 |
marshalProfilesResponse(pprofileotlp.ExportResponse) ([]byte, error) |
| 37 |
|
| 38 |
marshalStatus(rsp *spb.Status) ([]byte, error) |
| 39 |
|
| 40 |
contentType() string |
| 41 |
} |
| 42 |
|
| 43 |
type protoEncoder struct{} |
| 44 |
|
| 45 |
func (protoEncoder) unmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) { |
| 46 |
req := ptraceotlp.NewExportRequest() |
| 47 |
err := req.UnmarshalProto(buf) |
| 48 |
return req, err |
| 49 |
} |
| 50 |
|
| 51 |
func (protoEncoder) unmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) { |
| 52 |
req := pmetricotlp.NewExportRequest() |
| 53 |
err := req.UnmarshalProto(buf) |
| 54 |
return req, err |
| 55 |
} |
| 56 |
|
| 57 |
func (protoEncoder) unmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) { |
| 58 |
req := plogotlp.NewExportRequest() |
| 59 |
err := req.UnmarshalProto(buf) |
| 60 |
return req, err |
| 61 |
} |
| 62 |
|
| 63 |
func (protoEncoder) unmarshalProfilesRequest(buf []byte) (pprofileotlp.ExportRequest, error) { |
| 64 |
req := pprofileotlp.NewExportRequest() |
| 65 |
err := req.UnmarshalProto(buf) |
| 66 |
return req, err |
| 67 |
} |
| 68 |
|
| 69 |
func (protoEncoder) marshalTracesResponse(resp ptraceotlp.ExportResponse) ([]byte, error) { |
| 70 |
return resp.MarshalProto() |
| 71 |
} |
| 72 |
|
| 73 |
func (protoEncoder) marshalMetricsResponse(resp pmetricotlp.ExportResponse) ([]byte, error) { |
| 74 |
return resp.MarshalProto() |
| 75 |
} |
| 76 |
|
| 77 |
func (protoEncoder) marshalLogsResponse(resp plogotlp.ExportResponse) ([]byte, error) { |
| 78 |
return resp.MarshalProto() |
| 79 |
} |
| 80 |
|
| 81 |
func (protoEncoder) marshalProfilesResponse(resp pprofileotlp.ExportResponse) ([]byte, error) { |
| 82 |
return resp.MarshalProto() |
| 83 |
} |
| 84 |
|
| 85 |
func (protoEncoder) marshalStatus(resp *spb.Status) ([]byte, error) { |
| 86 |
return proto.Marshal(resp) |
| 87 |
} |
| 88 |
|
| 89 |
func (protoEncoder) contentType() string { |
| 90 |
return pbContentType |
| 91 |
} |
| 92 |
|
| 93 |
type jsonEncoder struct{} |
| 94 |
|
| 95 |
func (jsonEncoder) unmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) { |
| 96 |
req := ptraceotlp.NewExportRequest() |
| 97 |
err := req.UnmarshalJSON(buf) |
| 98 |
return req, err |
| 99 |
} |
| 100 |
|
| 101 |
func (jsonEncoder) unmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) { |
| 102 |
req := pmetricotlp.NewExportRequest() |
| 103 |
err := req.UnmarshalJSON(buf) |
| 104 |
return req, err |
| 105 |
} |
| 106 |
|
| 107 |
func (jsonEncoder) unmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) { |
| 108 |
req := plogotlp.NewExportRequest() |
| 109 |
err := req.UnmarshalJSON(buf) |
| 110 |
return req, err |
| 111 |
} |
| 112 |
|
| 113 |
func (jsonEncoder) unmarshalProfilesRequest(buf []byte) (pprofileotlp.ExportRequest, error) { |
| 114 |
req := pprofileotlp.NewExportRequest() |
| 115 |
err := req.UnmarshalJSON(buf) |
| 116 |
return req, err |
| 117 |
} |
| 118 |
|
| 119 |
func (jsonEncoder) marshalTracesResponse(resp ptraceotlp.ExportResponse) ([]byte, error) { |
| 120 |
return resp.MarshalJSON() |
| 121 |
} |
| 122 |
|
| 123 |
func (jsonEncoder) marshalMetricsResponse(resp pmetricotlp.ExportResponse) ([]byte, error) { |
| 124 |
return resp.MarshalJSON() |
| 125 |
} |
| 126 |
|
| 127 |
func (jsonEncoder) marshalLogsResponse(resp plogotlp.ExportResponse) ([]byte, error) { |
| 128 |
return resp.MarshalJSON() |
| 129 |
} |
| 130 |
|
| 131 |
func (jsonEncoder) marshalProfilesResponse(resp pprofileotlp.ExportResponse) ([]byte, error) { |
| 132 |
return resp.MarshalJSON() |
| 133 |
} |
| 134 |
|
| 135 |
func (jsonEncoder) marshalStatus(resp *spb.Status) ([]byte, error) { |
| 136 |
return protojson.Marshal(resp) |
| 137 |
} |
| 138 |
|
| 139 |
func (jsonEncoder) contentType() string { |
| 140 |
return jsonContentType |
| 141 |
} |