Skip to content

Commit ca0228a

Browse files
[e2e] Emit a duration-scoped grafana link for each test (#3340)
Signed-off-by: marun <maru.newby@avalabs.org> Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
1 parent ccf2612 commit ca0228a

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

tests/e2e/c/dynamic_fees.go

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

42+
// Avoid emitting a spec-scoped metrics link for the shared
43+
// network since the link emitted by the start of the private
44+
// network is more relevant.
45+
//
46+
// TODO(marun) Make this implicit to the start of a private network
47+
e2e.EmitMetricsLink = false
48+
4249
tc.By("allocating a pre-funded key")
4350
key := privateNetwork.PreFundedKeys[0]
4451
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
"time"
9+
10+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
11+
12+
ginkgo "github.com/onsi/ginkgo/v2"
13+
)
14+
15+
// The ginkgo event handlers defined in this file will be automatically
16+
// applied to all ginkgo suites importing this package.
17+
18+
// Whether a spec-scoped metrics link should be emitted after the current
19+
// spec finishes executing.
20+
var EmitMetricsLink bool
21+
22+
// This event handler ensures that by default a spec-scoped metrics link
23+
// will be emitted at the end of spec execution. If the test uses a
24+
// private network, it can disable this behavior by setting
25+
// EmitMetricsLink to false.
26+
//
27+
// TODO(marun) Make this conditional on metrics collection being enabled
28+
var _ = ginkgo.BeforeEach(func() {
29+
EmitMetricsLink = true
30+
})
31+
32+
// This event handler attempts to emit a metrics link scoped to the duration
33+
// of the current spec.
34+
//
35+
// TODO(marun) Make this conditional on metrics collection being enabled
36+
var _ = ginkgo.AfterEach(func() {
37+
tc := NewTestContext()
38+
env := GetEnv(tc)
39+
if env == nil || !EmitMetricsLink {
40+
return
41+
}
42+
43+
specReport := ginkgo.CurrentSpecReport()
44+
startTime := specReport.StartTime.UnixMilli()
45+
// Extend the end time by the shutdown delay (a proxy for the metrics
46+
// scrape interval) to maximize the chances of the specified duration
47+
// including all metrics relevant to the current spec.
48+
endTime := time.Now().Add(networkShutdownDelay).UnixMilli()
49+
metricsLink := tmpnet.MetricsLinkForNetwork(
50+
env.GetNetwork().UUID,
51+
strconv.FormatInt(startTime, 10),
52+
strconv.FormatInt(endTime, 10),
53+
)
54+
tc.Outf("Test Metrics: %s\n", metricsLink)
55+
})

tests/fixture/tmpnet/network.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ 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+
startTimeStr := strconv.FormatInt(startTime.UnixMilli(), 10)
377+
metricsURL := MetricsLinkForNetwork(n.UUID, startTimeStr, "")
378+
if _, err := fmt.Fprintf(w, "\nMetrics: %s\n", metricsURL); err != nil {
377379
return err
378380
}
379381

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

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

0 commit comments

Comments
 (0)