pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/DataDog/datadog-agent/commit/747e97f97af603faa374fe84548ada2e89cea075

ss" /> [ABLD-250] Bazel repo rule to get jmxfetch. (#46048) · DataDog/datadog-agent@747e97f · GitHub
Skip to content

Commit 747e97f

Browse files
authored
[ABLD-250] Bazel repo rule to get jmxfetch. (#46048)
30 minutes with Claude. It works well enough that I am happy to check this in, and tweak it (if required) when we start to try to use it. - I pointed it to deps/windows for a repo rule that can read release.json (which is where we get the version) - I to an example of a release version from https://datadoghq.atlassian.net/wiki/spaces/AM/pages/3090350700/Using+SNAPSHOT+build+in+the+Agent - I pointed it at the analysis test for bazel/rules/template_expand_test.bzl. to learn about starlark testing - I told it to look at anything in deps to make the license() rule. I'm not ready to modify the omnibus step yet. I want to understand the difference between the jmx and non-jmx flavors first. Co-authored-by: tony.aiuto <tony.aiuto@datadoghq.com>
1 parent 1ef6c91 commit 747e97f

File tree

8 files changed

+353
-0
lines changed

8 files changed

+353
-0
lines changed

.adms/bazel/adms.mirror.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ rewrite (registry.npmjs.org)/(.*) depot-read-api-bzl.us1.ddbuild.io/$1/$2
126126
# TODO: Move to Depot Java when we can determine why requests to repo1.maven.org
127127
# are still being made.
128128
rewrite (repo1.maven.org)/(.*) depot-read-api-bzl.us1.ddbuild.io/$1/$2
129+
rewrite (central.sonatype.com)/(.*) depot-read-api-bzl.us1.ddbuild.io/$1/$2
129130

130131
# TODO: We don't support Rust in ADMS yet, but once we do, this can go away. In
131132
# the meantime, it's a kind of "back door" to mirroring Rust artifacts internally.

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ include("//deps/freetds:freetds.MODULE.bazel")
238238
# buildifier: leave-alone
239239
include("//deps/gstatus:gstatus.MODULE.bazel")
240240

241+
# buildifier: leave-alone
242+
include("//deps/jmxfetch:jmxfetch.MODULE.bazel")
243+
241244
# buildifier: leave-alone
242245
include("//deps/nfsiostat:nfsiostat.MODULE.bazel")
243246

deps/jmxfetch/BUILD.bazel

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# jmxfetch JAR file.
2+
3+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
4+
load("@rules_pkg//pkg:install.bzl", "pkg_install")
5+
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
6+
load(":module_utils_test.bzl", "jmxfetch_module_utils_test_suite")
7+
8+
package(default_visibility = ["//packages:__subpackages__"])
9+
10+
pkg_files(
11+
name = "all_files",
12+
srcs = [
13+
"@jmxfetch//:jmxfetch.jar",
14+
],
15+
prefix = "bin/agent/dist/jmx",
16+
)
17+
18+
pkg_install(
19+
name = "install",
20+
srcs = [
21+
":all_files",
22+
],
23+
)
24+
25+
# Bazel library declarations
26+
bzl_library(
27+
name = "module_utils_bzl",
28+
srcs = ["module_utils.bzl"],
29+
visibility = ["//visibility:public"],
30+
)
31+
32+
bzl_library(
33+
name = "test_helpers_bzl",
34+
srcs = ["test_helpers.bzl"],
35+
deps = [":module_utils_bzl"],
36+
)
37+
38+
# Tests
39+
jmxfetch_module_utils_test_suite(name = "_jmxfetch_module_utils_tests")

deps/jmxfetch/jmxfetch.BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@rules_license//rules:license.bzl", "license")
2+
3+
package(
4+
default_package_metadata = [":license"],
5+
default_visibility = ["//visibility:public"],
6+
)
7+
8+
license(
9+
name = "license",
10+
license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause"],
11+
license_text = "LICENSE",
12+
visibility = ["//visibility:public"],
13+
)
14+
15+
exports_files(glob(["**"]))
16+
17+
filegroup(
18+
name = "file",
19+
srcs = ["{target_filename}"],
20+
)
21+
22+
filegroup(
23+
name = "license_file",
24+
srcs = ["LICENSE"],
25+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Fetch jmxfetch JAR based on current values in //release.json."""
2+
3+
get_jmxfetch_using_release_constants = use_repo_rule("//deps/jmxfetch:module_utils.bzl", "get_jmxfetch_using_release_constants")
4+
5+
get_jmxfetch_using_release_constants(
6+
name = "jmxfetch",
7+
)

deps/jmxfetch/module_utils.bzl

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
"""MODULE wrapper to download jmxfetch JAR using variables from release.json.
2+
3+
This loads 'dependencies' from release.json and uses that to construct the
4+
appropriate Maven repository URL based on the jmxfetch version type:
5+
- Release versions (X.Y.Z) use Maven Central repo
6+
- Snapshot versions (X.Y.Z-identifier-timestamp) use Maven Snapshots repo
7+
8+
Usage:
9+
get_jmxfetch_using_release_constants = use_repo_rule(":module_utils.bzl", "get_jmxfetch_using_release_constants")
10+
11+
get_jmxfetch_using_release_constants(
12+
name = "jmxfetch",
13+
)
14+
"""
15+
16+
load("@bazel_tools//tools/build_defs/repo:cache.bzl", "DEFAULT_CANONICAL_ID_ENV", "get_default_canonical_id")
17+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "get_auth")
18+
19+
get_jmxfetch_using_release_constants_attrs = {
20+
"executable": attr.bool(
21+
default = False,
22+
doc = "If the downloaded file should be made executable.",
23+
),
24+
"target_filename": attr.string(
25+
default = "jmxfetch.jar",
26+
doc = "Name assigned to the downloaded file.",
27+
),
28+
"canonical_id": attr.string(),
29+
"_release_info": attr.label(default = "//:release.json", allow_single_file = True),
30+
}
31+
32+
def parse_jmxfetch_version(version):
33+
"""Parse jmxfetch version to determine if it's release or snapshot.
34+
35+
Args:
36+
version: jmxfetch version
37+
38+
Returns:
39+
dict: is_snapshot, url: full download URL
40+
"""
41+
parts = version.split(".")
42+
43+
# Release version has exactly 3 parts (X.Y.Z)
44+
if len(parts) == 3:
45+
# It's a release version
46+
url = "https://repo1.maven.org/maven2/com/datadoghq/jmxfetch/{VERSION}/jmxfetch-{VERSION}-jar-with-dependencies.jar".format(VERSION = version)
47+
return {
48+
"is_snapshot": False,
49+
"url": url,
50+
}
51+
52+
# Otherwise it's a snapshot version like 0.48.0-20230706.234900
53+
# Parse using regex to extract components
54+
for i in range(len(version)):
55+
if version[i] == "-":
56+
# Found first dash
57+
before_dash = version[:i]
58+
after_dash = version[i + 1:]
59+
60+
# Check if before_dash is X.Y.Z format
61+
before_parts = before_dash.split(".")
62+
if len(before_parts) == 3:
63+
# Found the base version, now parse the rest
64+
# after_dash should be like "20230706.234900"
65+
# We need to split this into snapshot_version and timestamp
66+
after_parts = after_dash.split(".")
67+
if len(after_parts) >= 2:
68+
# snapshot_version is base + dash + date part
69+
snapshot_version = before_dash + "-" + after_parts[0]
70+
71+
# timestamp is everything after the dot
72+
timestamp = ".".join(after_parts[1:])
73+
74+
# Reconstruct timestamped_version as base_version-timestamp
75+
timestamped_version = before_dash + "-" + timestamp
76+
77+
url = "https://central.sonatype.com/repository/maven-snapshots/com/datadoghq/jmxfetch/{SNAPSHOT_VERSION}/jmxfetch-{TIMESTAMPED_VERSION}-jar-with-dependencies.jar".format(
78+
SNAPSHOT_VERSION = snapshot_version,
79+
TIMESTAMPED_VERSION = timestamped_version,
80+
)
81+
return {
82+
"is_snapshot": True,
83+
"url": url,
84+
}
85+
86+
# Fallback: treat as snapshot and construct best-effort URL
87+
url = "https://central.sonatype.com/repository/maven-snapshots/com/datadoghq/jmxfetch/{VERSION}/jmxfetch-{VERSION}-jar-with-dependencies.jar".format(VERSION = version)
88+
return {
89+
"is_snapshot": True,
90+
"url": url,
91+
}
92+
93+
def _get_jmxfetch_using_release_constants_impl(rctx):
94+
"""Implementation of the get_jmxfetch_using_release_constants rule."""
95+
release_info = json.decode(rctx.read(rctx.path(rctx.attr._release_info)))
96+
vars = release_info["dependencies"]
97+
98+
version = vars["JMXFETCH_VERSION"]
99+
sha256 = vars["JMXFETCH_HASH"]
100+
101+
# Parse version and get URL
102+
version_info = parse_jmxfetch_version(version)
103+
url = version_info["url"]
104+
is_snapshot = version_info["is_snapshot"]
105+
106+
# Determine license file version: 'master' for snapshots, version for releases
107+
license_file_version = "master" if is_snapshot else version
108+
109+
repo_root = rctx.path(".")
110+
forbidden_files = [
111+
repo_root,
112+
rctx.path("MODULE.bazel"),
113+
rctx.path("BUILD"),
114+
]
115+
target_filename = rctx.attr.target_filename
116+
download_path = rctx.path("file/" + target_filename)
117+
118+
if download_path in forbidden_files or not str(download_path).startswith(str(repo_root)):
119+
fail("'%s' cannot be used as target_filename in get_jmxfetch_using_release_constants" % rctx.attr.target_filename)
120+
121+
# Download the JAR file
122+
rctx.download(
123+
[url],
124+
target_filename,
125+
sha256,
126+
rctx.attr.executable,
127+
canonical_id = rctx.attr.canonical_id or get_default_canonical_id(rctx, [url]),
128+
auth = get_auth(rctx, [url]),
129+
)
130+
131+
# Download the LICENSE file
132+
license_url = "https://raw.githubusercontent.com/DataDog/jmxfetch/{version}/LICENSE".format(version = license_file_version)
133+
rctx.download(
134+
[license_url],
135+
"LICENSE",
136+
)
137+
138+
rctx.file("MODULE.bazel", "module(name = \"{name}\")\n".format(name = rctx.name))
139+
140+
# Use template to generate BUILD file
141+
rctx.template(
142+
"BUILD",
143+
Label("//deps/jmxfetch:jmxfetch.BUILD.bazel"),
144+
substitutions = {
145+
"{target_filename}": target_filename,
146+
},
147+
)
148+
149+
return rctx.repo_metadata(reproducible = True)
150+
151+
get_jmxfetch_using_release_constants = repository_rule(
152+
implementation = _get_jmxfetch_using_release_constants_impl,
153+
attrs = get_jmxfetch_using_release_constants_attrs,
154+
environ = [DEFAULT_CANONICAL_ID_ENV],
155+
)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Tests for jmxfetch module_utils version parsing."""
2+
3+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
4+
load("@rules_testing//lib:util.bzl", "util")
5+
load(":test_helpers.bzl", "VersionParseInfo", "version_parse_test_rule")
6+
7+
def jmxfetch_module_utils_test_suite(name):
8+
test_suite(
9+
name = name,
10+
tests = [
11+
_parse_release_version_test,
12+
_parse_snapshot_version_test,
13+
],
14+
)
15+
16+
def _parse_release_version_test(name):
17+
util.helper_target(
18+
version_parse_test_rule,
19+
name = name + "_release_test_target",
20+
version = "0.51.0",
21+
out = "release_test.txt",
22+
)
23+
analysis_test(
24+
name = name,
25+
impl = _release_version_test_impl,
26+
target = name + "_release_test_target",
27+
)
28+
29+
def _release_version_test_impl(env, target):
30+
# buildifier: disable=unused-variable
31+
subject = env.expect.that_target(target)
32+
33+
# Get the parsing result from the provider
34+
parse_info = target[VersionParseInfo]
35+
36+
# Verify the release version was correctly parsed
37+
env.expect.that_str(parse_info.version).equals("0.51.0")
38+
env.expect.that_bool(parse_info.is_snapshot).equals(False)
39+
env.expect.that_str(parse_info.url).equals(
40+
"https://repo1.maven.org/maven2/com/datadoghq/jmxfetch/0.51.0/jmxfetch-0.51.0-jar-with-dependencies.jar",
41+
)
42+
43+
def _parse_snapshot_version_test(name):
44+
util.helper_target(
45+
version_parse_test_rule,
46+
name = name + "_snapshot_test_target",
47+
version = "0.48.0-20230706.234900",
48+
out = "snapshot_test.txt",
49+
)
50+
analysis_test(
51+
name = name,
52+
impl = _snapshot_version_test_impl,
53+
target = name + "_snapshot_test_target",
54+
)
55+
56+
def _snapshot_version_test_impl(env, target):
57+
# buildifier: disable=unused-variable
58+
subject = env.expect.that_target(target)
59+
60+
# Get the parsing result from the provider
61+
parse_info = target[VersionParseInfo]
62+
63+
# Verify the snapshot version was correctly parsed
64+
env.expect.that_str(parse_info.version).equals("0.48.0-20230706.234900")
65+
env.expect.that_bool(parse_info.is_snapshot).equals(True)
66+
env.expect.that_str(parse_info.url).equals(
67+
"https://central.sonatype.com/repository/maven-snapshots/com/datadoghq/jmxfetch/0.48.0-20230706/jmxfetch-0.48.0-234900-jar-with-dependencies.jar",
68+
)

deps/jmxfetch/test_helpers.bzl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Test helpers for jmxfetch module_utils."""
2+
3+
load(":module_utils.bzl", "parse_jmxfetch_version")
4+
5+
# Define a provider to expose version parsing results
6+
VersionParseInfo = provider(
7+
doc = "Information about a parsed jmxfetch version",
8+
fields = {
9+
"version": "The input version string",
10+
"is_snapshot": "Whether the version is a snapshot",
11+
"url": "The constructed download URL",
12+
},
13+
)
14+
15+
version_parse_test_rule_attrs = {
16+
"version": attr.string(doc = "Version string to parse"),
17+
"out": attr.output(doc = "Output file"),
18+
}
19+
20+
def _version_parse_test_rule_impl(ctx):
21+
"""Test rule that parses a version and outputs the result."""
22+
23+
# Call the actual parsing function
24+
version = ctx.attr.version
25+
version_info = parse_jmxfetch_version(version)
26+
27+
# Write the result to the output file for debugging
28+
output_content = """Version: {version}
29+
is_snapshot: {is_snapshot}
30+
url: {url}
31+
""".format(
32+
version = version,
33+
is_snapshot = version_info["is_snapshot"],
34+
url = version_info["url"],
35+
)
36+
37+
ctx.actions.write(
38+
output = ctx.outputs.out,
39+
content = output_content,
40+
)
41+
42+
# Return both the file and a provider with the parsed info
43+
return [
44+
DefaultInfo(files = depset([ctx.outputs.out])),
45+
VersionParseInfo(
46+
version = version,
47+
is_snapshot = version_info["is_snapshot"],
48+
url = version_info["url"],
49+
),
50+
]
51+
52+
version_parse_test_rule = rule(
53+
implementation = _version_parse_test_rule_impl,
54+
attrs = version_parse_test_rule_attrs,
55+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy