From 180f7ea665e06739131967708e8ea6562a68bcb8 Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Mon, 14 Dec 2020 16:50:20 -0800 Subject: [PATCH] Adding global variables with supported resources Adding default service name population --- .../opentelemetry/sdk/resources/__init__.py | 63 ++++++++++++++++++- .../tests/metrics/test_metrics.py | 6 +- .../tests/resources/test_resources.py | 14 ++++- opentelemetry-sdk/tests/trace/test_trace.py | 6 +- 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 086a2fdb5a5..4b41db7dbda 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -91,9 +91,64 @@ logger = logging.getLogger(__name__) -TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language" +CLOUD_PROVIDER = "cloud.provider" +CLOUD_ACCOUNT_ID = "cloud.account.id" +CLOUD_REGION = "cloud.region" +CLOUD_ZONE = "cloud.zone" +CONTAINER_NAME = "container.name" +CONTAINER_ID = "container.id" +CONTAINER_IMAGE_NAME = "container.image.name" +CONTAINER_IMAGE_TAG = "container.image.tag" +CONTAINER_NAME = "container.name" +CONTAINER_NAME = "container.name" +DEPLOYMENT_ENVIRONMENT = "deployment.environment" +FAAS_NAME = "faas.name" +FAAS_ID = "faas.id" +FAAS_VERSION = "faas.version" +FAAS_INSTANCE = "faas.instance" +HOST_NAME = "host.name" +HOST_TYPE = "host.type" +HOST_IMAGE_NAME = "host.image.name" +HOST_IMAGE_ID = "host.image.id" +HOST_IMAGE_VERSION = "host.image.version" +KUBERNETES_CLUSTER_NAME = "k8s.cluster.name" +KUBERNETES_NAMESPACE_NAME = "k8s.namespace.name" +KUBERNETES_POD_UID = "k8s.pod.uid" +KUBERNETES_POD_NAME = "k8s.pod.name" +KUBERNETES_CONTAINER_NAME = "k8s.container.name" +KUBERNETES_REPLICA_SET_UID = "k8s.replicaset.uid" +KUBERNETES_REPLICA_SET_NAME = "k8s.replicaset.name" +KUBERNETES_DEPLOYMENT_UID = "k8s.deployment.uid" +KUBERNETES_DEPLOYMENT_NAME = "k8s.deployment.name" +KUBERNETES_STATEFUL_SET_UID = "k8s.statefulset.uid" +KUBERNETES_STATEFUL_SET_NAME = "k8s.statefulset.name" +KUBERNETES_DAEMON_SET_UID = "k8s.daemonset.uid" +KUBERNETES_DAEMON_SET_NAME = "k8s.daemonset.name" +KUBERNETES_JOB_UID = "k8s.job.uid" +KUBERNETES_JOB_NAME = "k8s.job.name" +KUBERNETES_CRON_JOB_UID = "k8s.cronjob.uid" +KUBERNETES_CRON_JOB_NAME = "k8s.cronjob.name" +OS_TYPE = "os.type" +OS_DESCRIPTION = "os.description" +PROCESS_PID = "process.pid" +PROCESS_EXECUTABLE_NAME = "process.executable.name" +PROCESS_EXECUTABLE_PATH = "process.executable.path" +PROCESS_COMMAND = "process.command" +PROCESS_COMMAND_LINE = "process.command_line" +PROCESS_COMMAND_ARGS = "process.command_args" +PROCESS_OWNER = "process.owner" +PROCESS_RUNTIME_NAME = "process.runtime.name" +PROCESS_RUNTIME_VERSION = "process.runtime.version" +PROCESS_RUNTIME_DESCRIPTION = "process.runtime.description" +SERVICE_NAME = "service.name" +SERVICE_NAMESPACE = "service.namespace" +SERVICE_INSTANCE_ID = "service.instance.id" +SERVICE_VERSION = "service.version" TELEMETRY_SDK_NAME = "telemetry.sdk.name" TELEMETRY_SDK_VERSION = "telemetry.sdk.version" +TELEMETRY_AUTO_VERSION = "telemetry.auto.version" +TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language" + OPENTELEMETRY_SDK_VERSION = pkg_resources.get_distribution( "opentelemetry-sdk" @@ -111,6 +166,12 @@ def create(attributes: typing.Optional[Attributes] = None) -> "Resource": resource = _DEFAULT_RESOURCE else: resource = _DEFAULT_RESOURCE.merge(Resource(attributes)) + + default_service_name = "unknown_service" + process_executable_name = resource.attributes.get(PROCESS_EXECUTABLE_NAME, None) + if process_executable_name: + default_service_name += ":" + process_executable_name + resource = resource.merge(Resource({SERVICE_NAME: default_service_name})) return resource.merge(OTELResourceDetector().detect()) @staticmethod diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index 32c22c8c6bb..c885fd997de 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -38,7 +38,11 @@ def test_resource(self): def test_resource_empty(self): meter_provider = metrics.MeterProvider() # pylint: disable=protected-access - self.assertEqual(meter_provider.resource, resources._DEFAULT_RESOURCE) + self.assertIsInstance(meter_provider.resource, resources.Resource) + self.assertEqual(meter_provider.resource.attributes.get(resources.SERVICE_NAME), "unknown_service") + self.assertEqual(meter_provider.resource.attributes.get(resources.TELEMETRY_SDK_LANGUAGE), "python") + self.assertEqual(meter_provider.resource.attributes.get(resources.TELEMETRY_SDK_NAME), "opentelemetry") + self.assertEqual(meter_provider.resource.attributes.get(resources.TELEMETRY_SDK_VERSION),resources.OPENTELEMETRY_SDK_VERSION) def test_start_pipeline(self): exporter = Mock() diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 35bffb10b8c..dd4942efddc 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -44,6 +44,7 @@ def test_create(self): resources.TELEMETRY_SDK_NAME: "opentelemetry", resources.TELEMETRY_SDK_LANGUAGE: "python", resources.TELEMETRY_SDK_VERSION: resources.OPENTELEMETRY_SDK_VERSION, + resources.SERVICE_NAME: "unknown_service", } resource = resources.Resource.create(attributes) @@ -62,10 +63,12 @@ def test_create(self): self.assertEqual(resource, resources._EMPTY_RESOURCE) resource = resources.Resource.create(None) - self.assertEqual(resource, resources._DEFAULT_RESOURCE) + self.assertEqual(resource, resources._DEFAULT_RESOURCE.merge( + resources.Resource({resources.SERVICE_NAME: "unknown_service"}))) resource = resources.Resource.create({}) - self.assertEqual(resource, resources._DEFAULT_RESOURCE) + self.assertEqual(resource, resources._DEFAULT_RESOURCE.merge( + resources.Resource({resources.SERVICE_NAME: "unknown_service"}))) def test_resource_merge(self): left = resources.Resource({"service": "ui"}) @@ -103,6 +106,7 @@ def test_immutability(self): resources.TELEMETRY_SDK_NAME: "opentelemetry", resources.TELEMETRY_SDK_LANGUAGE: "python", resources.TELEMETRY_SDK_VERSION: resources.OPENTELEMETRY_SDK_VERSION, + resources.SERVICE_NAME: "unknown_service", } attributes_copy = attributes.copy() @@ -117,6 +121,12 @@ def test_immutability(self): attributes["cost"] = 999.91 self.assertEqual(resource.attributes, attributes_copy) + def test_service_name_using_process_name(self): + resource = resources.Resource.create( + {resources.PROCESS_EXECUTABLE_NAME: "test"}) + self.assertEqual(resource.attributes.get( + resources.SERVICE_NAME), "unknown_service:test") + def test_aggregated_resources_no_detectors(self): aggregated_resources = resources.get_aggregated_resources([]) self.assertEqual( diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 6879b6390d3..1e0816275ff 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -426,7 +426,11 @@ def test_default_span_resource(self): tracer = tracer_provider.get_tracer(__name__) span = tracer.start_span("root") # pylint: disable=protected-access - self.assertEqual(span.resource, resources._DEFAULT_RESOURCE) + self.assertIsInstance(span.resource, resources.Resource) + self.assertEqual(span.resource.attributes.get(resources.SERVICE_NAME), "unknown_service") + self.assertEqual(span.resource.attributes.get(resources.TELEMETRY_SDK_LANGUAGE), "python") + self.assertEqual(span.resource.attributes.get(resources.TELEMETRY_SDK_NAME), "opentelemetry") + self.assertEqual(span.resource.attributes.get(resources.TELEMETRY_SDK_VERSION),resources.OPENTELEMETRY_SDK_VERSION) def test_span_context_remote_flag(self): tracer = new_tracer()