Skip to content

Commit 4785d43

Browse files
committed
[tmpnet] Move monitoring label handling to node
1 parent e0304c5 commit 4785d43

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"
@@ -324,17 +323,7 @@ func getProcess(pid int) (*os.Process, error) {
324323
// Write monitoring configuration enabling collection of metrics and logs from the node.
325324
func (p *ProcessRuntime) writeMonitoringConfig() error {
326325
// Ensure labeling that uniquely identifies the node and its network
327-
commonLabels := FlagsMap{
328-
// Explicitly setting an instance label avoids the default
329-
// behavior of using the node's URI since the URI isn't
330-
// guaranteed stable (e.g. port may change after restart).
331-
"instance": p.node.GetUniqueID(),
332-
"network_uuid": p.node.network.UUID,
333-
"node_id": p.node.NodeID.String(),
334-
"is_ephemeral_node": strconv.FormatBool(p.node.IsEphemeral),
335-
"network_owner": p.node.network.Owner,
336-
}
337-
commonLabels.SetDefaults(githubLabelsFromEnv())
326+
commonLabels := p.node.getMonitoringLabels()
338327

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

0 commit comments

Comments
 (0)