diff --git a/tests/e2e/c/dynamic_fees.go b/tests/e2e/c/dynamic_fees.go index 6b554087a996..4e3c3e506549 100644 --- a/tests/e2e/c/dynamic_fees.go +++ b/tests/e2e/c/dynamic_fees.go @@ -39,6 +39,9 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() { privateNetwork := tmpnet.NewDefaultNetwork("avalanchego-e2e-dynamic-fees") e2e.GetEnv(tc).StartPrivateNetwork(privateNetwork) + // Ensure the metrics link for the test is for the network + e2e.NetworkUUIDForCurrentSpec = privateNetwork.UUID + tc.By("allocating a pre-funded key") key := privateNetwork.PreFundedKeys[0] ethAddress := evm.GetEthAddress(key) diff --git a/tests/fixture/e2e/env.go b/tests/fixture/e2e/env.go index a8b10c19c9fa..f39961dfb522 100644 --- a/tests/fixture/e2e/env.go +++ b/tests/fixture/e2e/env.go @@ -58,6 +58,9 @@ type TestEnvironment struct { // Retrieve the test environment configured with the provided test context. func GetEnv(tc tests.TestContext) *TestEnvironment { + if env == nil { + return nil + } return &TestEnvironment{ NetworkDir: env.NetworkDir, URIs: env.URIs, diff --git a/tests/fixture/e2e/flags.go b/tests/fixture/e2e/flags.go index 05d698ce6262..f87132b0c974 100644 --- a/tests/fixture/e2e/flags.go +++ b/tests/fixture/e2e/flags.go @@ -12,6 +12,10 @@ import ( "github.com/ava-labs/avalanchego/tests/fixture/tmpnet" ) +// Ensure that this value takes into account the scrape_interval +// defined in scripts/run_prometheus.sh. +const networkShutdownDelay = 12 * time.Second + type FlagVars struct { avalancheGoExecPath string pluginDir string @@ -52,9 +56,8 @@ func (v *FlagVars) RestartNetwork() bool { func (v *FlagVars) NetworkShutdownDelay() time.Duration { if v.delayNetworkShutdown { - // Only return a non-zero value if the delay is enabled. Make sure this value takes - // into account the scrape_interval defined in scripts/run_prometheus.sh. - return 12 * time.Second + // Only return a non-zero value if the delay is enabled. + return networkShutdownDelay } return 0 } diff --git a/tests/fixture/e2e/metrics_link.go b/tests/fixture/e2e/metrics_link.go new file mode 100644 index 000000000000..3cc7323db5b2 --- /dev/null +++ b/tests/fixture/e2e/metrics_link.go @@ -0,0 +1,58 @@ +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package e2e + +import ( + "strconv" + + "github.com/ava-labs/avalanchego/tests/fixture/tmpnet" + + ginkgo "github.com/onsi/ginkgo/v2" +) + +// The ginkgo event handlers defined in this file will be automatically +// applied to all ginkgo suites importing this package. + +// The UUID of the network that should be used to compose the metrics link +// for the current spec. +var NetworkUUIDForCurrentSpec string + +// This event handler attempts to ensure that the UUID of the network +// targeted by the current spec is retained for use by the AfterEach +// handler. If the test uses a private network, it can override this value by +// setting NetworkUUIDForCurrentSpec directly. +// +// TODO(marun) Make this conditional on metrics collection being enabled +var _ = ginkgo.BeforeEach(func() { + tc := NewTestContext() + env := GetEnv(tc) + if env == nil { + // Not possible to discover the uuid of the test network in this + // event handler without a global env. + return + } + NetworkUUIDForCurrentSpec = env.GetNetwork().UUID +}) + +// This event handler attempts to emit a metrics link scoped to the duration +// of the current spec. +// +// TODO(marun) Make this conditional on metrics collection being enabled +var _ = ginkgo.AfterEach(func() { + if len(NetworkUUIDForCurrentSpec) == 0 { + // Composition of the metrics link requires a network UUID + return + } + specReport := ginkgo.CurrentSpecReport() + startTime := specReport.StartTime.UnixMilli() + // Extend the end time by the metrics scrape duration to ensure the + // specified duration includes all details relevant to a given test. + endTime := specReport.StartTime.Add(networkShutdownDelay).UnixMilli() + metricsLink := tmpnet.MetricsLinkForNetwork( + NetworkUUIDForCurrentSpec, + strconv.FormatInt(startTime, 10), + strconv.FormatInt(endTime, 10), + ) + NewTestContext().Outf("Test Metrics: %s\n", metricsLink) +}) diff --git a/tests/fixture/tmpnet/network.go b/tests/fixture/tmpnet/network.go index 3eec5cdf7a06..85eb7d56d88f 100644 --- a/tests/fixture/tmpnet/network.go +++ b/tests/fixture/tmpnet/network.go @@ -373,7 +373,7 @@ func (n *Network) StartNodes(ctx context.Context, w io.Writer, nodesToStart ...* return err } // Provide a link to the main dashboard filtered by the uuid and showing results from now till whenever the link is viewed - if _, err := fmt.Fprintf(w, "\nMetrics: https://grafana-poc.avax-dev.network/d/kBQpRdWnk/avalanche-main-dashboard?&var-filter=network_uuid%%7C%%3D%%7C%s&var-filter=is_ephemeral_node%%7C%%3D%%7Cfalse&from=%d&to=now\n", n.UUID, startTime.UnixMilli()); err != nil { + if _, err := fmt.Fprintf(w, "\nMetrics: %s\n", MetricsLinkForNetwork(n.UUID, strconv.FormatInt(startTime.UnixMilli(), 10), "")); err != nil { return err } @@ -1009,3 +1009,21 @@ func getRPCVersion(command string, versionArgs ...string) (uint64, error) { return version.RPCChainVM, nil } + +// MetricsLinkForNetwork returns a link to the default metrics dashboard for the network +// with the given UUID. The start and end times are accepted as strings to support the +// use of Grafana's time range syntax (e.g. `now`, `now-1h`). +func MetricsLinkForNetwork(networkUUID string, startTime string, endTime string) string { + if startTime == "" { + startTime = "now-1h" + } + if endTime == "" { + endTime = "now" + } + return fmt.Sprintf( + "https://grafana-poc.avax-dev.network/d/kBQpRdWnk/avalanche-main-dashboard?&var-filter=network_uuid%%7C%%3D%%7C%s&var-filter=is_ephemeral_node%%7C%%3D%%7Cfalse&from=%s&to=%s", + networkUUID, + startTime, + endTime, + ) +}