|
| 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 | +}) |
0 commit comments