diff --git a/src/Debugger/Daemon.php b/src/Debugger/Daemon.php index f7f2f5e99527..326733ecb569 100644 --- a/src/Debugger/Daemon.php +++ b/src/Debugger/Daemon.php @@ -17,6 +17,8 @@ namespace Google\Cloud\Debugger; +use Google\Cloud\Core\Report\MetadataProviderInterface; +use Google\Cloud\Core\Report\MetadataProviderUtils; use Google\Cloud\Core\Exception\ConflictException; use Google\Cloud\Debugger\BreakpointStorage\BreakpointStorageInterface; use Google\Cloud\Debugger\BreakpointStorage\SysvBreakpointStorage; @@ -67,10 +69,17 @@ class Daemon * debuggee. **Defaults to** a value autodetected from the * environment. * @type string $description A display name for the debuggee in the - * Stackdriver Debugger UI. **Defaults to** the uniquifier. + * Stackdriver Debugger UI. **Defaults to** a value detected + * from the environment. * @type BreakpointStorageInterface $storage The breakpoint storage * mechanism to use. **Defaults to** a new SysvBreakpointStorage * instance. + * @type array $labels A set of custom debuggee properties, populated + * by the agent, to be displayed to the user. **Defaults to** + * labels detected from the environment. + * @type MetadataProviderInterface $metadataProvider **Defaults to** An + * automatically chosen provider, based on detected environment + * settings. * } */ public function __construct($sourceRoot, array $options = []) @@ -83,7 +92,9 @@ public function __construct($sourceRoot, array $options = []) 'sourceContext' => [], 'extSourceContext' => [], 'uniquifier' => null, - 'description' => null + 'description' => null, + 'labels' => null, + 'metadataProvider' => null ]; $this->sourceRoot = realpath($sourceRoot); @@ -98,11 +109,13 @@ public function __construct($sourceRoot, array $options = []) $uniquifier = $options['uniquifier'] ?: $this->defaultUniquifier(); $description = $options['description'] ?: $this->defaultDescription(); + $labels = $options['labels'] ?: $this->defaultLabels($options['metadataProvider']); $this->debuggee = $client->debuggee(null, [ 'uniquifier' => $uniquifier, 'description' => $description, - 'extSourceContexts' => $extSourceContext ? [$extSourceContext] : [] + 'extSourceContexts' => $extSourceContext ? [$extSourceContext] : [], + 'labels' => $labels ]); $this->debuggee->register(); @@ -193,4 +206,20 @@ private function defaultSourceContext() } return []; } + + private function defaultLabels(MetadataProviderInterface $metadataProvider = null) + { + $metadataProvider = $metadataProvider ?: MetadataProviderUtils::autoSelect($_SERVER); + $labels = []; + if ($metadataProvider->projectId()) { + $labels['projectid'] = $metadataProvider->projectId(); + } + if ($metadataProvider->serviceId()) { + $labels['module'] = $metadataProvider->serviceId(); + } + if ($metadataProvider->versionId()) { + $labels['version'] = $metadataProvider->versionId(); + } + return $labels; + } } diff --git a/src/Debugger/Debuggee.php b/src/Debugger/Debuggee.php index 041a5e0c30e3..a460a8680b86 100644 --- a/src/Debugger/Debuggee.php +++ b/src/Debugger/Debuggee.php @@ -130,8 +130,11 @@ class Debuggee implements \JsonSerializable * user about this debuggee. Absence of this field indicates no * status. The message can be either informational or an error * status. - * @type ExtendedSourceContext[] $extSourceContexts References to the locations and - * revisions of the source code used in the deployed application. + * @type ExtendedSourceContext[] $extSourceContexts References to the + * locations and revisions of the source code used in the + * deployed application. + * @type array $labels A set of custom debuggee properties, populated + * by the agent, to be displayed to the user. * } */ public function __construct(ConnectionInterface $connection, array $info = []) @@ -145,7 +148,8 @@ public function __construct(ConnectionInterface $connection, array $info = []) 'status' => null, 'extSourceContexts' => [], 'uniquifier' => null, - 'description' => null + 'description' => null, + 'labels' => [] ]; $this->id = $info['id']; @@ -156,6 +160,7 @@ public function __construct(ConnectionInterface $connection, array $info = []) $this->agentVersion = $info['agentVersion']; $this->isInactive = $info['isInactive']; $this->extSourceContexts = $info['extSourceContexts']; + $this->labels = $info['labels']; } /** @@ -361,7 +366,7 @@ public function updateBreakpointBatch(array $breakpoints, array $options = []) */ public function jsonSerialize() { - return [ + $info = [ 'id' => $this->id, 'project' => $this->project, 'uniquifier' => $this->uniquifier, @@ -374,5 +379,9 @@ public function jsonSerialize() }, $this->extSourceContexts), 'extSourceContexts' => $this->extSourceContexts ]; + if (!empty($this->labels)) { + $info['labels'] = $this->labels; + } + return $info; } } diff --git a/tests/unit/Debugger/DaemonTest.php b/tests/unit/Debugger/DaemonTest.php index bf93b114f22a..af82fa07ddf8 100644 --- a/tests/unit/Debugger/DaemonTest.php +++ b/tests/unit/Debugger/DaemonTest.php @@ -17,6 +17,7 @@ namespace Google\Cloud\Tests\Unit\Debugger; +use Google\Cloud\Core\Report\SimpleMetadataProvider; use Google\Cloud\Debugger\Breakpoint; use Google\Cloud\Debugger\BreakpointStorage\BreakpointStorageInterface; use Google\Cloud\Debugger\Daemon; @@ -157,4 +158,25 @@ public function testFetchesBreakpoints() ]); $daemon->run(); } + + public function testDetectsLabelsFromEnvironment() + { + $provider = new SimpleMetadataProvider([], 'project1', 'service1', 'version1'); + $expectedLabels = [ + 'module' => 'service1', + 'projectid' => 'project1', + 'version' => 'version1' + ]; + $this->debuggee->register(Argument::any()) + ->shouldBeCalled(); + $this->client->debuggee(null, Argument::withEntry('labels', $expectedLabels)) + ->willReturn($this->debuggee->reveal()) + ->shouldBeCalled(); + + $daemon = new Daemon('.', [ + 'metadataProvider' => $provider, + 'client' => $this->client->reveal(), + 'storage' => $this->storage->reveal() + ]); + } }