-
Notifications
You must be signed in to change notification settings - Fork 6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8850a7
distro: add cloud resource detectors
xrmx 33dd321
Remove container resource detector that has not been published
xrmx 4af2bf8
Update distributoin default settings
xrmx 73c6a6e
Update README.md
xrmx d34185c
Update dev-requirements with the dependencies
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why the leading underscore. Is it because of the
_detector
sub-package import?There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 😓