Skip to content

Commit

Permalink
Adding global variables with supported resources
Browse files Browse the repository at this point in the history
Adding default service name population
  • Loading branch information
hectorhdzg committed Dec 15, 2020
1 parent 0011637 commit 180f7ea
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
63 changes: 62 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"})
Expand Down Expand Up @@ -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()
Expand All @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 180f7ea

Please sign in to comment.