Sha256: 8c7ca6536f7cc3cc834f028f9db2472a7cd8803ef00a78311fb65fc268983c4f

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

# Copyright 2019 OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

# require_relative 'command_serializer'

module OpenTelemetry
  module Instrumentation
    module ActiveModelSerializers
      # Event handler singleton for ActiveModelSerializers
      module EventHandler
        extend self

        def handle(start_timestamp, end_timestamp, payload)
          tracer.start_span(span_name(payload),
                            start_timestamp: start_timestamp,
                            attributes: build_attributes(payload),
                            kind: :internal)
                .finish(end_timestamp: end_timestamp)
        end

        protected

        def span_name(payload)
          "#{demodulize(payload[:serializer].name)} render"
        end

        def build_attributes(payload)
          {
            'serializer.name' => payload[:serializer].name,
            'serializer.renderer' => 'active_model_serializers',
            'serializer.format' => payload[:adapter]&.class&.name || 'default'
          }
        end

        def tracer
          ActiveModelSerializers::Instrumentation.instance.tracer
        end

        def demodulize(string)
          string = string.to_s
          i = string.rindex('::')
          i ? string[(i + 2)..-1] : string
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-active_model_serializers-0.10.1 lib/opentelemetry/instrumentation/active_model_serializers/event_handler.rb
opentelemetry-instrumentation-active_model_serializers-0.10.0 lib/opentelemetry/instrumentation/active_model_serializers/event_handler.rb
opentelemetry-instrumentation-active_model_serializers-0.9.0 lib/opentelemetry/instrumentation/active_model_serializers/event_handler.rb