|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package otlpjson |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + |
| 20 | + "github.com/gogo/protobuf/jsonpb" |
| 21 | + |
| 22 | + otlpcollectorlogs "go.opentelemetry.io/collector/internal/data/protogen/collector/logs/v1" |
| 23 | + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" |
| 24 | + otlpcollectortrace "go.opentelemetry.io/collector/internal/data/protogen/collector/trace/v1" |
| 25 | + "go.opentelemetry.io/collector/internal/model/serializer" |
| 26 | +) |
| 27 | + |
| 28 | +type marshaler struct { |
| 29 | + delegate jsonpb.Marshaler |
| 30 | +} |
| 31 | + |
| 32 | +// NewTracesMarshaler returns a serializer.TracesUnmarshaler to marshal to OTLP json bytes. |
| 33 | +func NewTracesMarshaler() serializer.TracesMarshaler { |
| 34 | + return &marshaler{delegate: jsonpb.Marshaler{}} |
| 35 | +} |
| 36 | + |
| 37 | +// NewMetricsMarshaler returns a serializer.MetricsMarshaler to marshal to OTLP json bytes. |
| 38 | +func NewMetricsMarshaler() serializer.MetricsMarshaler { |
| 39 | + return &marshaler{delegate: jsonpb.Marshaler{}} |
| 40 | +} |
| 41 | + |
| 42 | +// NewLogsMarshaler returns a serializer.LogsMarshaler to marshal to OTLP json bytes. |
| 43 | +func NewLogsMarshaler() serializer.LogsMarshaler { |
| 44 | + return &marshaler{delegate: jsonpb.Marshaler{}} |
| 45 | +} |
| 46 | + |
| 47 | +func (en *marshaler) MarshalLogs(model interface{}) ([]byte, error) { |
| 48 | + buf := bytes.Buffer{} |
| 49 | + if err := en.delegate.Marshal(&buf, model.(*otlpcollectorlogs.ExportLogsServiceRequest)); err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + return buf.Bytes(), nil |
| 53 | +} |
| 54 | + |
| 55 | +func (en *marshaler) MarshalMetrics(model interface{}) ([]byte, error) { |
| 56 | + buf := bytes.Buffer{} |
| 57 | + if err := en.delegate.Marshal(&buf, model.(*otlpcollectormetrics.ExportMetricsServiceRequest)); err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return buf.Bytes(), nil |
| 61 | +} |
| 62 | + |
| 63 | +func (en *marshaler) MarshalTraces(model interface{}) ([]byte, error) { |
| 64 | + buf := bytes.Buffer{} |
| 65 | + if err := en.delegate.Marshal(&buf, model.(*otlpcollectortrace.ExportTraceServiceRequest)); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + return buf.Bytes(), nil |
| 69 | +} |
0 commit comments