-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make the app run via docker compose * Add basic DB monitoring * Refactoring
- Loading branch information
1 parent
442304c
commit 2307d0e
Showing
8 changed files
with
472 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
}, | ||
"autoload": { | ||
"psr-4": { | ||
"App\\": "src/" | ||
"Last9\\": "last9/" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,92 @@ | ||
<?php | ||
require __DIR__ . '/vendor/autoload.php'; | ||
require __DIR__ . '/instrumentation.php'; | ||
require __DIR__ . '/last9/instrumentation.php'; | ||
|
||
// Simple router | ||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | ||
// Use our DB class instead of PDO directly | ||
$pdo = \Last9\DB::connect( | ||
"mysql:host=" . getenv('DB_HOST') . ";dbname=" . getenv('DB_NAME'), | ||
getenv('DB_USER'), | ||
getenv('DB_PASSWORD') | ||
); | ||
|
||
// Create table if not exists | ||
try { | ||
$pdo->exec("CREATE TABLE IF NOT EXISTS dice_rolls ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
roll_value INT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
)"); | ||
} catch (\Exception $e) { | ||
error_log("Error creating table: " . $e->getMessage()); | ||
} | ||
|
||
// Get HTTP method and create operation name | ||
$method = $_SERVER['REQUEST_METHOD']; | ||
$operationName = $method . ' ' . $uri; | ||
// Initialize instrumented HTTP client | ||
$http = new \Last9\InstrumentedHttpClient([ | ||
'timeout' => 5, | ||
'connect_timeout' => 2, | ||
'verify' => false // Added to handle HTTPS issues | ||
]); | ||
|
||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | ||
|
||
// Route handling | ||
switch ($uri) { | ||
case '/': | ||
echo "Hello World!"; | ||
// Instrument the HTTP request with path-based operation name | ||
instrumentHTTPRequest($operationName, []); | ||
break; | ||
try { | ||
switch ($uri) { | ||
case '/': | ||
echo "Hello World!"; | ||
break; | ||
|
||
case '/rolldice': | ||
$result = random_int(1, 6); | ||
|
||
// Instrument the HTTP request with path-based operation name | ||
instrumentHTTPRequest($operationName, ['result' => strval($result)]); | ||
|
||
echo "Rolled dice result: $result"; | ||
break; | ||
case '/rolldice': | ||
$result = random_int(1, 6); | ||
|
||
// Insert new roll | ||
$stmt = $pdo->prepare("INSERTT INTO dice_rolls (roll_value) VALUES (?)"); | ||
$stmt->execute([$result]); | ||
|
||
// Get last 5 rolls with error logging | ||
try { | ||
$stmt = $pdo->prepare("SELECT roll_value, created_at FROM dice_rolls ORDER BY created_at DESC LIMIT 5"); | ||
$stmt->execute(); | ||
$previousRolls = $stmt->fetchAll(\PDO::FETCH_ASSOC); | ||
// error_log("Previous rolls: " . print_r($previousRolls, true)); | ||
} catch (\Exception $e) { | ||
error_log("Error fetching previous rolls: " . $e->getMessage()); | ||
$previousRolls = []; | ||
} | ||
|
||
// Make external API call with error handling | ||
try { | ||
$response = $http->request('GET', "http://numbersapi.com/{$result}/math", [ | ||
'headers' => [ | ||
'Accept' => 'text/plain', | ||
'User-Agent' => 'PHP/1.0' | ||
], | ||
'verify' => false, // Disable SSL verification for testing | ||
'timeout' => 30 | ||
]); | ||
$numberFact = $response->getBody(); | ||
error_log("Number fact API response: " . $numberFact); | ||
} catch (\Exception $e) { | ||
error_log("Error fetching number fact: " . $e->getMessage() . "\n" . $e->getTraceAsString()); | ||
$numberFact = "Could not fetch fact due to: " . $e->getMessage(); | ||
} | ||
|
||
$response = [ | ||
'current_roll' => $result, | ||
'fact' => "test", | ||
'previous_rolls' => $previousRolls | ||
]; | ||
|
||
// error_log("Sending response: " . print_r($response, true)); | ||
echo json_encode($response, JSON_PRETTY_PRINT); | ||
break; | ||
|
||
default: | ||
header("HTTP/1.0 404 Not Found"); | ||
instrumentHTTPRequest($operationName, ['result' => strval($result)]); | ||
echo "404 Not Found"; | ||
break; | ||
default: | ||
http_response_code(404); | ||
echo "404 Not Found"; | ||
break; | ||
} | ||
} catch (Exception $e) { | ||
error_log("Main error: " . $e->getMessage()); | ||
http_response_code(500); | ||
echo "Error: " . $e->getMessage(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
namespace Last9; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
class InstrumentedHttpClient { | ||
private $client; | ||
|
||
public function __construct(array $config = []) { | ||
$this->client = new Client($config); | ||
} | ||
|
||
public function request($method, $uri, array $options = []) { | ||
$spanData = \Last9\createSpan( | ||
'http.client', | ||
\Last9\Instrumentation::getRootSpanId(), | ||
[ | ||
['key' => 'http.method', 'value' => ['stringValue' => $method]], | ||
['key' => 'http.url', 'value' => ['stringValue' => $uri]], | ||
['key' => 'http.flavor', 'value' => ['stringValue' => '1.1']], | ||
['key' => 'network.protocol.name', 'value' => ['stringValue' => 'http']], | ||
['key' => 'network.protocol.version', 'value' => ['stringValue' => '1.1']] | ||
] | ||
); | ||
|
||
try { | ||
$response = $this->client->request($method, $uri, $options); | ||
\Last9\endSpan($spanData, | ||
['code' => 1], | ||
[ | ||
['key' => 'http.status_code', 'value' => ['intValue' => $response->getStatusCode()]], | ||
['key' => 'http.response.body.size', 'value' => ['intValue' => strlen($response->getBody())]] | ||
] | ||
); | ||
return $response; | ||
} catch (\Exception $e) { | ||
\Last9\endSpan($spanData, | ||
['code' => 2, 'message' => $e->getMessage()], | ||
[['key' => 'error.message', 'value' => ['stringValue' => $e->getMessage()]]] | ||
); | ||
throw $e; | ||
} | ||
} | ||
} |
Oops, something went wrong.