Sha256: 3415d949fa4332da538d8ec6ff539db57f6b57a07592d488f3c3cf08bd2fecc8

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

# typed: true
require 'json'
require 'msgpack'

module Datadog
  # Encoding module that encodes data for the AgentTransport
  module Encoding
    # Encoder interface that provides the logic to encode traces and service
    module Encoder
      include Kernel # Ensure that kernel methods are always available (https://sorbet.org/docs/error-reference#7003)

      def content_type
        raise NotImplementedError
      end

      # Concatenates a list of elements previously encoded by +#encode+.
      def join(encoded_elements)
        raise NotImplementedError
      end

      # Serializes a single trace into a String suitable for network transmission.
      def encode(_)
        raise NotImplementedError
      end
    end

    # Encoder for the JSON format
    module JSONEncoder
      extend Encoder

      CONTENT_TYPE = 'application/json'.freeze

      module_function

      def content_type
        CONTENT_TYPE
      end

      def encode(obj)
        JSON.dump(obj)
      end

      def join(encoded_data)
        "[#{encoded_data.join(',')}]"
      end
    end

    # Encoder for the Msgpack format
    module MsgpackEncoder
      extend Encoder

      module_function

      CONTENT_TYPE = 'application/msgpack'.freeze

      def content_type
        CONTENT_TYPE
      end

      def encode(obj)
        MessagePack.pack(obj)
      end

      def join(encoded_data)
        packer = MessagePack::Packer.new
        packer.write_array_header(encoded_data.size)

        (packer.buffer.to_a + encoded_data).join
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.54.2 lib/ddtrace/encoding.rb
ddtrace-0.54.1 lib/ddtrace/encoding.rb
ddtrace-0.54.0 lib/ddtrace/encoding.rb
ddtrace-0.53.0 lib/ddtrace/encoding.rb
ddtrace-0.52.0 lib/ddtrace/encoding.rb