Skip to content

Commit

Permalink
Refactor resource mapping logic into resource detection package
Browse files Browse the repository at this point in the history
and update the exporters to use the shared code.
  • Loading branch information
aabmass committed Feb 22, 2023
1 parent 65893a8 commit dd1dd1e
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 234 deletions.
1 change: 1 addition & 0 deletions e2e-test-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ FROM python-base as build-base
# copy local dependencies
COPY opentelemetry-exporter-gcp-trace opentelemetry-exporter-gcp-trace
COPY opentelemetry-propagator-gcp opentelemetry-propagator-gcp
COPY opentelemetry-resourcedetector-gcp opentelemetry-resourcedetector-gcp
WORKDIR $SRC/e2e-test-server
# copy requirements/constraints
COPY e2e-test-server/requirements.txt e2e-test-server/constraints.txt ./
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-exporter-gcp-monitoring/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ install_requires =
google-cloud-monitoring ~= 2.0
opentelemetry-api ~= 1.0
opentelemetry-sdk ~= 1.0
opentelemetry-resourcedetector-gcp >= 1.5.0dev0, == 1.*

[options.packages.find]
where = src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from google.api.label_pb2 import LabelDescriptor
from google.api.metric_pb2 import Metric as GMetric
from google.api.metric_pb2 import MetricDescriptor
from google.api.monitored_resource_pb2 import MonitoredResource
from google.cloud.monitoring_v3 import (
CreateMetricDescriptorRequest,
CreateTimeSeriesRequest,
Expand All @@ -39,10 +40,10 @@

# pylint: disable=no-name-in-module
from google.protobuf.timestamp_pb2 import Timestamp
from opentelemetry.exporter.cloud_monitoring._resource import (
from opentelemetry.exporter.cloud_monitoring.version import __version__
from opentelemetry.resourcedetector.gcp_resource_detector._mapping import (
get_monitored_resource,
)
from opentelemetry.exporter.cloud_monitoring.version import __version__
from opentelemetry.sdk.metrics.export import (
Gauge,
Histogram,
Expand Down Expand Up @@ -296,9 +297,17 @@ def export(
all_series = []

for resource_metric in metrics_data.resource_metrics:
monitored_resource = get_monitored_resource(
monitored_resource_data = get_monitored_resource(
resource_metric.resource
)
# convert it to proto
monitored_resource = None
if monitored_resource_data:
monitored_resource = MonitoredResource(
type=monitored_resource_data.type,
labels=monitored_resource_data.labels,
)

for scope_metric in resource_metric.scope_metrics:
for metric in scope_metric.metrics:
# Convert all data_points to Sequences, see
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-exporter-gcp-trace/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ install_requires =
google-cloud-trace ~= 1.1
opentelemetry-api ~= 1.0
opentelemetry-sdk ~= 1.0
opentelemetry-resourcedetector-gcp >= 1.5.0dev0, == 1.*

[options.packages.find]
where = src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX,
)
from opentelemetry.exporter.cloud_trace.version import __version__
from opentelemetry.resourcedetector.gcp_resource_detector._mapping import (
get_monitored_resource,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import Event
from opentelemetry.sdk.trace.export import (
Expand Down Expand Up @@ -420,24 +423,6 @@ def _strip_characters(ot_version):
return "".join(filter(lambda x: x.isdigit() or x == ".", ot_version))


OT_RESOURCE_ATTRIBUTE_TO_GCP = {
"gce_instance": {
"host.id": "instance_id",
"cloud.account.id": "project_id",
"cloud.zone": "zone",
},
"gke_container": {
"k8s.cluster.name": "cluster_name",
"k8s.namespace.name": "namespace_id",
"k8s.pod.name": "pod_id",
"host.id": "instance_id",
"container.name": "container_name",
"cloud.account.id": "project_id",
"cloud.zone": "zone",
},
}


def _extract_resources(
resource: Resource, resource_regex: Optional[Pattern] = None
) -> Dict[str, str]:
Expand All @@ -451,24 +436,14 @@ def _extract_resources(
if resource_regex.match(k)
}
)
if resource_attributes.get("cloud.provider") != "gcp":
return extracted_attributes
resource_type = resource_attributes["gcp.resource_type"]
if (
not isinstance(resource_type, str)
or resource_type not in OT_RESOURCE_ATTRIBUTE_TO_GCP
):
return extracted_attributes
extracted_attributes.update(
{
"g.co/r/{}/{}".format(resource_type, gcp_resource_key): str(
resource_attributes[ot_resource_key]
)
for ot_resource_key, gcp_resource_key in OT_RESOURCE_ATTRIBUTE_TO_GCP[
resource_type
].items()
}
)
monitored_resource = get_monitored_resource(resource)
if monitored_resource:
extracted_attributes.update(
{
"g.co/r/{}/{}".format(monitored_resource.type, k): v
for k, v in monitored_resource.labels.items()
}
)
return extracted_attributes


Expand Down
109 changes: 50 additions & 59 deletions opentelemetry-exporter-gcp-trace/tests/test_cloud_trace_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ def test_constructor_explicit(self):
def test_export(self):
resource_info = Resource(
{
"cloud.account.id": 123,
"host.id": "host",
"cloud.zone": "US",
"cloud.account.id": "123",
"cloud.platform": "gcp_compute_engine",
"cloud.provider": "gcp",
"gcp.resource_type": "gce_instance",
"cloud.region": "us-east4",
"cloud.availability_zone": "us-east4-b",
"host.id": "host",
"host.name": "fakeName",
"host.type": "fakeMachineType",
}
)
span_datas = [
Expand Down Expand Up @@ -157,13 +160,12 @@ def test_export(self):
),
"attributes": ProtoSpan.Attributes(
attribute_map={
"g.co/r/gce_instance/zone": _format_attribute_value("US"),
"g.co/r/gce_instance/zone": _format_attribute_value(
"us-east4-b"
),
"g.co/r/gce_instance/instance_id": _format_attribute_value(
"host"
),
"g.co/r/gce_instance/project_id": _format_attribute_value(
"123"
),
"g.co/agent": self.agent_code,
"attr_key": _format_attribute_value("attr_value"),
}
Expand Down Expand Up @@ -652,27 +654,34 @@ def test_too_many_link_attributes(self):
)

def test_extract_empty_resources(self):
self.assertEqual(_extract_resources(Resource.get_empty()), {})
self.assertEqual(
_extract_resources(Resource.get_empty()),
{
"g.co/r/generic_node/location": "global",
"g.co/r/generic_node/namespace": "",
"g.co/r/generic_node/node_id": "",
},
)

def test_extract_resource_attributes_with_regex(self):
resource_regex = re.compile(r"service\..*")
resource = Resource(
attributes={
"cloud.account.id": 123,
"host.id": "host",
"cloud.zone": "US",
"cloud.account.id": "123",
"cloud.availability_zone": "us-east4-b",
"cloud.platform": "gcp_compute_engine",
"cloud.provider": "gcp",
"extra_info": "extra",
"gcp.resource_type": "gce_instance",
"not_gcp_resource": "value",
"cloud.region": "us-east4",
"host.id": "host",
"host.name": "fakeName",
"host.type": "fakeMachineType",
"service.name": "my-app",
"service.version": "1",
}
)
expected_extract = {
"g.co/r/gce_instance/project_id": "123",
"g.co/r/gce_instance/instance_id": "host",
"g.co/r/gce_instance/zone": "US",
"g.co/r/gce_instance/zone": "us-east4-b",
"service.name": "my-app",
"service.version": "1",
}
Expand All @@ -684,19 +693,19 @@ def test_non_matching_regex(self):
resource_regex = re.compile(r"this-regex-matches-nothing")
resource = Resource(
attributes={
"cloud.account.id": 123,
"host.id": "host",
"cloud.zone": "US",
"cloud.account.id": "123",
"cloud.availability_zone": "us-east4-b",
"cloud.platform": "gcp_compute_engine",
"cloud.provider": "gcp",
"extra_info": "extra",
"gcp.resource_type": "gce_instance",
"not_gcp_resource": "value",
"cloud.region": "us-east4",
"host.id": "host",
"host.name": "fakeName",
"host.type": "fakeMachineType",
}
)
expected_extract = {
"g.co/r/gce_instance/project_id": "123",
"g.co/r/gce_instance/instance_id": "host",
"g.co/r/gce_instance/zone": "US",
"g.co/r/gce_instance/zone": "us-east4-b",
}
self.assertEqual(
_extract_resources(resource, resource_regex), expected_extract
Expand All @@ -705,34 +714,22 @@ def test_non_matching_regex(self):
def test_extract_well_formed_resources(self):
resource = Resource(
attributes={
"cloud.account.id": 123,
"host.id": "host",
"cloud.zone": "US",
"cloud.account.id": "123",
"cloud.availability_zone": "us-east4-b",
"cloud.platform": "gcp_compute_engine",
"cloud.provider": "gcp",
"extra_info": "extra",
"gcp.resource_type": "gce_instance",
"not_gcp_resource": "value",
"cloud.region": "us-east4",
"host.id": "host",
"host.name": "fakeName",
"host.type": "fakeMachineType",
}
)
expected_extract = {
"g.co/r/gce_instance/project_id": "123",
"g.co/r/gce_instance/instance_id": "host",
"g.co/r/gce_instance/zone": "US",
"g.co/r/gce_instance/zone": "us-east4-b",
}
self.assertEqual(_extract_resources(resource), expected_extract)

def test_extract_malformed_resources(self):
# This resource doesn't have all the fields required for a gce_instance
# Specifically its missing "host.id", "cloud.zone", "cloud.account.id"
resource = Resource(
attributes={
"gcp.resource_type": "gce_instance",
"cloud.provider": "gcp",
}
)
# Should throw when passed a malformed GCP resource dict
self.assertRaises(KeyError, _extract_resources, resource)

def test_extract_unsupported_gcp_resources(self):
# Unsupported gcp resources will be ignored
resource = Resource(
Expand All @@ -741,24 +738,18 @@ def test_extract_unsupported_gcp_resources(self):
"host.id": "host",
"extra_info": "extra",
"not_gcp_resource": "value",
"gcp.resource_type": "unsupported_gcp_resource",
"cloud.platform": "gcp_some_unsupported_thing",
"cloud.provider": "gcp",
}
)
self.assertEqual(_extract_resources(resource), {})

def test_extract_unsupported_provider_resources(self):
# Resources with currently unsupported providers will be ignored
resource = Resource(
attributes={
"cloud.account.id": "123",
"host.id": "host",
"extra_info": "extra",
"not_gcp_resource": "value",
"cloud.provider": "aws",
}
self.assertEqual(
_extract_resources(resource),
{
"g.co/r/generic_node/location": "global",
"g.co/r/generic_node/namespace": "",
"g.co/r/generic_node/node_id": "host",
},
)
self.assertEqual(_extract_resources(resource), {})

def test_truncate_string(self):
"""Cloud Trace API imposes limits on the length of many things,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO: use opentelemetry-semantic-conventions package for these constants once it has
# stabilized. Right now, pinning an unstable version would cause dependency conflicts for
# users so these are copied in.
class ResourceAttributes:
AWS_EC2 = "aws_ec2"
CLOUD_ACCOUNT_ID = "cloud.account.id"
CLOUD_AVAILABILITY_ZONE = "cloud.availability_zone"
CLOUD_PLATFORM_KEY = "cloud.platform"
CLOUD_PROVIDER = "cloud.provider"
CLOUD_REGION = "cloud.region"
GCP_COMPUTE_ENGINE = "gcp_compute_engine"
GCP_KUBERNETES_ENGINE = "gcp_kubernetes_engine"
HOST_ID = "host.id"
HOST_NAME = "host.name"
HOST_TYPE = "host.type"
K8S_CLUSTER_NAME = "k8s.cluster.name"
K8S_CONTAINER_NAME = "k8s.container.name"
K8S_NAMESPACE_NAME = "k8s.namespace.name"
K8S_NODE_NAME = "k8s.node.name"
K8S_POD_NAME = "k8s.pod.name"
SERVICE_INSTANCE_ID = "service.instance.id"
SERVICE_NAME = "service.name"
SERVICE_NAMESPACE = "service.namespace"


AWS_ACCOUNT = "aws_account"
AWS_EC2_INSTANCE = "aws_ec2_instance"
CLUSTER_NAME = "cluster_name"
CONTAINER_NAME = "container_name"
GCE_INSTANCE = "gce_instance"
GENERIC_NODE = "generic_node"
GENERIC_TASK = "generic_task"
INSTANCE_ID = "instance_id"
JOB = "job"
K8S_CLUSTER = "k8s_cluster"
K8S_CONTAINER = "k8s_container"
K8S_NODE = "k8s_node"
K8S_POD = "k8s_pod"
LOCATION = "location"
NAMESPACE = "namespace"
NAMESPACE_NAME = "namespace_name"
NODE_ID = "node_id"
NODE_NAME = "node_name"
POD_NAME = "pod_name"
REGION = "region"
TASK_ID = "task_id"
ZONE = "zone"
Loading

0 comments on commit dd1dd1e

Please sign in to comment.