|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package encoder // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/libhoneyreceiver/encoder" |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + |
| 9 | + "github.com/gogo/protobuf/jsonpb" |
| 10 | + "go.opentelemetry.io/collector/pdata/plog/plogotlp" |
| 11 | + "go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" |
| 12 | + "go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" |
| 13 | + spb "google.golang.org/genproto/googleapis/rpc/status" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + PbContentType = "application/x-protobuf" |
| 18 | + JsonContentType = "application/json" |
| 19 | + MsgpackContentType = "application/x-msgpack" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + JsEncoder = &JsonEncoder{} |
| 24 | + JsonPbMarshaler = &jsonpb.Marshaler{} |
| 25 | + MpEncoder = &msgpackEncoder{} |
| 26 | +) |
| 27 | + |
| 28 | +type Encoder interface { |
| 29 | + UnmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) |
| 30 | + UnmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) |
| 31 | + UnmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) |
| 32 | + |
| 33 | + MarshalTracesResponse(ptraceotlp.ExportResponse) ([]byte, error) |
| 34 | + MarshalMetricsResponse(pmetricotlp.ExportResponse) ([]byte, error) |
| 35 | + MarshalLogsResponse(plogotlp.ExportResponse) ([]byte, error) |
| 36 | + |
| 37 | + MarshalStatus(rsp *spb.Status) ([]byte, error) |
| 38 | + |
| 39 | + ContentType() string |
| 40 | +} |
| 41 | + |
| 42 | +type JsonEncoder struct{} |
| 43 | + |
| 44 | +func (JsonEncoder) UnmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) { |
| 45 | + req := ptraceotlp.NewExportRequest() |
| 46 | + err := req.UnmarshalJSON(buf) |
| 47 | + return req, err |
| 48 | +} |
| 49 | + |
| 50 | +func (JsonEncoder) UnmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) { |
| 51 | + req := pmetricotlp.NewExportRequest() |
| 52 | + err := req.UnmarshalJSON(buf) |
| 53 | + return req, err |
| 54 | +} |
| 55 | + |
| 56 | +func (JsonEncoder) UnmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) { |
| 57 | + req := plogotlp.NewExportRequest() |
| 58 | + err := req.UnmarshalJSON(buf) |
| 59 | + return req, err |
| 60 | +} |
| 61 | + |
| 62 | +func (JsonEncoder) MarshalTracesResponse(resp ptraceotlp.ExportResponse) ([]byte, error) { |
| 63 | + return resp.MarshalJSON() |
| 64 | +} |
| 65 | + |
| 66 | +func (JsonEncoder) MarshalMetricsResponse(resp pmetricotlp.ExportResponse) ([]byte, error) { |
| 67 | + return resp.MarshalJSON() |
| 68 | +} |
| 69 | + |
| 70 | +func (JsonEncoder) MarshalLogsResponse(resp plogotlp.ExportResponse) ([]byte, error) { |
| 71 | + return resp.MarshalJSON() |
| 72 | +} |
| 73 | + |
| 74 | +func (JsonEncoder) MarshalStatus(resp *spb.Status) ([]byte, error) { |
| 75 | + buf := new(bytes.Buffer) |
| 76 | + err := JsonPbMarshaler.Marshal(buf, resp) |
| 77 | + return buf.Bytes(), err |
| 78 | +} |
| 79 | + |
| 80 | +func (JsonEncoder) ContentType() string { |
| 81 | + return JsonContentType |
| 82 | +} |
| 83 | + |
| 84 | +// messagepack responses seem to work in JSON so leaving this alone for now. |
| 85 | +type msgpackEncoder struct{} |
| 86 | + |
| 87 | +func (msgpackEncoder) UnmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) { |
| 88 | + req := ptraceotlp.NewExportRequest() |
| 89 | + err := req.UnmarshalJSON(buf) |
| 90 | + return req, err |
| 91 | +} |
| 92 | + |
| 93 | +func (msgpackEncoder) UnmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) { |
| 94 | + req := pmetricotlp.NewExportRequest() |
| 95 | + err := req.UnmarshalJSON(buf) |
| 96 | + return req, err |
| 97 | +} |
| 98 | + |
| 99 | +func (msgpackEncoder) UnmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) { |
| 100 | + req := plogotlp.NewExportRequest() |
| 101 | + err := req.UnmarshalJSON(buf) |
| 102 | + return req, err |
| 103 | +} |
| 104 | + |
| 105 | +func (msgpackEncoder) MarshalTracesResponse(resp ptraceotlp.ExportResponse) ([]byte, error) { |
| 106 | + return resp.MarshalJSON() |
| 107 | +} |
| 108 | + |
| 109 | +func (msgpackEncoder) MarshalMetricsResponse(resp pmetricotlp.ExportResponse) ([]byte, error) { |
| 110 | + return resp.MarshalJSON() |
| 111 | +} |
| 112 | + |
| 113 | +func (msgpackEncoder) MarshalLogsResponse(resp plogotlp.ExportResponse) ([]byte, error) { |
| 114 | + return resp.MarshalJSON() |
| 115 | +} |
| 116 | + |
| 117 | +func (msgpackEncoder) MarshalStatus(resp *spb.Status) ([]byte, error) { |
| 118 | + buf := new(bytes.Buffer) |
| 119 | + err := JsonPbMarshaler.Marshal(buf, resp) |
| 120 | + return buf.Bytes(), err |
| 121 | +} |
| 122 | + |
| 123 | +func (msgpackEncoder) ContentType() string { |
| 124 | + return MsgpackContentType |
| 125 | +} |
0 commit comments