Sha256: 32e102a04a7836edd391e1172f06dc6e36849fc68a3dc39a3ccefcfa04e687c0

Contents?: true

Size: 1.84 KB

Versions: 49

Compression:

Stored size: 1.84 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
    class Encoder
      attr_reader :content_type

      # When extending the ``Encoder`` class, ``content_type`` must be set because
      # they're used by the HTTPTransport so that it should not need to know what is
      # the right header to suggest the decoding format to the agent
      def initialize
        @content_type = ''
      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
    class JSONEncoder < Encoder
      def initialize
        Datadog::Tracer.log.debug('using JSON encoder; application performance may be degraded')
        @content_type = 'application/json'
      end

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

    # Encoder for the Msgpack format
    class MsgpackEncoder < Encoder
      def initialize
        Datadog::Tracer.log.debug('using Msgpack encoder')
        @content_type = 'application/msgpack'
      end

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

Version data entries

49 entries across 49 versions & 2 rubygems

Version Path
ddtrace-0.16.1 lib/ddtrace/encoding.rb
fair-ddtrace-0.8.2.a lib/ddtrace/encoding.rb
ddtrace-0.15.0.internaltracinfeature1 lib/ddtrace/encoding.rb
ddtrace-0.16.0 lib/ddtrace/encoding.rb
ddtrace-0.14.2.disableprotocolversion4 lib/ddtrace/encoding.rb
ddtrace-0.15.0 lib/ddtrace/encoding.rb
ddtrace-0.14.2.withoutpriorityparsing1 lib/ddtrace/encoding.rb
ddtrace-0.14.2 lib/ddtrace/encoding.rb
ddtrace-0.14.1 lib/ddtrace/encoding.rb
ddtrace-0.15.0.beta1 lib/ddtrace/encoding.rb
ddtrace-0.14.0 lib/ddtrace/encoding.rb
ddtrace-0.14.0.rc1 lib/ddtrace/encoding.rb
ddtrace-0.13.2 lib/ddtrace/encoding.rb
ddtrace-0.14.0.beta2 lib/ddtrace/encoding.rb
ddtrace-0.14.0.beta1 lib/ddtrace/encoding.rb
ddtrace-0.13.1 lib/ddtrace/encoding.rb
ddtrace-0.13.0 lib/ddtrace/encoding.rb
ddtrace-0.12.1 lib/ddtrace/encoding.rb
ddtrace-0.13.0.beta1 lib/ddtrace/encoding.rb
ddtrace-0.12.0 lib/ddtrace/encoding.rb