Sha256: 29fed83dfe344ede14547ee5b20309de12ebc24f93b94040ea80235949702ba5

Contents?: true

Size: 1.24 KB

Versions: 28

Compression:

Stored size: 1.24 KB

Contents

# typed: false
# frozen_string_literal: true

require "openapi_first"
require "active_support/parameter_filter"

module PlugApp
  module Middleware
    class TracingAttributes
      extend T::Sig

      sig { returns(T.untyped) }
      attr_reader :app

      HTTP_REQUEST_BODY = "http.request.body"
      PLUG_APP_PATH_PREFIX = "/app/"
      RACK_REQUEST_BODY = "rack.input"

      sig { params(app: T.untyped).void }
      def initialize(app)
        @app = T.let(app, T.untyped)
        @filterer = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
      end

      sig { params(env: T.untyped).returns(T.untyped) }
      def call(env)
        OpenTelemetry::Trace.current_span.add_attributes({
          OpenTelemetry::VERSION => PlugApp::Application::GIT_SHA,
          OpenTelemetry::SemanticConventions::Trace::HTTP_REQUEST_CONTENT_LENGTH => env["CONTENT_LENGTH"].to_i,
          HTTP_REQUEST_BODY => filtered_params(env),
        })

        app.call(env)
      end

      def filtered_params(env)
        body = env[RACK_REQUEST_BODY]&.read
        return "{}" if body.blank? || body == "{}"

        @filterer.filter(JSON.parse(body)).to_json
      ensure
        env[RACK_REQUEST_BODY]&.try(:rewind)
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
hephaestus-0.8.3 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.8.2 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.8.1 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.8.0 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.7.2 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.7 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.6.2 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.6.1 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.6 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.5.3 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.5.2 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.5.1 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.5 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.4.2 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.4.1 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.4 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.3.1 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.3 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.2.5 templates/app/lib/plug_app/middleware/tracing_attributes.rb
hephaestus-0.7.2.4 templates/app/lib/plug_app/middleware/tracing_attributes.rb