Module: Datadog::Tracing::Contrib::Rack::TraceProxyMiddleware
- Defined in:
- lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
Overview
Module to create virtual proxy span
Class Method Summary collapse
- .call(env, configuration, &block) ⇒ Object
-
.call_with_inferred_proxy(env, proxy_type) ⇒ Object
Creates a virtual parent span representing the upstream proxy that wraps the actual request processing.
-
.propagate_request_span_tags(span, env:) ⇒ Object
Propagates response-level and security tags from the request span to the inferred parent.
-
.set_optional_tags(span, env:, proxy_type:) ⇒ Object
Sets cloud provider metadata and constructs the resource ARN.
Class Method Details
.call(env, configuration, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb', line 16 def call(env, configuration, &block) if configuration[:inferred_proxy_enabled] && (proxy_type = env[Ext::HEADER_X_DD_PROXY]) && !proxy_type.empty? return call_with_inferred_proxy(env, proxy_type, &block) end return yield unless configuration[:request_queuing] # parse the request queue time start_time = Contrib::Rack::QueueTime.get_request_start(env) return yield unless start_time = { service: configuration[:web_service_name], start_time: start_time, type: Tracing::Metadata::Ext::HTTP::TYPE_PROXY, } request_span = Tracing.trace(Ext::SPAN_HTTP_PROXY_REQUEST, **) request_span.set_tag(Tracing::Metadata::Ext::TAG_SVC_SRC, Ext::TAG_COMPONENT_HTTP_PROXY) request_span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT_HTTP_PROXY) request_span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_HTTP_PROXY_REQUEST) request_span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_PROXY) queue_span = Tracing.trace(Ext::SPAN_HTTP_PROXY_QUEUE, **) queue_span.set_tag(Tracing::Metadata::Ext::TAG_SVC_SRC, Ext::TAG_COMPONENT_HTTP_PROXY) queue_span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT_HTTP_PROXY) queue_span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_HTTP_PROXY_QUEUE) queue_span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_PROXY) Contrib::Analytics.set_measured(queue_span) # finish the `queue` span now to record only the time spent *in queue*, # excluding the time spent processing the request itself queue_span.finish yield ensure # Ensure that the spans are finished even if an exception is raised. # **This is very important** to prevent the trace from leaking between requests, # especially because `queue_span` is normally a root span. queue_span&.finish request_span&.finish end |
.call_with_inferred_proxy(env, proxy_type) ⇒ Object
Creates a virtual parent span representing the upstream proxy that wraps the actual request processing.
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb', line 65 def call_with_inferred_proxy(env, proxy_type) span_name = Ext::PROXY_SPAN_NAMES[proxy_type] return yield unless span_name request_time_ms = env[Ext::HEADER_X_DD_PROXY_REQUEST_TIME_MS].to_f return yield unless request_time_ms.positive? path = env[Ext::HEADER_X_DD_PROXY_PATH] stage = env[Ext::HEADER_X_DD_PROXY_STAGE] domain = env[Ext::HEADER_X_DD_PROXY_DOMAIN_NAME] http_method = env[Ext::HEADER_X_DD_PROXY_HTTPMETHOD] resource_path = env[Ext::HEADER_X_DD_PROXY_RESOURCE_PATH] # NOTE: resource_path is the parameterized route (e.g. /users/{id}) vs literal path resource = "#{http_method} #{resource_path || path}" if http_method inferred_span = Tracing.trace( span_name, service: domain, type: Tracing::Metadata::Ext::AppTypes::TYPE_WEB, start_time: Time.at(request_time_ms / 1_000), ) inferred_span.resource = resource if resource inferred_span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, proxy_type) inferred_span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_SERVER) inferred_span.set_tag("stage", stage) if stage inferred_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_METHOD, http_method) if http_method inferred_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_URL, "https://#{domain}#{path}") if domain && path inferred_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE, resource_path) if resource_path inferred_span.set_metric(Ext::TAG_INFERRED_SPAN, 1) (inferred_span, env: env, proxy_type: proxy_type) yield # NOTE: The underlying {Rack::TraceMiddleware} rescues {Exception} # to tag the request span with error details. # We must propagate errors to the inferred proxy parent span. rescue Exception => e # rubocop:disable Lint/RescueException inferred_span&.set_error(e) raise ensure if inferred_span (inferred_span, env: env) if (trace = Tracing.active_trace) && resource trace.resource = resource end inferred_span.finish end end |
.propagate_request_span_tags(span, env:) ⇒ Object
Propagates response-level and security tags from the request span to the inferred parent.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb', line 149 def (span, env:) rack_span = env[Ext::RACK_ENV_REQUEST_SPAN] return unless rack_span if (status_code = rack_span.get_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE)) span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, status_code) span.status = rack_span.status end if (user_agent = rack_span.get_tag(Tracing::Metadata::Ext::HTTP::TAG_USER_AGENT)) span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_USER_AGENT, user_agent) end # NOTE: Tracing shouldn't know about AppSec tags. if (appsec_enabled = rack_span.get_metric("_dd.appsec.enabled")) span.set_metric("_dd.appsec.enabled", appsec_enabled) end # NOTE: Tracing shouldn't know about AppSec tags. if (appsec_json = rack_span.get_tag("_dd.appsec.json")) span.set_tag("_dd.appsec.json", appsec_json) end end |
.set_optional_tags(span, env:, proxy_type:) ⇒ Object
Sets cloud provider metadata and constructs the resource ARN.
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb', line 121 def (span, env:, proxy_type:) api_id = env[Ext::HEADER_X_DD_PROXY_API_ID] region = env[Ext::HEADER_X_DD_PROXY_REGION] # API Gateway v1 sends region as a single-quoted string region = region.delete("'") if region span.set_tag("apiid", api_id) if api_id span.set_tag("region", region) if region if (account_id = env[Ext::HEADER_X_DD_PROXY_ACCOUNT_ID]) span.set_tag("account_id", account_id) end if (user = env[Ext::HEADER_X_DD_PROXY_USER]) span.set_tag("aws_user", user) end if api_id && region # NOTE: Update this when adding non-AWS proxy types. restapi_prefix = (proxy_type == Ext::PROXY_AWS_APIGATEWAY) ? "restapis" : "apis" span.set_tag("dd_resource_key", "arn:aws:apigateway:#{region}::/#{restapi_prefix}/#{api_id}") end end |