Skip to content

Commit d8d5c44

Browse files
authored
AGE-237 : Adding VCS Metadata from .git (#61)
* Moving VCS info to resource attributes * Added VCS Metadata via git SDK
1 parent 7ada9fa commit d8d5c44

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

middleware/distro.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ def custom_record_exception(span: Span, exc: Exception):
228228
# Determine if the exception is escaping
229229
current_exc = sys.exc_info()[1] # Get the currently active exception
230230
exception_escaped = current_exc is exc # True if it's still propagating
231-
232-
mw_vcs_repository_url = os.getenv("MW_VCS_REPOSITORY_URL")
233-
mw_vcs_commit_sha = os.getenv("MW_VCS_COMMIT_SHA")
234231

235232
# Serialize stack info as JSON string since OpenTelemetry only supports string values
236233
stack_info_str = json.dumps(stack_info, indent=2)
@@ -244,8 +241,6 @@ def custom_record_exception(span: Span, exc: Exception):
244241
"exception.language": "python",
245242
"exception.stacktrace": traceback.format_exc(),
246243
"exception.escaped": exception_escaped,
247-
"exception.vcs.commit_sha": mw_vcs_commit_sha or "",
248-
"exception.vcs.repository_url": mw_vcs_repository_url or "",
249244
"exception.stack_details": stack_info_str, # Attach full stacktrace details
250245
}
251246
)

middleware/resource.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from middleware.detectors.detector import process_detector_input, get_detectors
1313
from middleware.version import __version__
1414
import typing
15+
import os
16+
import git
1517

1618
_logger = getLogger(__name__)
1719

@@ -54,6 +56,23 @@ def create_resource(options: MWOptions):
5456
"Skipped Custom attributes: parsing error expected format `abcd=1234,wxyz=5678`"
5557
)
5658

59+
mw_vcs_repository_url = os.getenv("MW_VCS_REPOSITORY_URL")
60+
mw_vcs_commit_sha = os.getenv("MW_VCS_COMMIT_SHA")
61+
62+
# Fallback to git if env vars are missing (independent logic, single Repo call)
63+
git_url, git_sha = None, None
64+
if not mw_vcs_repository_url or not mw_vcs_commit_sha:
65+
git_url, git_sha = get_git_info()
66+
if not mw_vcs_repository_url and git_url:
67+
mw_vcs_repository_url = git_url
68+
if not mw_vcs_commit_sha and git_sha:
69+
mw_vcs_commit_sha = git_sha
70+
71+
if mw_vcs_repository_url:
72+
attributes["vcs.repository_url"] = mw_vcs_repository_url
73+
if mw_vcs_commit_sha:
74+
attributes["vcs.commit_sha"] = mw_vcs_commit_sha
75+
5776
detectors: typing.List["ResourceDetector"] = [
5877
OTELResourceDetector(),
5978
ProcessResourceDetector(),
@@ -70,3 +89,19 @@ def create_resource(options: MWOptions):
7089
initial_resource=Resource.create(attributes),
7190
)
7291
return resources
92+
93+
94+
def get_git_info():
95+
"""
96+
Returns (repository_url, commit_sha) from the local .git directory.
97+
Returns (None, None) if not a git repo or info unavailable.
98+
"""
99+
try:
100+
repo = git.Repo(search_parent_directories=True)
101+
commit_sha = repo.head.commit.hexsha
102+
remote_url = None
103+
if repo.remotes:
104+
remote_url = repo.remotes.origin.url
105+
return remote_url, commit_sha
106+
except Exception:
107+
return None, None

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "middleware-io"
7-
version = "2.2.1"
7+
version = "2.3.0"
88
requires-python = ">=3.8"
99
description = "Middleware's APM tool enables Python developers to effortlessly monitor their applications, gathering distributed tracing, metrics, logs, and profiling data for valuable insights and performance optimization."
1010
authors = [{ name = "middleware-dev" }]
@@ -40,6 +40,7 @@ dependencies =[
4040
"opentelemetry-resource-detector-azure==0.1.5",
4141
"opentelemetry-resourcedetector-gcp==1.7.0a0",
4242
"opentelemetry-resourcedetector-docker==0.4.0",
43+
"gitpython>=3.1.0",
4344

4445
"opentelemetry-instrumentation-aio-pika==0.48b0",
4546
"opentelemetry-instrumentation-aiohttp-client==0.48b0",

0 commit comments

Comments
 (0)