| 1 | package Plack::Middleware::OpenTelemetry; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | $Plack::Middleware::OpenTelemetry::VERSION = '0.262410'; | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Plack middleware to setup OpenTelemetry tracing | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use v5.30.0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use parent qw(Plack::Middleware); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Plack; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Plack::Util::Accessor qw(resource_attributes include_client_errors); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use OpenTelemetry -all; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use OpenTelemetry::Constants qw( SPAN_KIND_SERVER SPAN_STATUS_ERROR SPAN_STATUS_OK ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use OpenTelemetry::Common 'config'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 14 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 19 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 19 | my ($self, $env) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 19 | my $tracer = | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | otel_tracer_provider->tracer(schema_url => 'https://opentelemetry.io/schemas/1.21.0'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 19 | my $method = $env->{REQUEST_METHOD}; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 19 | ▶ | my $scheme = $env->{HTTP_X_FORWARDED_PROTO} || $env->{"psgi.url_scheme"}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$env->{'HTTP_X_FORWARDED_PROTO'} || $env->{'psgi.url_scheme'}
MC/DC0%0/2
$env->{'HTTP_X_FORWARDED_PROTO'} || $env->{'psgi.url_scheme'}
$env->{'HTTP_X_FORWARDED_PROTO'}
$env->{'psgi.url_scheme'}
Truth table66%2/3
A || B
A= $env->{'HTTP_X_FORWARDED_PROTO'}B= $env->{'psgi.url_scheme'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 19 | my $url = URI->new($scheme . '://' . $env->{HTTP_HOST} . $env->{REQUEST_URI}); | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | my $context = otel_propagator->extract( | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | $env, undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 39 | ▶ | sub ($carrier, $key) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 39 | $carrier->{'HTTP_' . uc $key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 19 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 19 | my $resource; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 19 | ▶ | if (my $a = $self->resource_attributes) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | $resource = OpenTelemetry::SDK::Resource->new() | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | ->merge(OpenTelemetry::SDK::Resource->new(empty => 1, attributes => $a)); | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | my $span = $tracer->create_span( | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | name => "$method request", | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ($resource ? (resource => $resource) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | parent => $context, | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | kind => SPAN_KIND_SERVER, | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | attributes => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | "plack.version" => "$Plack::VERSION", | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # https://opentelemetry.io/docs/specs/semconv/http/http-spans/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # https://opentelemetry.io/blog/2023/http-conventions-declared-stable/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | "client.address" => $env->{REMOTE_ADDR}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | "http.request.method" => $method, | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | "user_agent.original" => ($env->{HTTP_USER_AGENT} || ''), | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | "server.address" => $env->{HTTP_HOST}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 19 | ▶ | "url.full" => $url->as_string, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$env->{'HTTP_USER_AGENT'} || ''
MC/DC100%1/1
$env->{'HTTP_USER_AGENT'} || ''
$env->{'HTTP_USER_AGENT'}
Truth table100%2/2
A
A= $env->{'HTTP_USER_AGENT'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | "url.scheme" => $scheme, | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | "url.path" => $url->path, | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ($url->query ? ("url.query" => $url->query) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # todo: "http.request.body.size" | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 19 | my $previous_context = otel_current_context; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 19 | $context = otel_context_with_span($span, $context); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 19 | otel_current_context = $context; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 19 | try { | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 19 | my $res = eval { $self->app->($env) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 19 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | warn "request returned: $@"; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 18 | ▶ | if (ref($res) && ref($res) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $res and ref $res eq "ARRAY"
MC/DC50%1/2
ref $res and ref $res eq "ARRAY"
ref $res
ref $res eq "ARRAY"
Truth table66%2/3
A and B
A= ref $resB= ref $res eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 17 | $self->set_status_code($span, $res); | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 17 | my $content_length = Plack::Util::content_length($res->[2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 17 | $span->set_attribute("http.response.body.size", $content_length); | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 17 | $span->end(); | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 17 | otel_current_context = $previous_context; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 17 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | return $self->response_cb( | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | $res, | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $res = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $self->set_status_code($span, $res); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $span->set_attribute("plack.callback", "true"); | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $content_length = Plack::Util::content_length($res->[2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | if (defined $content_length) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $span->set_attribute("http.response.body.size", $content_length); | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $span->end(); | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | otel_current_context = $previous_context; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | $content_length = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | my $chunk = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | unless (defined $chunk) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | $span->set_attribute("http.response.body.size", $content_length); | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | $span->end(); | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | $content_length += length($chunk); | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | otel_current_context = $previous_context; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | return $chunk; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | catch ($error) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | warn "got request error: $error"; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | my $message = $error; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | $span->record_exception($error)->set_attribute('http.response.status_code' => 500) | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | ->set_status(SPAN_STATUS_ERROR, $message)->end; | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | otel_current_context = $previous_context; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | die $error; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 18 | ▶ | sub set_status_code ($self, $span, $res) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_status_code: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 18 | ▶ | my $status_code = $res->[0] or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 18 | $span->set_attribute("http.response.status_code", $status_code); | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 18 | ▶ | if ( $status_code >= 400 and $self->include_client_errors | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$status_code >= 400 and $self->include_client_errors or $status_code >= 500
$status_code >= 400 and $self->include_client_errors
MC/DC100%3/3
$status_code >= 400 and $self->include_client_errors or $status_code >= 500
$status_code >= 400
$self->include_client_errors
$status_code >= 500
Truth table80%4/5
A and B or C
A= $status_code >= 400B= $self->include_client_errorsC= $status_code >= 500
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | or $status_code >= 500) | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | $span->set_status(SPAN_STATUS_ERROR); | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | Plack::Middleware::OpenTelemetry - Plack middleware to setup OpenTelemetry spans | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | version 0.262410 | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | builder { | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | enable "Plack::Middleware::OpenTelemetry", | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | include_client_errors => 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # With custom resource attributes | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | builder { | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | enable "Plack::Middleware::OpenTelemetry", | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | include_client_errors => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | resource_attributes => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 'service.version' => '1.0.0', | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 'deployment.environment' => 'production', | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | C<Plack::Middleware::OpenTelemetry> will setup an C<OpenTelemetry> | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | span for the request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | The middleware automatically: | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | =item * Creates OpenTelemetry spans for HTTP requests | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =item * Extracts W3C trace context from incoming requests | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | =item * Sets standard HTTP semantic attributes following OpenTelemetry conventions | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | =item * Handles both synchronous and streaming responses | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | =item * Records exceptions and sets appropriate span status | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | The following HTTP attributes are set on spans: | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | =item * C<http.request.method> - HTTP method | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | =item * C<http.response.status_code> - HTTP status code | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | =item * C<client.address> - Client IP address | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | =item * C<server.address> - Server hostname | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | =item * C<url.full> - Full request URL | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | =item * C<url.scheme> - URL scheme (http/https) | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | =item * C<url.path> - URL path | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | =item * C<url.query> - URL query string (if present) | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | =item * C<user_agent.original> - User-Agent header | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | =item * C<http.response.body.size> - Response body size | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | =head1 PARAMETERS | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | =item include_client_errors | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | By default client errors (HTTP status 400-499) don't set span status to | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | "error". Enable this option to include them as errors. | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | =item resource_attributes | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | Hash reference of custom resource attributes to add to the OpenTelemetry resource. | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | These will be merged with the default resource attributes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | Example: | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | enable "Plack::Middleware::OpenTelemetry", | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | resource_attributes => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 'service.version' => '1.0.0', | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 'deployment.environment' => 'production', | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | =head1 ENVIRONMENT VARIABLES | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | The middleware respects standard OpenTelemetry environment variables: | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | =item OTEL_TRACES_EXPORTER | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | Exporter type (console, otlp, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | =item OTEL_SERVICE_NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | Service name for spans | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | =item OTEL_RESOURCE_ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | Additional resource attributes | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | =head1 NOTES | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | The L<Net::Async::HTTP::Server> plackup server is recommended: | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | C<plackup -s Net::Async::HTTP::Server> | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | L<Plack::Middleware>, L<OpenTelemetry::SDK> | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | Ask Bjørn Hansen <ask@develooper.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | This software is copyright (c) 2023 by Ask Bjørn Hansen. | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | the MIT software license. | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||