예제 #1
0
    def test_set_attributes_gae(self):
        import os

        trace = {'spans': [
            {
                'attributes': {}
            }
        ]}

        expected = {
            'attributes': {
                'attributeMap': {
                    'g.co/gae/app/module': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'service'
                        }
                    },
                    'g.co/gae/app/instance': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'flex'
                        }
                    },
                    'g.co/gae/app/version': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'version'
                        }
                    },
                    'g.co/gae/app/project': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'project'
                        }
                    },
                    'g.co/agent': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'opencensus-python [{}]'.format(
                                stackdriver_exporter.VERSION
                            )
                        }
                    },
                }
            }
        }

        with mock.patch.dict(
                os.environ,
                {stackdriver_exporter._APPENGINE_FLEXIBLE_ENV_VM: 'vm',
                 stackdriver_exporter._APPENGINE_FLEXIBLE_ENV_FLEX: 'flex',
                 'GOOGLE_CLOUD_PROJECT': 'project',
                 'GAE_SERVICE': 'service',
                 'GAE_VERSION': 'version'}):
            self.assertTrue(stackdriver_exporter.is_gae_environment())
            stackdriver_exporter.set_attributes(trace)

        span = trace.get('spans')[0]
        self.assertEqual(span, expected)
    def test_monitored_resource_attributes(self, monitor_resource_mock):
        import os

        trace = {'spans': [{'attributes': {}}]}

        expected = {
            'attributes': {
                'attributeMap': {
                    'g.co/gae/app/module': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'service'
                        }
                    },
                    'g.co/gae/app/instance': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'flex'
                        }
                    },
                    'g.co/gae/app/version': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'version'
                        }
                    },
                    'g.co/gae/app/project': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'project'
                        }
                    },
                    'g.co/agent': {
                        'string_value': {
                            'truncated_byte_count':
                            0,
                            'value':
                            'opencensus-python [{}]'.format(
                                stackdriver_exporter.VERSION)
                        }
                    },
                    'g.co/r/gke_container/instance_id': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': '12345'
                        }
                    },
                    'g.co/r/gke_container/project_id': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'my_project'
                        }
                    },
                    'g.co/r/gke_container/zone': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'zone1'
                        }
                    },
                }
            }
        }

        monitor_resource_mock.return_value = mock.Mock()

        monitor_resource_mock.return_value.resource_type = 'gke_container'
        monitor_resource_mock.return_value.get_resource_labels.return_value = {
            'project_id': 'my_project',
            'instance_id': '12345',
            'zone': 'zone1'
        }
        with mock.patch.dict(
                os.environ, {
                    stackdriver_exporter._APPENGINE_FLEXIBLE_ENV_VM: 'vm',
                    stackdriver_exporter._APPENGINE_FLEXIBLE_ENV_FLEX: 'flex',
                    'GOOGLE_CLOUD_PROJECT': 'project',
                    'GAE_SERVICE': 'service',
                    'GAE_VERSION': 'version'
                }):
            self.assertTrue(stackdriver_exporter.is_gae_environment())
            stackdriver_exporter.set_attributes(trace)

        span = trace.get('spans')[0]
        self.assertEqual(span, expected)