-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathCloudRunServiceMetadataProvider.php
More file actions
138 lines (122 loc) · 3.27 KB
/
CloudRunServiceMetadataProvider.php
File metadata and controls
138 lines (122 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Copyright 2024 Google LLC
*
* 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);
}
}