-
Notifications
You must be signed in to change notification settings - Fork 10
distro: add cloud resource detectors #198
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
Changes from 3 commits
f8850a7
33dd321
4af2bf8
73c6a6e
d34185c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,8 +31,11 @@ dependencies = [ | |
| "opentelemetry-exporter-otlp == 1.28.2", | ||
| "opentelemetry-instrumentation == 0.49b2", | ||
| "opentelemetry-instrumentation-system-metrics == 0.49b2", | ||
| "opentelemetry-semantic-conventions == 0.49b2", | ||
| "opentelemetry-resourcedetector-gcp ~= 1.7.0a0", | ||
| "opentelemetry-resource-detector-azure ~= 0.1.5", | ||
| "opentelemetry-sdk == 1.28.2", | ||
| "opentelemetry-sdk-extension-aws ~= 2.0.2", | ||
| "opentelemetry-semantic-conventions == 0.49b2", | ||
| "packaging", | ||
| ] | ||
|
|
||
|
|
@@ -48,6 +51,7 @@ distro = "elasticotel.distro:ElasticOpenTelemetryDistro" | |
| [project.entry-points.opentelemetry_resource_detector] | ||
| process_runtime = "elasticotel.sdk.resources:ProcessRuntimeResourceDetector" | ||
| telemetry_distro = "elasticotel.sdk.resources:TelemetryDistroResourceDetector" | ||
| _gcp = "opentelemetry.resourcedetector.gcp_resource_detector._detector:GoogleCloudResourceDetector" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious why the leading underscore. Is it because of the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because I'm defining the entry point here while it should really be the package with the resource detector doing it 😓 |
||
|
|
||
| [project.scripts] | ||
| edot-bootstrap = "elasticotel.instrumentation.bootstrap:run" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| # or more contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| import os | ||
|
|
||
| AWS_LAMBDA_DETECTORS = ["aws_lambda"] | ||
| AZURE_FUNCTIONS_DETECTORS = ["azure_functions"] | ||
| GCP_CLOUD_RUN_DETECTORS = ["_gcp"] | ||
| KUBERNETES_DETECTORS = ["_gcp", "aws_eks"] | ||
| OTHER_CLOUD_DETECTORS = [ | ||
| "_gcp", | ||
| "aws_ec2", | ||
| "aws_ecs", | ||
| "aws_elastic_beanstalk", | ||
| "azure_app_service", | ||
| "azure_vm", | ||
| ] | ||
|
|
||
|
|
||
| def _on_aws_lambda(): | ||
| """Cheap check to detect if we are running on AWS lambda""" | ||
| return "AWS_LAMBDA_FUNCTION_NAME" in os.environ | ||
|
|
||
|
|
||
| def _on_azure_functions(): | ||
| """Cheap check to detect if we are running on Azure functions""" | ||
| return "FUNCTIONS_WORKER_RUNTIME" in os.environ | ||
|
|
||
|
|
||
| def _on_gcp_cloud_run(): | ||
| """Cheap check to detect if we are running inside Google Cloud Run""" | ||
| return "K_CONFIGURATION" in os.environ | ||
|
|
||
|
|
||
| def _on_k8s(): | ||
| """Cheap check to detect if we are running inside a Kubernetes pod or not""" | ||
| return "KUBERNETES_SERVICE_HOST" in os.environ | ||
|
|
||
|
|
||
| def get_cloud_resource_detectors(): | ||
| """Helper to get a subset of the available cloud resource detectors depending on the environment | ||
| This is done to avoid loading resource detectors doing HTTP requests for metadata that will fail""" | ||
| if _on_aws_lambda(): | ||
| return AWS_LAMBDA_DETECTORS | ||
| elif _on_azure_functions(): | ||
| return AZURE_FUNCTIONS_DETECTORS | ||
| elif _on_gcp_cloud_run(): | ||
| return GCP_CLOUD_RUN_DETECTORS | ||
| elif _on_k8s(): | ||
| return KUBERNETES_DETECTORS | ||
| return OTHER_CLOUD_DETECTORS | ||
|
Comment on lines
+53
to
+65
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. I like this. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| # or more contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| from unittest import TestCase, mock | ||
|
|
||
| from elasticotel.distro.resource_detectors import get_cloud_resource_detectors | ||
|
|
||
|
|
||
| class TestGetCloudResourceDetectors(TestCase): | ||
| @mock.patch.dict("os.environ", {"AWS_LAMBDA_FUNCTION_NAME": "lambda"}, clear=True) | ||
| def test_aws_lambda(self): | ||
| resource_detectors = get_cloud_resource_detectors() | ||
| self.assertEqual(resource_detectors, ["aws_lambda"]) | ||
|
|
||
| @mock.patch.dict("os.environ", {"FUNCTIONS_WORKER_RUNTIME": "azure"}, clear=True) | ||
| def test_azure_functions(self): | ||
| resource_detectors = get_cloud_resource_detectors() | ||
| self.assertEqual(resource_detectors, ["azure_functions"]) | ||
|
|
||
| @mock.patch.dict("os.environ", {"K_CONFIGURATION": "cloudrun"}, clear=True) | ||
| def test_gcp_cloud_run(self): | ||
| resource_detectors = get_cloud_resource_detectors() | ||
| self.assertEqual(resource_detectors, ["_gcp"]) | ||
|
|
||
| @mock.patch.dict("os.environ", {"KUBERNETES_SERVICE_HOST": "k8s"}, clear=True) | ||
| def test_kubernetes_pod(self): | ||
| resource_detectors = get_cloud_resource_detectors() | ||
| self.assertEqual(resource_detectors, ["_gcp", "aws_eks"]) | ||
|
|
||
| @mock.patch.dict("os.environ", {}, clear=True) | ||
| def test_other_cloud_detectors(self): | ||
| resource_detectors = get_cloud_resource_detectors() | ||
| self.assertEqual( | ||
| resource_detectors, | ||
| ["_gcp", "aws_ec2", "aws_ecs", "aws_elastic_beanstalk", "azure_app_service", "azure_vm"], | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.