Sha256: 273a241e038b0a162cd3654516544b0ff22ac631cec90d8e0dd5ca07802d1f62

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'json'
require 'msgpack'

module Datadog
  module Core
    # Encoding module that encodes data for the AgentTransport
    module Encoding
      # Encoder interface that provides the logic to encode traces and service
      # @abstract
      module Encoder
        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'

        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'

        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
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
datadog-2.7.0 lib/datadog/core/encoding.rb
datadog-2.6.0 lib/datadog/core/encoding.rb
datadog-2.5.0 lib/datadog/core/encoding.rb
datadog-2.4.0 lib/datadog/core/encoding.rb
datadog-2.3.0 lib/datadog/core/encoding.rb
datadog-2.2.0 lib/datadog/core/encoding.rb
datadog-2.1.0 lib/datadog/core/encoding.rb
datadog-2.0.0 lib/datadog/core/encoding.rb
datadog-2.0.0.beta2 lib/datadog/core/encoding.rb
datadog-2.0.0.beta1 lib/datadog/core/encoding.rb