prober.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. chdir(dirname(__FILE__));
  3. require '../vendor/autoload.php';
  4. // require_once '../Google/Cloud/Firestore/V1beta1/FirestoreClient.php';
  5. // require_once '../Google/Cloud/Spanner/V1/SpannerClient.php';
  6. $firestore_probes = require './firestore_probes.php';
  7. $spanner_probes = require './spanner_probes.php';
  8. require './stackdriver_util.php';
  9. $_OAUTH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform';
  10. $_FIRESTORE_TARGET = 'firestore.googleapis.com:443';
  11. $_SPANNER_TARGET = 'spanner.googleapis.com:443';
  12. use Google\Auth\ApplicationDefaultCredentials;
  13. use Google\Cloud\Firestore\V1beta1\FirestoreGrpcClient;
  14. use Google\Cloud\Spanner\V1\SpannerGrpcClient;
  15. function getArgs(){
  16. $options = getopt('',['api:','extension:']);
  17. return $options;
  18. }
  19. /*
  20. function secureAuthorizedChannel($credentials, $request, $target, $kwargs){
  21. $metadata_plugin = $transport_grpc->AuthMetadataPlugin($credentials, $request);
  22. $ssl_credentials = Grpc\ChannelCredentials::createSsl();
  23. $composit_credentials = $grpc->composite_channel_credentials($ssl_credentials, $google_auth_credentials);
  24. return $grpc_gcp->secure_channel($target, $composit_credentials, $kwargs);
  25. }
  26. function getStubChannel($target){
  27. $res = $auth->default([$_OAUTH_SCOPE]);
  28. $cred = $res[0];
  29. return secureAuthorizedChannel($cred, Request(), $target);
  30. }*/
  31. function executeProbes($api){
  32. global $_OAUTH_SCOPE;
  33. global $_SPANNER_TARGET;
  34. global $_FIRESTORE_TARGET;
  35. global $spanner_probes;
  36. global $firestore_probes;
  37. $util = new StackdriverUtil($api);
  38. $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials($_OAUTH_SCOPE);
  39. $opts = [
  40. 'credentials' => Grpc\ChannelCredentials::createSsl(),
  41. 'update_metadata' => $auth->getUpdateMetadataFunc(),
  42. ];
  43. if($api == 'spanner'){
  44. $client = new SpannerGrpcClient($_SPANNER_TARGET, $opts);
  45. $probe_functions = $spanner_probes;
  46. }
  47. else if($api == 'firestore'){
  48. $client = new FirestoreGrpcClient($_FIRESTORE_TARGET, $opts);
  49. $probe_functions = $firestore_probes;
  50. }
  51. else{
  52. echo 'grpc not implemented for '.$api;
  53. exit(1);
  54. }
  55. $total = sizeof($probe_functions);
  56. $success = 0;
  57. $metrics = [];
  58. # Execute all probes for given api
  59. foreach ($probe_functions as $probe_name => $probe_function) {
  60. try{
  61. $probe_function($client, $metrics);
  62. $success++;
  63. }
  64. catch(Exception $e){
  65. $util->reportError($e);
  66. }
  67. }
  68. if($success == $total){
  69. $util->setSuccess(True);
  70. }
  71. $util->addMetrics($metrics);
  72. $util->outputMetrics();
  73. if($success != $total){
  74. # TODO: exit system
  75. exit(1);
  76. }
  77. }
  78. function main(){
  79. $args = getArgs();
  80. executeProbes($args['api']);
  81. }
  82. main();