Skip to content

Commit d37b6d2

Browse files
colincadamsc24t
authored andcommitted
Fix zone value in monitored resource (census-instrumentation#534)
1 parent ff80d11 commit d37b6d2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

opencensus/common/monitored_resource/gcp_metadata_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
ZONE_KEY: 'instance/zone'
3838
}
3939

40+
_ATTRIBUTE_URI_TRANSFORMATIONS = {
41+
_GCE_ATTRIBUTES[ZONE_KEY]:
42+
lambda v: v[v.rfind('/') + 1:] if '/' in v else v
43+
}
4044

4145
_GCP_METADATA_MAP = {}
4246

@@ -104,4 +108,8 @@ def get_attribute(attribute_uri):
104108
# to a normal string:
105109
attribute_value = attribute_value.decode('utf-8')
106110

111+
transformation = _ATTRIBUTE_URI_TRANSFORMATIONS.get(attribute_uri)
112+
if transformation is not None:
113+
attribute_value = transformation(attribute_value)
114+
107115
return attribute_value

tests/unit/common/monitored_resource_util/test_gcp_metadata_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def assign_attribute_value(*args, **kwargs):
3131
elif attribute_uri == 'project-id':
3232
return 'my-project'
3333
elif attribute_uri == 'zone':
34-
return 'us-east1'
34+
return '/projects/012345678/zones/us-east1'
3535

3636
http_request_mock.side_effect = assign_attribute_value
3737
gcp_metadata_config.GcpMetadataConfig.inited = False
@@ -68,7 +68,7 @@ def assign_attribute_value(*args, **kwargs):
6868
elif attribute_uri == 'project-id':
6969
return b'my-project'
7070
elif attribute_uri == 'zone':
71-
return b'us-east1'
71+
return b'/projects/012345678/zones/us-east1'
7272

7373
http_request_mock.side_effect = assign_attribute_value
7474
gcp_metadata_config.GcpMetadataConfig.inited = False

0 commit comments

Comments
 (0)