-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to MRGFY-4457 Change-Id: I39965fbdb72833c4e371b5bf345a24e3a2f91875
- Loading branch information
Showing
6 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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,19 @@ | ||
from opentelemetry.sdk.resources import Resource, ResourceDetector | ||
|
||
from pytest_mergify import utils | ||
|
||
|
||
class CIResourceDetector(ResourceDetector): | ||
"""Detects OpenTelemetry Resource attributes for GitHub Actions.""" | ||
|
||
def detect(self) -> Resource: | ||
provider = utils.get_ci_provider() | ||
|
||
if provider is None: | ||
return Resource({}) | ||
|
||
return Resource( | ||
{ | ||
"cicd.provider.name": provider, | ||
} | ||
) |
This file contains 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,44 @@ | ||
import os | ||
import subprocess | ||
from typing import Dict, Union | ||
|
||
from opentelemetry.sdk.resources import Resource, ResourceDetector | ||
from opentelemetry.semconv.resource import ResourceAttributes | ||
|
||
Attributes = Dict[str, Union[str, bool, int, float]] | ||
|
||
|
||
class GitHubActionsResourceDetector(ResourceDetector): | ||
"""Detects OpenTelemetry Resource attributes for GitHub Actions.""" | ||
|
||
@staticmethod | ||
def get_codebase_name() -> str: | ||
# TODO: any better ways to guess the name of the codebase? | ||
# TODO: look into methods for locating packaging information | ||
return os.path.split(os.getcwd())[-1] | ||
|
||
@staticmethod | ||
def get_codebase_version() -> str: | ||
try: | ||
response = subprocess.check_output( | ||
["git", "rev-parse", "--is-inside-work-tree"] | ||
) | ||
if response.strip() != b"true": | ||
return "[unknown: not a git repository]" | ||
except Exception: # pylint: disable=broad-except | ||
return "[unknown: not a git repository]" | ||
|
||
try: | ||
version = subprocess.check_output(["git", "rev-parse", "HEAD"]) | ||
except Exception as exception: # pylint: disable=broad-except | ||
return f"[unknown: {str(exception)}]" | ||
|
||
return version.decode().strip() | ||
|
||
def detect(self) -> Resource: | ||
return Resource( | ||
{ | ||
ResourceAttributes.SERVICE_NAME: self.get_codebase_name(), | ||
ResourceAttributes.SERVICE_VERSION: self.get_codebase_version(), | ||
} | ||
) |
This file contains 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 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,12 @@ | ||
import _pytest.pytester | ||
import _pytest.config | ||
|
||
|
||
def test_span_resources_attributes( | ||
pytestconfig: _pytest.config.Config, | ||
) -> None: | ||
plugin = pytestconfig.pluginmanager.get_plugin("PytestMergify") | ||
assert plugin is not None | ||
assert plugin.exporter is not None | ||
spans = plugin.exporter.get_finished_spans() | ||
assert spans[0].resource.attributes["cicd.provider.name"] == "pytest_mergify_suite" |