Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger: Register Debuggee with labels #883

Merged
merged 6 commits into from
Feb 6, 2018
Merged
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
35 changes: 32 additions & 3 deletions src/Debugger/Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [])
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
17 changes: 13 additions & 4 deletions src/Debugger/Debuggee.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand All @@ -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'];
Expand All @@ -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'];
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -374,5 +379,9 @@ public function jsonSerialize()
}, $this->extSourceContexts),
'extSourceContexts' => $this->extSourceContexts
];
if (!empty($this->labels)) {
$info['labels'] = $this->labels;
}
return $info;
}
}
22 changes: 22 additions & 0 deletions tests/unit/Debugger/DaemonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
]);
}
}