Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,45 @@ DEFAULT_PROFILE_IMAGE=
AUTH_PASSWORD_RESET_LIFETIME=1800

AUTH_PASSWORD_SHAPE_PATTERN="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-])"
AUTH_PASSWORD_SHAPE_WARNING="Password must include at least one uppercase letter, one lowercase letter, one number, and one special character."
AUTH_PASSWORD_SHAPE_WARNING="Password must include at least one uppercase letter, one lowercase letter, one number, and one special character."


#Open Telemetry
OTEL_SERVICE_ENABLED=true
OTEL_SERVICE_NAME=idp-api
OTEL_PROPAGATORS=tracecontext,baggage
OTEL_EXPORTER_OTLP_PROTOCOL=http/json # Supported values: "grpc", "http/protobuf", "http/json"
OTEL_EXPORTER_OTLP_MAX_RETRIES=3
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
TRACE_SPAN_PREFIX=SPAN
OTEL_TRACES_SAMPLER_PARENT=false
# OTEL_TRACES_SAMPLER_TYPE=always_on # Supported values: "always_on", "always_off", "traceidratio"
# OTEL_TRACES_SAMPLER_TRACEIDRATIO_RATIO=0.05
# OTEL_METRICS_EXPORTER=otlp
# OTEL_TRACES_EXPORTER=otlp
# OTEL_LOGS_EXPORTER=otlp
# OTEL_EXPORTER_OTLP_TIMEOUT=10000
# OTEL_EXPORTER_OTLP_HEADERS=
# OTEL_EXPORTER_OTLP_TRACES_TIMEOUT=10000
# OTEL_EXPORTER_OTLP_TRACES_HEADERS=
# OTEL_EXPORTER_OTLP_METRICS_TIMEOUT=10000
# OTEL_EXPORTER_OTLP_METRICS_HEADERS=
# OTEL_EXPORTER_OTLP_LOGS_TIMEOUT=10000
# OTEL_EXPORTER_OTLP_LOGS_HEADERS=
# OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://localhost:9411
# OTEL_EXPORTER_ZIPKIN_TIMEOUT=10000
# OTEL_EXPORTER_ZIPKIN_MAX_RETRIES=3
# OTEL_INSTRUMENTATION_HTTP_SERVER=true
# OTEL_INSTRUMENTATION_HTTP_CLIENT=true
# OTEL_INSTRUMENTATION_QUERY=true
# OTEL_INSTRUMENTATION_REDIS=true
# OTEL_INSTRUMENTATION_QUEUE=true
# OTEL_INSTRUMENTATION_CACHE=true
# OTEL_INSTRUMENTATION_VIEW=true
# OTEL_INSTRUMENTATION_LIVEWIRE=true
# OTEL_INSTRUMENTATION_CONSOLE=true

# SWAGGER CONFIG

L5_SWAGGER_CONST_HOST=${APP_URL}
L5_SWAGGER_CONST_AUTH_URL=${IDP_AUTHORIZATION_ENDPOINT}
86 changes: 86 additions & 0 deletions app/Http/Middleware/TrackRequestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use Illuminate\Log\LogManager;
use OpenTelemetry\API\Baggage\Baggage;
use OpenTelemetry\Context\ScopeInterface;

class TrackRequestMiddleware
{
private const ATTRIBUTE_START_TIME = '_start_time';
private const EVENT_REQUEST_STARTED = 'request.started';
private const EVENT_REQUEST_FINISHED = 'request.finished';

protected LogManager $logger;
private ?ScopeInterface $baggageScope = null;
private bool $shouldTrack;

public function __construct(LogManager $logger)
{
$this->logger = $logger;
$this->shouldTrack = env('OTEL_SERVICE_ENABLED', false);
}

public function handle(Request $request, Closure $next)
{
if (!$this->shouldTrack) {
return $next($request);
}

try {
$request->attributes->set(self::ATTRIBUTE_START_TIME, microtime(true));
if ($span = Tracer::activeSpan()) {
if ($ray = $request->header('cf-ray')) {
$span->setAttribute('cloudflare.cf-ray', $ray);

$baggage = Baggage::getCurrent()
->toBuilder()
->set('cf-ray', $ray)
->set('user_agent', substr($request->userAgent() ?? 'unknown', 0, 100))
->build();

$this->baggageScope = $baggage->activate();
}

$span->addEvent(self::EVENT_REQUEST_STARTED, [
'method' => $request->method(),
'url' => $request->fullUrl(),
]);
}
} catch (\Throwable $e) {
$this->logger->channel('daily')->error("Error on request tracking: " . $e->getMessage());
}

return $next($request);
}

public function terminate(Request $request, Response $response): void
{
if (!$this->shouldTrack) {
return;
}

try {
$start = (float) $request->attributes->get(self::ATTRIBUTE_START_TIME, microtime(true));
$ms = (int) ((microtime(true) - $start) * 1000);

if ($span = Tracer::activeSpan()) {
$span->setAttribute('app.response_ms', $ms);
$span->setAttribute('http.status_code', $response->getStatusCode());
$span->addEvent(self::EVENT_REQUEST_FINISHED, ['response_ms' => $ms]);
}
} catch (\Throwable $e) {
$this->logger->channel('daily')->error("Error on request tracking: " . $e->getMessage());
} finally {
if ($this->baggageScope) {
$this->baggageScope->detach();
$this->baggageScope = null;
}
}
}
}
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"guzzlehttp/uri-template": "^1.0",
"ircmaxell/random-lib": "1.2.0",
"jenssegers/agent": "2.6.3",
"keepsuit/laravel-opentelemetry": "^1.14",
"laminas/laminas-crypt": "3.11.0",
"laminas/laminas-math": "3.7.0",
"laravel-doctrine/extensions": "2.0.1",
Expand Down Expand Up @@ -69,9 +70,9 @@
"phpunit/phpunit": "^11.0.1",
"rector/rector": "^2.0"
},
"suggest":{
"suggest": {
"lib-openssl": "Required to use AES algorithms (except AES GCM)",
"ext-json":"Required to use json algorithms"
"ext-json": "Required to use json algorithms"
},
"autoload": {
"classmap": [
Expand All @@ -88,7 +89,10 @@
"Utils\\": "app/libs/Utils/",
"Models\\": "app/Models/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
"Database\\Seeders\\": "database/seeders/",
"OpenTelemetry\\SemConv\\Unstable\\Metrics\\": "vendor/open-telemetry/sem-conv/Incubating/Metrics/",
"OpenTelemetry\\": "vendor/open-telemetry/",
"OpenTelemetry\\API\\": "vendor/open-telemetry/api/"
},
"files": [
"app/Utils/helpers.php",
Expand All @@ -102,8 +106,7 @@
},
"extra": {
"laravel": {
"dont-discover": [
]
"dont-discover": []
}
},
"scripts": {
Expand All @@ -128,7 +131,9 @@
"sort-packages": true,
"optimize-autoloader": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
"composer/package-versions-deprecated": true,
"php-http/discovery": true,
"tbachert/spi": true
}
},
"minimum-stability": "dev",
Expand Down
Loading
Loading