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

feat: [Logging] Add resource labels to logs from Cloud Run #7642

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
144 changes: 144 additions & 0 deletions Core/src/Report/CloudRunJobMetadataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* Copyright 2023 Google Inc. All Rights Reserved.
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core\Report;

use Google\Cloud\Core\Compute\Metadata;

/**
* A MetadataProvider for Cloud Run Jobs.
*/
class CloudRunJobMetadataProvider implements MetadataProviderInterface
{
/**
* @var Metadata
*/
private $metadata;

/**
* @var string
*/
private $serviceId;

/**
* @var string
*/
private $revisionId;

/**
* @var string
*/
private $taskIndex;

/**
* @var string
*/
private $taskAttempt;

/**
* @var string
*/
private $instanceId;

/**
* @var string
*/
private $traceId;

/**
* @var string
*/
private $region;

/**
* @see https://cloud.google.com/run/docs/container-contract#jobs-env-vars
* @param array $env
*/
public function __construct(array $env, ?Metadata $metadata = null)
{
$this->serviceId = $env['CLOUD_RUN_JOB'] ?? 'unknown-job';
$this->revisionId = $env['CLOUD_RUN_EXECUTION'] ?? '';
$this->taskIndex = $env['CLOUD_RUN_TASK_INDEX'] ?? '';
$this->taskAttempt = $env['CLOUD_RUN_TASK_ATTEMPT'] ?? '';

$this->traceId = isset($env['HTTP_X_CLOUD_TRACE_CONTEXT'])
? \substr($env['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32)
: null;

$this->metadata = $metadata ?? new Metadata();
$this->region = \basename($this->metadata->get('instance/region'));
$this->instanceId = $this->metadata->get('instance/id');
}

/**
* @return array
*/
public function monitoredResource()
{
return [
'type' => 'cloud_run_job',
'labels' => [
'job_name' => $this->serviceId,
'location' => $this->region,
'project_id' => $this->projectId(),
],
];
}

/**
* Return the project id.
* @return string
*/
public function projectId()
{
return $this->metadata->getProjectId();
}

/**
* Return the service id.
* @return string
*/
public function serviceId()
{
return $this->serviceId;
}

/**
* Return the version id.
* @return string
*/
public function versionId()
{
return $this->revisionId;
}

/**
* Return the labels.
* @return array
*/
public function labels()
{
$labels = [
'instanceId' => $this->instanceId,
'run.googleapis.com/execution_name' => $this->revisionId,
'run.googleapis.com/task_attempt' => $this->taskAttempt,
'run.googleapis.com/task_index' => $this->taskIndex,
'run.googleapis.com/trace_id' => $this->traceId,
];
return \array_filter($labels);
}
}
86 changes: 2 additions & 84 deletions Core/src/Report/CloudRunMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,91 +17,9 @@

namespace Google\Cloud\Core\Report;

use Google\Cloud\Core\Compute\Metadata;

/**
* A MetadataProvider for Cloud Run.
* @deprecated Use \Google\Cloud\Core\Report\CloudRunServiceMetadataProvider instead
*/
class CloudRunMetadataProvider implements MetadataProviderInterface
class CloudRunMetadataProvider extends CloudRunServiceMetadataProvider
{
/**
* @var Metadata
*/
private $metadata;

/**
* @var string
*/
private $serviceId;

/**
* @var string
*/
private $revisionId;

/**
* @var string
*/
private $traceId;

public function __construct(array $env)
{
$this->serviceId = isset($env['K_SERVICE'])
? $env['K_SERVICE']
: 'unknown-service';
$this->revisionId = isset($env['K_REVISION'])
? $env['K_REVISION']
: 'unknown-revision';
$this->traceId = isset($env['HTTP_X_CLOUD_TRACE_CONTEXT'])
? substr($env['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32)
: null;
$this->metadata = new Metadata();
}

/**
* not implemented
* @TODO
*/
public function monitoredResource()
{
return [];
}

/**
* not implemented
* @TODO
*/
public function projectId()
{
return $this->metadata->getProjectId();
}

/**
* Return the service id.
* @return string
*/
public function serviceId()
{
return $this->serviceId;
}

/**
* Return the version id.
* @return string
*/
public function versionId()
{
return $this->revisionId;
}

/**
* Return the labels. We need to evaluate $_SERVER for each request.
* @return array
*/
public function labels()
{
return !empty($this->traceId)
? ['run.googleapis.com/trace_id' => $this->traceId ]
: [];
}
}
138 changes: 138 additions & 0 deletions Core/src/Report/CloudRunServiceMetadataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Copyright 2023 Google Inc. All Rights Reserved.
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core\Report;

use Google\Cloud\Core\Compute\Metadata;

/**
* A MetadataProvider for Cloud Run Services.
*/
class CloudRunServiceMetadataProvider implements MetadataProviderInterface
{
/**
* @var Metadata
*/
private $metadata;

/**
* @var string
*/
private $configurationId;

/**
* @var string
*/
private $serviceId;

/**
* @var string
*/
private $revisionId;

/**
* @var string
*/
private $instanceId;

/**
* @var string
*/
private $traceId;

/**
* @var string
*/
private $region;

/**
* @see https://cloud.google.com/run/docs/container-contract#services-env-vars
* @param array $env
*/
public function __construct(array $env, ?Metadata $metadata = null)
{
$this->configurationId = $env['K_CONFIGURATION'] ?? 'unknown-configuration';
$this->serviceId = $env['K_SERVICE'] ?? 'unknown-service';
$this->revisionId = $env['K_REVISION'] ?? 'unknown-revision';
$this->traceId = isset($env['HTTP_X_CLOUD_TRACE_CONTEXT'])
? \substr($env['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32)
: null;

$this->metadata = $metadata ?? new Metadata();
$this->region = \basename($this->metadata->get('instance/region'));
$this->instanceId = $this->metadata->get('instance/id');
}

/**
* @return array
*/
public function monitoredResource()
{
return [
'type' => 'cloud_run_revision',
'labels' => [
'configuration_name' => $this->configurationId,
'location' => $this->region,
'project_id' => $this->projectId(),
'revision_name' => $this->revisionId,
'service_name' => $this->serviceId,
],
];
}

/**
* Return the project id.
* @return string
*/
public function projectId()
{
return $this->metadata->getProjectId();
}

/**
* Return the service id.
* @return string
*/
public function serviceId()
{
return $this->serviceId;
}

/**
* Return the version id.
* @return string
*/
public function versionId()
{
return $this->revisionId;
}

/**
* Return the labels. We need to evaluate $_SERVER for each request.
*
* @return array
* @todo Figure out where to get the `container_name` from
*/
public function labels()
{
$labels = [
'instanceId' => $this->instanceId,
'run.googleapis.com/trace_id' => $this->traceId,
];
return \array_filter($labels);
}
}
Loading
Loading