Skip to content

Commit

Permalink
feat: add cicd provider detector
Browse files Browse the repository at this point in the history
Related to MRGFY-4457

Change-Id: I39965fbdb72833c4e371b5bf345a24e3a2f91875
  • Loading branch information
jd committed Dec 16, 2024
1 parent 8ee7c6e commit 6a5505e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pytest_mergify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter,
)
import opentelemetry.sdk.resources

from pytest_mergify import utils

import pytest_mergify.resources.ci as resources_ci

import pytest_opentelemetry.instrumentation

Expand Down Expand Up @@ -74,7 +75,13 @@ def pytest_configure(self, config: _pytest.config.Config) -> None:
else:
return

tracer_provider = TracerProvider()
resource = opentelemetry.sdk.resources.get_aggregated_resources(
[
resources_ci.CIResourceDetector(),
]
)

tracer_provider = TracerProvider(resource=resource)

tracer_provider.add_span_processor(span_processor)

Expand Down
Empty file.
19 changes: 19 additions & 0 deletions pytest_mergify/resources/ci.py
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,
}
)
44 changes: 44 additions & 0 deletions pytest_mergify/resources/github_actions.py
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(),
}
)
3 changes: 3 additions & 0 deletions pytest_mergify/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import typing

from pytest_mergify import utils

CIProviderT = typing.Literal["github_actions", "circleci"]

SUPPORTED_CIs = {
"GITHUB_ACTIONS": "github_actions",
"CIRCLECI": "circleci",
"_PYTEST_MERGIFY_TEST": "pytest_mergify_suite",
}


Expand Down
12 changes: 12 additions & 0 deletions tests/test_resources.py
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"

0 comments on commit 6a5505e

Please sign in to comment.