-
Notifications
You must be signed in to change notification settings - Fork 17
/
preload.php
executable file
·142 lines (120 loc) · 5.28 KB
/
preload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
60
61
62
63
64
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
use AffordableMobiles\GServerlessSupportLaravel\Integration\ErrorReporting\Report as ErrorBootstrap;
use AffordableMobiles\GServerlessSupportLaravel\Trace\Instrumentation\Guzzle\GuzzleInstrumentation;
use AffordableMobiles\GServerlessSupportLaravel\Trace\Propagator\CloudTracePropagator;
use AffordableMobiles\OpenTelemetry\CloudTrace\SpanExporterFactory;
use App\Trace\InstrumentationLoader;
use Google\Cloud\Storage\StorageClient;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSampler;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProviderBuilder;
require __DIR__.'/helpers.php';
// Load in the Laravel support helpers, for the "env()" function,
// as we may be loading before them, resulting in undefined function errors
// in the Trace initialisation.
$helpers = [
__DIR__.'/../../../laravel/framework/src/Illuminate/Support/helpers.php',
__DIR__.'/../../../laravel/framework/src/Illuminate/Collections/helpers.php',
__DIR__.'/../../../laravel/framework/src/Illuminate/Foundation/helpers.php',
];
foreach ($helpers as $helper) {
if (is_file($helper)) {
require $helper;
}
}
if (is_g_serverless() && (PHP_SAPI !== 'cli')) {
putenv('GOOGLE_CLOUD_BATCH_DAEMON_FAILURE_DIR=false');
if (extension_loaded('opentelemetry')) {
try {
putenv('OTEL_PHP_DETECTORS=none');
require __DIR__.'/AffordableMobiles/GServerlessSupportLaravel/Trace/Propagator/_register.php';
Context::getCurrent()->withContextValue(
Span::wrap(
g_serverless_trace_context(),
),
)->activate();
$instrumentation = new CachedInstrumentation('g-serverless-support-laravel.opentelemetry.low-level');
$loaderInterface = InstrumentationLoader::class;
if (!class_exists($loaderInterface)) {
$loaderInterface = AffordableMobiles\GServerlessSupportLaravel\Trace\InstrumentationLoader::class;
}
$preregisterList = [
GuzzleInstrumentation::class,
];
$instrumentationList = $loaderInterface::getInstrumentation();
foreach ($preregisterList as $pre) {
if (in_array($pre, $instrumentationList, true)) {
$instrumentationList = array_filter($instrumentationList, static fn ($var) => $var !== $pre);
$pre::register($instrumentation);
}
}
$propagator = CloudTracePropagator::getInstance();
$spanProcessor = new SimpleSpanProcessor(
(new SpanExporterFactory())->create(),
);
$sampler = new ParentBased(
new AlwaysOnSampler(),
);
if (!empty($_SERVER['G_SERVERLESS_TRACE_STOP'])) {
$sampler = new AlwaysOffSampler();
} elseif (is_g_serverless_development()) {
$sampler = new AlwaysOnSampler();
}
$tracerProvider = (new TracerProviderBuilder())
->addSpanProcessor($spanProcessor)
->setSampler($sampler)
->build()
;
Sdk::builder()
->setTracerProvider($tracerProvider)
->setPropagator($propagator)
->setAutoShutdown(true)
->buildAndRegisterGlobal()
;
foreach ($instrumentationList as $inst) {
$inst::register($instrumentation);
}
} catch (Throwable $ex) {
ErrorBootstrap::init();
ErrorBootstrap::exceptionHandler($ex, 200);
}
} else {
if (empty($_SERVER['G_SERVERLESS_TRACE_STOP'])) {
g_serverless_basic_log('exception', 'WARNING', 'OpenTelemetry Tracing disabled as module isn\'t loaded. Add "extension=opentelemetry.so" to "php.ini".');
}
}
// Set up exception logging properly...
ErrorBootstrap::init();
// Properly set REMOTE_ADDR from a trustworthy source (hopefully).
$sourceIPHeader = 'HTTP_'.strtoupper(
str_replace(
'-',
'_',
env('SOURCE_IP_HEADER', 'X-AppEngine-User-IP'),
),
);
if (!empty($_SERVER[$sourceIPHeader])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER[$sourceIPHeader];
} elseif (!empty($_SERVER['HTTP_X_APPENGINE_USER_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_APPENGINE_USER_IP'];
} else {
$forwards = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = trim(array_shift($forwards));
}
g_serverless_basic_log('audit', 'INFO', 'Correcting Source IP Address (REMOTE_ADDR) to '.$_SERVER['REMOTE_ADDR'], ['ip_address' => $_SERVER['REMOTE_ADDR']]);
if (!empty($_SERVER['HTTP_X_APPENGINE_HTTPS'])) {
// Turn HTTPS on for Laravel
$_SERVER['HTTPS'] = $_SERVER['HTTP_X_APPENGINE_HTTPS'];
}
$storage = new StorageClient([
'projectId' => g_project(),
]);
$storage->registerStreamWrapper();
}