Skip to content

Commit

Permalink
feat: add cicd provider detector (#18)
Browse files Browse the repository at this point in the history
Related to MRGFY-4457
  • Loading branch information
jd authored Dec 17, 2024
1 parent 9ce61a7 commit 3c76330
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 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.
17 changes: 17 additions & 0 deletions pytest_mergify/resources/ci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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:
if (provider := utils.get_ci_provider()) is None:
return Resource({})

return Resource(
{
"cicd.provider.name": provider,
}
)
3 changes: 2 additions & 1 deletion pytest_mergify/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import typing

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

SUPPORTED_CIs: dict[str, CIProviderT] = {
"GITHUB_ACTIONS": "github_actions",
"CIRCLECI": "circleci",
"_PYTEST_MERGIFY_TEST": "pytest_mergify_suite",
}


Expand Down
14 changes: 14 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import _pytest.pytester
import _pytest.config

from pytest_mergify import utils


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"] == utils.get_ci_provider()

0 comments on commit 3c76330

Please sign in to comment.