|
| 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 | + "go.opentelemetry.io/collector/internal/model/translator" |
| 27 | +) |
| 28 | + |
| 29 | +type marshaler struct { |
| 30 | + delegate jsonpb.Marshaler |
| 31 | +} |
| 32 | + |
| 33 | +// NewTracesMarshaler returns a serializer.TracesUnmarshaler to marshal to OTLP json bytes. |
| 34 | +func NewTracesMarshaler() serializer.TracesMarshaler { |
| 35 | + return &marshaler{delegate: jsonpb.Marshaler{}} |
| 36 | +} |
| 37 | + |
| 38 | +// NewMetricsMarshaler returns a serializer.MetricsMarshaler to marshal to OTLP json bytes. |
| 39 | +func NewMetricsMarshaler() serializer.MetricsMarshaler { |
| 40 | + return &marshaler{delegate: jsonpb.Marshaler{}} |
| 41 | +} |
| 42 | + |
| 43 | +// NewLogsMarshaler returns a serializer.LogsMarshaler to marshal to OTLP json bytes. |
| 44 | +func NewLogsMarshaler() serializer.LogsMarshaler { |
| 45 | + return &marshaler{delegate: jsonpb.Marshaler{}} |
| 46 | +} |
| 47 | + |
| 48 | +func (en *marshaler) MarshalLogs(model interface{}) ([]byte, error) { |
| 49 | + ld, ok := model.(*otlpcollectorlogs.ExportLogsServiceRequest) |
| 50 | + if !ok { |
| 51 | + return nil, translator.NewErrIncompatibleType(&otlpcollectorlogs.ExportLogsServiceRequest{}, model) |
| 52 | + } |
| 53 | + buf := bytes.Buffer{} |
| 54 | + if err := en.delegate.Marshal(&buf, ld); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + return buf.Bytes(), nil |
| 58 | +} |
| 59 | + |
| 60 | +func (en *marshaler) MarshalMetrics(model interface{}) ([]byte, error) { |
| 61 | + md, ok := model.(*otlpcollectormetrics.ExportMetricsServiceRequest) |
| 62 | + if !ok { |
| 63 | + return nil, translator.NewErrIncompatibleType(&otlpcollectormetrics.ExportMetricsServiceRequest{}, model) |
| 64 | + } |
| 65 | + buf := bytes.Buffer{} |
| 66 | + if err := en.delegate.Marshal(&buf, md); err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + return buf.Bytes(), nil |
| 70 | +} |
| 71 | + |
| 72 | +func (en *marshaler) MarshalTraces(model interface{}) ([]byte, error) { |
| 73 | + td, ok := model.(*otlpcollectortrace.ExportTraceServiceRequest) |
| 74 | + if !ok { |
| 75 | + return nil, translator.NewErrIncompatibleType(&otlpcollectortrace.ExportTraceServiceRequest{}, model) |
| 76 | + } |
| 77 | + buf := bytes.Buffer{} |
| 78 | + if err := en.delegate.Marshal(&buf, td); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + return buf.Bytes(), nil |
| 82 | +} |
0 commit comments