def test_aggregated_resources_with_static_resource(self): static_resource = resources.Resource({"static_key": "static_value"}) self.assertEqual( resources.get_aggregated_resources( [], initial_resource=static_resource), static_resource, ) # Static resource values should never be overwritten resource_detector = mock.Mock(spec=resources.ResourceDetector) resource_detector.detect.return_value = resources.Resource({ "static_key": "try_to_overwrite_existing_value", "key": "value" }) self.assertEqual( resources.get_aggregated_resources( [resource_detector], initial_resource=static_resource), resources.Resource({ "static_key": "static_value", "key": "value" }), )
def test_aggregated_resources_with_default_destroying_static_resource( self, ): static_resource = resources.Resource({"static_key": "static_value"}) self.assertEqual( resources.get_aggregated_resources( [], initial_resource=static_resource ), static_resource, ) resource_detector = mock.Mock(spec=resources.ResourceDetector) resource_detector.detect.return_value = resources.Resource( {"static_key": "try_to_overwrite_existing_value", "key": "value"} ) self.assertEqual( resources.get_aggregated_resources( [resource_detector], initial_resource=static_resource ), resources.Resource( { "static_key": "try_to_overwrite_existing_value", "key": "value", } ), )
def test_aggregated_resources_multiple_detectors(self): resource_detector1 = mock.Mock(spec=resources.ResourceDetector) resource_detector1.detect.return_value = resources.Resource( {"key1": "value1"}) resource_detector2 = mock.Mock(spec=resources.ResourceDetector) resource_detector2.detect.return_value = resources.Resource({ "key2": "value2", "key3": "value3" }) resource_detector3 = mock.Mock(spec=resources.ResourceDetector) resource_detector3.detect.return_value = resources.Resource({ "key2": "try_to_overwrite_existing_value", "key3": "try_to_overwrite_existing_value", "key4": "value4", }) # New values should not overwrite existing values self.assertEqual( resources.get_aggregated_resources( [resource_detector1, resource_detector2, resource_detector3]), resources.Resource({ "key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4", }), )
def test_resource_detector_ignore_error(self): resource_detector = mock.Mock(spec=resources.ResourceDetector) resource_detector.detect.side_effect = Exception() resource_detector.raise_on_error = False self.assertEqual( resources.get_aggregated_resources([resource_detector]), resources.Resource.create_empty(), )
def test_aggregated_resources_no_detectors(self): aggregated_resources = resources.get_aggregated_resources([]) self.assertEqual( aggregated_resources, resources._DEFAULT_RESOURCE.merge( resources.Resource( {resources.SERVICE_NAME: "unknown_service"}, "" ) ), )
def test_resource_detector_ignore_error(self): resource_detector = mock.Mock(spec=resources.ResourceDetector) resource_detector.detect.side_effect = Exception() resource_detector.raise_on_error = False self.assertEqual( resources.get_aggregated_resources([resource_detector]), resources._DEFAULT_RESOURCE.merge( resources.Resource( {resources.SERVICE_NAME: "unknown_service"}, "" ) ), )
def test_aggregated_resources_multiple_detectors(self): resource_detector1 = mock.Mock(spec=resources.ResourceDetector) resource_detector1.detect.return_value = resources.Resource( {"key1": "value1"} ) resource_detector2 = mock.Mock(spec=resources.ResourceDetector) resource_detector2.detect.return_value = resources.Resource( {"key2": "value2", "key3": "value3"} ) resource_detector3 = mock.Mock(spec=resources.ResourceDetector) resource_detector3.detect.return_value = resources.Resource( { "key2": "try_to_overwrite_existing_value", "key3": "try_to_overwrite_existing_value", "key4": "value4", } ) self.assertEqual( resources.get_aggregated_resources( [resource_detector1, resource_detector2, resource_detector3] ), resources._DEFAULT_RESOURCE.merge( resources.Resource( {resources.SERVICE_NAME: "unknown_service"}, "" ) ).merge( resources.Resource( { "key1": "value1", "key2": "try_to_overwrite_existing_value", "key3": "try_to_overwrite_existing_value", "key4": "value4", } ) ), )
# limitations under the License. import time from opentelemetry import metrics from opentelemetry.exporter.cloud_monitoring import ( CloudMonitoringMetricsExporter, ) from opentelemetry.sdk.metrics import Counter, MeterProvider from opentelemetry.sdk.resources import get_aggregated_resources from opentelemetry.tools.resource_detector import GoogleCloudResourceDetector # MUST be run on a Google tool! # Detect resources from the environment resources = get_aggregated_resources( [GoogleCloudResourceDetector(raise_on_error=True)] ) metrics.set_meter_provider(MeterProvider(resource=resources)) meter = metrics.get_meter(__name__) metrics.get_meter_provider().start_pipeline( meter, CloudMonitoringMetricsExporter(), 5 ) requests_counter = meter.create_metric( name="request_counter_with_resource", description="number of requests", unit="1", value_type=int, metric_type=Counter, )
def test_aggregated_resources_no_detectors(self): aggregated_resources = resources.get_aggregated_resources([]) self.assertEqual(aggregated_resources, resources.Resource.create_empty())
def test_aggregated_resources_different_schema_urls(self): resource_detector1 = mock.Mock(spec=resources.ResourceDetector) resource_detector1.detect.return_value = resources.Resource( {"key1": "value1"}, "" ) resource_detector2 = mock.Mock(spec=resources.ResourceDetector) resource_detector2.detect.return_value = resources.Resource( {"key2": "value2", "key3": "value3"}, "url1" ) resource_detector3 = mock.Mock(spec=resources.ResourceDetector) resource_detector3.detect.return_value = resources.Resource( { "key2": "try_to_overwrite_existing_value", "key3": "try_to_overwrite_existing_value", "key4": "value4", }, "url2", ) resource_detector4 = mock.Mock(spec=resources.ResourceDetector) resource_detector4.detect.return_value = resources.Resource( { "key2": "try_to_overwrite_existing_value", "key3": "try_to_overwrite_existing_value", "key4": "value4", }, "url1", ) self.assertEqual( resources.get_aggregated_resources( [resource_detector1, resource_detector2] ), resources._DEFAULT_RESOURCE.merge( resources.Resource( {resources.SERVICE_NAME: "unknown_service"}, "" ) ).merge( resources.Resource( {"key1": "value1", "key2": "value2", "key3": "value3"}, "url1", ) ), ) with self.assertLogs(level=ERROR) as log_entry: self.assertEqual( resources.get_aggregated_resources( [resource_detector2, resource_detector3] ), resources._DEFAULT_RESOURCE.merge( resources.Resource( {resources.SERVICE_NAME: "unknown_service"}, "" ) ).merge( resources.Resource( {"key2": "value2", "key3": "value3"}, "url1" ) ), ) self.assertIn("url1", log_entry.output[0]) self.assertIn("url2", log_entry.output[0]) with self.assertLogs(level=ERROR): self.assertEqual( resources.get_aggregated_resources( [ resource_detector2, resource_detector3, resource_detector4, resource_detector1, ] ), resources._DEFAULT_RESOURCE.merge( resources.Resource( {resources.SERVICE_NAME: "unknown_service"}, "" ) ).merge( resources.Resource( { "key1": "value1", "key2": "try_to_overwrite_existing_value", "key3": "try_to_overwrite_existing_value", "key4": "value4", }, "url1", ) ), ) self.assertIn("url1", log_entry.output[0]) self.assertIn("url2", log_entry.output[0])
from opentelemetry import trace from opentelemetry.sdk.resources import get_aggregated_resources from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ( ConsoleSpanExporter, SimpleExportSpanProcessor, ) from opentelemetry.tools.resource_detector import GoogleCloudResourceDetector resources = get_aggregated_resources([GoogleCloudResourceDetector()]) trace.set_tracer_provider(TracerProvider(resource=resources)) trace.get_tracer_provider().add_span_processor( SimpleExportSpanProcessor(ConsoleSpanExporter()) ) tracer = trace.get_tracer(__name__) with tracer.start_as_current_span("foo"): print("Hello world!")