Skip to content

Commit 5a02656

Browse files
committed
[e2e] Emit a duration-scoped grafana link for each test
1 parent f30febb commit 5a02656

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

tests/e2e/c/dynamic_fees.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
3939
privateNetwork := tmpnet.NewDefaultNetwork("avalanchego-e2e-dynamic-fees")
4040
e2e.GetEnv(tc).StartPrivateNetwork(privateNetwork)
4141

42+
// Ensure the metrics link for the test is for the targeted network
43+
e2e.NetworkUUIDForCurrentSpec = privateNetwork.UUID
44+
4245
tc.By("allocating a pre-funded key")
4346
key := privateNetwork.PreFundedKeys[0]
4447
ethAddress := evm.GetEthAddress(key)

tests/fixture/e2e/env.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type TestEnvironment struct {
5858

5959
// Retrieve the test environment configured with the provided test context.
6060
func GetEnv(tc tests.TestContext) *TestEnvironment {
61+
if env == nil {
62+
return nil
63+
}
6164
return &TestEnvironment{
6265
NetworkDir: env.NetworkDir,
6366
URIs: env.URIs,

tests/fixture/e2e/flags.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
1313
)
1414

15+
// Ensure that this value takes into account the scrape_interval
16+
// defined in scripts/run_prometheus.sh.
17+
const networkShutdownDelay = 12 * time.Second
18+
1519
type FlagVars struct {
1620
avalancheGoExecPath string
1721
pluginDir string
@@ -52,9 +56,8 @@ func (v *FlagVars) RestartNetwork() bool {
5256

5357
func (v *FlagVars) NetworkShutdownDelay() time.Duration {
5458
if v.delayNetworkShutdown {
55-
// Only return a non-zero value if the delay is enabled. Make sure this value takes
56-
// into account the scrape_interval defined in scripts/run_prometheus.sh.
57-
return 12 * time.Second
59+
// Only return a non-zero value if the delay is enabled.
60+
return networkShutdownDelay
5861
}
5962
return 0
6063
}

tests/fixture/e2e/metrics_link.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package e2e
5+
6+
import (
7+
"strconv"
8+
9+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
10+
11+
ginkgo "github.com/onsi/ginkgo/v2"
12+
)
13+
14+
// The ginkgo event handlers defined in this file will be automatically
15+
// applied to all ginkgo suites importing this package.
16+
17+
// The UUID of the network that should be used to compose the metrics link
18+
// for the current spec.
19+
var NetworkUUIDForCurrentSpec string
20+
21+
// This event handler attempts to ensure that the UUID of the network
22+
// targeted by the current spec is retained for use by the AfterEach
23+
// handler. If the test uses a private network, it can override this value by
24+
// setting NetworkUUIDForCurrentSpec directly.
25+
//
26+
// TODO(marun) Make this conditional on metrics collection being enabled
27+
var _ = ginkgo.BeforeEach(func() {
28+
tc := NewTestContext()
29+
env := GetEnv(tc)
30+
if env == nil {
31+
// Not possible to discover the uuid of the test network in this
32+
// event handler without a global env.
33+
return
34+
}
35+
NetworkUUIDForCurrentSpec = env.GetNetwork().UUID
36+
})
37+
38+
// This event handler attempts to emit a metrics link scoped to the duration
39+
// of the current spec.
40+
//
41+
// TODO(marun) Make this conditional on metrics collection being enabled
42+
var _ = ginkgo.AfterEach(func() {
43+
if len(NetworkUUIDForCurrentSpec) == 0 {
44+
// Composition of the metrics link requires a network UUID
45+
return
46+
}
47+
specReport := ginkgo.CurrentSpecReport()
48+
startTime := specReport.StartTime.UnixMilli()
49+
// Extend the end time by the metrics scrape duration to ensure the
50+
// specified duration includes all details relevant to a given test.
51+
endTime := specReport.StartTime.Add(networkShutdownDelay).UnixMilli()
52+
metricsLink := tmpnet.MetricsLinkForNetwork(
53+
NetworkUUIDForCurrentSpec,
54+
strconv.FormatInt(startTime, 10),
55+
strconv.FormatInt(endTime, 10),
56+
)
57+
NewTestContext().Outf("Test Metrics: %s\n", metricsLink)
58+
})

tests/fixture/tmpnet/network.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (n *Network) StartNodes(ctx context.Context, w io.Writer, nodesToStart ...*
373373
return err
374374
}
375375
// Provide a link to the main dashboard filtered by the uuid and showing results from now till whenever the link is viewed
376-
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 {
376+
if _, err := fmt.Fprintf(w, "\nMetrics: %s\n", MetricsLinkForNetwork(n.UUID, strconv.FormatInt(startTime.UnixMilli(), 10), "")); err != nil {
377377
return err
378378
}
379379

@@ -1009,3 +1009,21 @@ func getRPCVersion(command string, versionArgs ...string) (uint64, error) {
10091009

10101010
return version.RPCChainVM, nil
10111011
}
1012+
1013+
// MetricsLinkForNetwork returns a link to the default metrics dashboard for the network
1014+
// with the given UUID. The start and end times are accepted as strings to support the
1015+
// use of Grafana's time range syntax (e.g. `now`, `now-1h`).
1016+
func MetricsLinkForNetwork(networkUUID string, startTime string, endTime string) string {
1017+
if startTime == "" {
1018+
startTime = "now-1h"
1019+
}
1020+
if endTime == "" {
1021+
endTime = "now"
1022+
}
1023+
return fmt.Sprintf(
1024+
"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",
1025+
networkUUID,
1026+
startTime,
1027+
endTime,
1028+
)
1029+
}

0 commit comments

Comments
 (0)