Sha256: 369d5587f5224ba3087a9fa685b780c949c04149abc2a8b9e591cdfca045c279

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  module Transport
    module Serializers
      # @api private
      class SpanSerializer < Serializer
        def initialize(config)
          super

          @context_serializer = ContextSerializer.new(config)
        end

        attr_reader :context_serializer

        # rubocop:disable Metrics/MethodLength
        def build(span)
          {
            span: {
              id: span.id,
              transaction_id: span.transaction_id,
              parent_id: span.parent_id,
              name: keyword_field(span.name),
              type: join_type(span),
              duration: ms(span.duration),
              context: context_serializer.build(span.context),
              stacktrace: span.stacktrace.to_a,
              timestamp: span.timestamp,
              trace_id: span.trace_id
            }
          }
        end
        # rubocop:enable Metrics/MethodLength

        # @api private
        class ContextSerializer < Serializer
          def build(context)
            return unless context

            { sync: context.sync }.tap do |base|
              base[:db] = build_db(context.db) if context.db
              base[:http] = build_http(context.http) if context.http
            end
          end

          private

          def build_db(db)
            return unless db

            {
              instance: db.instance,
              statement: Util.truncate(db.statement, max_length: 10_000),
              type: db.type,
              user: db.user
            }
          end

          def build_http(http)
            return unless http

            {
              url: http.url,
              status_code: http.status_code.to_i,
              method: keyword_field(http.method)
            }
          end
        end

        private

        def join_type(span)
          combined = [span.type, span.subtype, span.action]
          combined.compact!
          combined.join '.'
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
elastic-apm-3.1.0 lib/elastic_apm/transport/serializers/span_serializer.rb
elastic-apm-3.0.0 lib/elastic_apm/transport/serializers/span_serializer.rb
elastic-apm-2.12.0 lib/elastic_apm/transport/serializers/span_serializer.rb
elastic-apm-2.11.0 lib/elastic_apm/transport/serializers/span_serializer.rb