Sha256: 173f507fc2ce9e360f3a27f429c7d3ad607505ff5b3617a4b296dbdc781abc41

Contents?: true

Size: 1.56 KB

Versions: 15

Compression:

Stored size: 1.56 KB

Contents

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
      def content_type
        raise NotImplementedError
      end

      # Encodes a list of traces, expecting a list of items where each items
      # is a list of spans. Before dump the string in a serialized format all
      # traces are normalized. The traces nesting is not changed.
      def encode_traces(traces)
        to_send = []
        traces.each do |trace|
          to_send << trace.map(&:to_hash)
        end
        encode(to_send)
      end

      # Encodes services hash
      def encode_services(services)
        encode(services)
      end

      # Defines the underlying format used during traces or services encoding.
      # This method must be implemented and should only be used by the internal functions.
      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
    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
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ddtrace-0.22.0 lib/ddtrace/encoding.rb
ddtrace-0.21.2 lib/ddtrace/encoding.rb
ddtrace-0.21.1 lib/ddtrace/encoding.rb
ddtrace-0.21.0 lib/ddtrace/encoding.rb
ddtrace-0.20.0 lib/ddtrace/encoding.rb
ddtrace-0.19.1 lib/ddtrace/encoding.rb
ddtrace-0.19.0 lib/ddtrace/encoding.rb
ddtrace-0.18.3 lib/ddtrace/encoding.rb
ddtrace-0.18.2 lib/ddtrace/encoding.rb
ddtrace-0.18.1 lib/ddtrace/encoding.rb
ddtrace-0.18.0 lib/ddtrace/encoding.rb
ddtrace-0.17.3 lib/ddtrace/encoding.rb
ddtrace-0.17.2 lib/ddtrace/encoding.rb
ddtrace-0.17.1 lib/ddtrace/encoding.rb
ddtrace-0.17.0 lib/ddtrace/encoding.rb