Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure Resources follow semantic conventions #1480

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.16b1...HEAD)

- Make sure Resources follow semantic conventions
([#1480](https://github.com/open-telemetry/opentelemetry-python/pull/1480))

- Add support for Python 3.9
([#1441](https://github.com/open-telemetry/opentelemetry-python/pull/1441))

Expand Down
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"
lzchen marked this conversation as resolved.
Show resolved Hide resolved
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dup

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}))
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
return resource.merge(OTELResourceDetector().detect())
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

@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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if this test should be renamed since it's not using the empty resource (there's a separate create_empty method for empty resources)

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