marshaler.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package kafkaexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/kafkaexporter"
  4. import (
  5. "github.com/IBM/sarama"
  6. "go.opentelemetry.io/collector/pdata/plog"
  7. "go.opentelemetry.io/collector/pdata/pmetric"
  8. "go.opentelemetry.io/collector/pdata/ptrace"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin/zipkinv2"
  10. )
  11. // TracesMarshaler marshals traces into Message array.
  12. type TracesMarshaler interface {
  13. // Marshal serializes spans into sarama's ProducerMessages
  14. Marshal(traces ptrace.Traces, topic string) ([]*sarama.ProducerMessage, error)
  15. // Encoding returns encoding name
  16. Encoding() string
  17. }
  18. // MetricsMarshaler marshals metrics into Message array
  19. type MetricsMarshaler interface {
  20. // Marshal serializes metrics into sarama's ProducerMessages
  21. Marshal(metrics pmetric.Metrics, topic string) ([]*sarama.ProducerMessage, error)
  22. // Encoding returns encoding name
  23. Encoding() string
  24. }
  25. // LogsMarshaler marshals logs into Message array
  26. type LogsMarshaler interface {
  27. // Marshal serializes logs into sarama's ProducerMessages
  28. Marshal(logs plog.Logs, topic string) ([]*sarama.ProducerMessage, error)
  29. // Encoding returns encoding name
  30. Encoding() string
  31. }
  32. // tracesMarshalers returns map of supported encodings with TracesMarshaler.
  33. func tracesMarshalers() map[string]TracesMarshaler {
  34. otlpPb := newPdataTracesMarshaler(&ptrace.ProtoMarshaler{}, defaultEncoding)
  35. otlpJSON := newPdataTracesMarshaler(&ptrace.JSONMarshaler{}, "otlp_json")
  36. zipkinProto := newPdataTracesMarshaler(zipkinv2.NewProtobufTracesMarshaler(), "zipkin_proto")
  37. zipkinJSON := newPdataTracesMarshaler(zipkinv2.NewJSONTracesMarshaler(), "zipkin_json")
  38. jaegerProto := jaegerMarshaler{marshaler: jaegerProtoSpanMarshaler{}}
  39. jaegerJSON := jaegerMarshaler{marshaler: newJaegerJSONMarshaler()}
  40. return map[string]TracesMarshaler{
  41. otlpPb.Encoding(): otlpPb,
  42. otlpJSON.Encoding(): otlpJSON,
  43. zipkinProto.Encoding(): zipkinProto,
  44. zipkinJSON.Encoding(): zipkinJSON,
  45. jaegerProto.Encoding(): jaegerProto,
  46. jaegerJSON.Encoding(): jaegerJSON,
  47. }
  48. }
  49. // metricsMarshalers returns map of supported encodings and MetricsMarshaler
  50. func metricsMarshalers() map[string]MetricsMarshaler {
  51. otlpPb := newPdataMetricsMarshaler(&pmetric.ProtoMarshaler{}, defaultEncoding)
  52. otlpJSON := newPdataMetricsMarshaler(&pmetric.JSONMarshaler{}, "otlp_json")
  53. return map[string]MetricsMarshaler{
  54. otlpPb.Encoding(): otlpPb,
  55. otlpJSON.Encoding(): otlpJSON,
  56. }
  57. }
  58. // logsMarshalers returns map of supported encodings and LogsMarshaler
  59. func logsMarshalers() map[string]LogsMarshaler {
  60. otlpPb := newPdataLogsMarshaler(&plog.ProtoMarshaler{}, defaultEncoding)
  61. otlpJSON := newPdataLogsMarshaler(&plog.JSONMarshaler{}, "otlp_json")
  62. raw := newRawMarshaler()
  63. return map[string]LogsMarshaler{
  64. otlpPb.Encoding(): otlpPb,
  65. otlpJSON.Encoding(): otlpJSON,
  66. raw.Encoding(): raw,
  67. }
  68. }