Skip to content

Commit 5ccfadb

Browse files
committed
[tmpnet] Move monitoring label handling to node
1 parent 2cf574c commit 5ccfadb

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

tests/fixture/tmpnet/check_monitoring.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"math"
1414
"net/http"
1515
"net/url"
16+
"os"
1617
"strconv"
1718
"strings"
1819
"time"
@@ -271,13 +272,11 @@ func getSelectors(networkUUID string) (string, error) {
271272

272273
// Fall back to using Github labels as selectors
273274
selectors := []string{}
274-
githubLabels := githubLabelsFromEnv()
275-
for label := range githubLabels {
276-
value := githubLabels[label]
277-
if len(value) == 0 {
278-
continue
275+
for _, label := range githubLabels {
276+
value := os.Getenv(strings.ToUpper(label))
277+
if len(value) > 0 {
278+
selectors = append(selectors, fmt.Sprintf(`%s="%s"`, label, value))
279279
}
280-
selectors = append(selectors, fmt.Sprintf(`%s="%s"`, label, value))
281280
}
282281
if len(selectors) == 0 {
283282
return "", errors.New("no GH_* env vars set to use for selectors")

tests/fixture/tmpnet/node.go

+34
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net"
1414
"net/http"
1515
"net/netip"
16+
"os"
1617
"strconv"
1718
"strings"
1819
"time"
@@ -37,6 +38,16 @@ var (
3738
errMissingTLSKeyForNodeID = fmt.Errorf("failed to ensure node ID: missing value for %q", config.StakingTLSKeyContentKey)
3839
errMissingCertForNodeID = fmt.Errorf("failed to ensure node ID: missing value for %q", config.StakingCertContentKey)
3940
errInvalidKeypair = fmt.Errorf("%q and %q must be provided together or not at all", config.StakingTLSKeyContentKey, config.StakingCertContentKey)
41+
42+
// Labels expected to be available in the environment when running in GitHub Actions
43+
githubLabels = []string{
44+
"gh_repo",
45+
"gh_workflow",
46+
"gh_run_id",
47+
"gh_run_number",
48+
"gh_run_attempt",
49+
"gh_job_id",
50+
}
4051
)
4152

4253
// NodeRuntime defines the methods required to support running a node.
@@ -418,3 +429,26 @@ func (n *Node) WaitForHealthy(ctx context.Context) error {
418429
}
419430
}
420431
}
432+
433+
// getMonitoringLabels retrieves the map of labels and their values to be
434+
// applied to metrics and logs collected from the node.
435+
func (n *Node) getMonitoringLabels() map[string]string {
436+
labels := map[string]string{
437+
// Explicitly setting an instance label avoids the default
438+
// behavior of using the node's URI since the URI isn't
439+
// guaranteed stable (e.g. port may change after restart).
440+
"instance": n.GetUniqueID(),
441+
"network_uuid": n.network.UUID,
442+
"node_id": n.NodeID.String(),
443+
"is_ephemeral_node": strconv.FormatBool(n.IsEphemeral),
444+
"network_owner": n.network.Owner,
445+
}
446+
// Include the values of github labels if available
447+
for _, label := range githubLabels {
448+
value := os.Getenv(strings.ToUpper(label))
449+
if len(value) > 0 {
450+
labels[label] = value
451+
}
452+
}
453+
return labels
454+
}

tests/fixture/tmpnet/process_runtime.go

+1-26
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"os"
1717
"os/exec"
1818
"path/filepath"
19-
"strconv"
2019
"strings"
2120
"syscall"
2221
"time"
@@ -322,17 +321,7 @@ func getProcess(pid int) (*os.Process, error) {
322321
// Write monitoring configuration enabling collection of metrics and logs from the node.
323322
func (p *ProcessRuntime) writeMonitoringConfig() error {
324323
// Ensure labeling that uniquely identifies the node and its network
325-
commonLabels := FlagsMap{
326-
// Explicitly setting an instance label avoids the default
327-
// behavior of using the node's URI since the URI isn't
328-
// guaranteed stable (e.g. port may change after restart).
329-
"instance": p.node.GetUniqueID(),
330-
"network_uuid": p.node.network.UUID,
331-
"node_id": p.node.NodeID.String(),
332-
"is_ephemeral_node": strconv.FormatBool(p.node.IsEphemeral),
333-
"network_owner": p.node.network.Owner,
334-
}
335-
commonLabels.SetDefaults(githubLabelsFromEnv())
324+
commonLabels := p.node.getMonitoringLabels()
336325

337326
prometheusConfig := []ConfigMap{
338327
{
@@ -479,17 +468,3 @@ func watchLogFileForFatal(ctx context.Context, cancelWithCause context.CancelCau
479468
}
480469
}
481470
}
482-
483-
func githubLabelsFromEnv() map[string]string {
484-
return map[string]string{
485-
// prometheus/promtail ignore empty values so including these
486-
// labels with empty values outside of a github worker (where
487-
// the env vars will not be set) should not be a problem.
488-
"gh_repo": os.Getenv("GH_REPO"),
489-
"gh_workflow": os.Getenv("GH_WORKFLOW"),
490-
"gh_run_id": os.Getenv("GH_RUN_ID"),
491-
"gh_run_number": os.Getenv("GH_RUN_NUMBER"),
492-
"gh_run_attempt": os.Getenv("GH_RUN_ATTEMPT"),
493-
"gh_job_id": os.Getenv("GH_JOB_ID"),
494-
}
495-
}

0 commit comments

Comments
 (0)