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