github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/metric/periodic_test.go (about)

     1  package metric_test
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/lager"
     8  	"github.com/pf-qiu/concourse/v6/atc/db"
     9  	"github.com/pf-qiu/concourse/v6/atc/db/dbfakes"
    10  	"github.com/pf-qiu/concourse/v6/atc/metric"
    11  	"github.com/pf-qiu/concourse/v6/atc/metric/metricfakes"
    12  	"github.com/tedsuo/ifrit"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gstruct"
    17  )
    18  
    19  var _ = Describe("Periodic emission of metrics", func() {
    20  	var (
    21  		emitter *metricfakes.FakeEmitter
    22  		monitor *metric.Monitor
    23  
    24  		process ifrit.Process
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		emitter = &metricfakes.FakeEmitter{}
    29  		monitor = metric.NewMonitor()
    30  
    31  		emitterFactory := &metricfakes.FakeEmitterFactory{}
    32  		emitterFactory.IsConfiguredReturns(true)
    33  		emitterFactory.NewEmitterReturns(emitter, nil)
    34  
    35  		monitor.RegisterEmitter(emitterFactory)
    36  		monitor.Initialize(testLogger, "test", map[string]string{}, 1000)
    37  
    38  	})
    39  
    40  	JustBeforeEach(func() {
    41  		runner := metric.PeriodicallyEmit(
    42  			lager.NewLogger("dont care"),
    43  			monitor,
    44  			250*time.Millisecond,
    45  		)
    46  
    47  		process = ifrit.Invoke(runner)
    48  	})
    49  
    50  	AfterEach(func() {
    51  		process.Signal(os.Interrupt)
    52  		<-process.Wait()
    53  	})
    54  
    55  	events := func() []metric.Event {
    56  		var events []metric.Event
    57  		for i := 0; i < emitter.EmitCallCount(); i++ {
    58  			_, event := emitter.EmitArgsForCall(i)
    59  			events = append(events, event)
    60  		}
    61  		return events
    62  	}
    63  
    64  	Context("database-related metrics", func() {
    65  		BeforeEach(func() {
    66  			a := &dbfakes.FakeConn{}
    67  			a.NameReturns("A")
    68  			b := &dbfakes.FakeConn{}
    69  			b.NameReturns("B")
    70  			monitor.Databases = []db.Conn{a, b}
    71  		})
    72  
    73  		It("emits database queries", func() {
    74  			Eventually(events).Should(
    75  				ContainElement(
    76  					MatchFields(IgnoreExtras, Fields{
    77  						"Name": Equal("database queries"),
    78  					}),
    79  				),
    80  			)
    81  
    82  			By("emits database connections for each pool")
    83  			Eventually(events).Should(
    84  				ContainElement(
    85  					MatchFields(IgnoreExtras, Fields{
    86  						"Name":       Equal("database connections"),
    87  						"Attributes": Equal(map[string]string{"ConnectionName": "A"}),
    88  					}),
    89  				),
    90  			)
    91  			Eventually(events).Should(
    92  				ContainElement(
    93  					MatchFields(IgnoreExtras, Fields{
    94  						"Name":       Equal("database connections"),
    95  						"Attributes": Equal(map[string]string{"ConnectionName": "B"}),
    96  					}),
    97  				),
    98  			)
    99  		})
   100  	})
   101  
   102  	Context("concurrent requests", func() {
   103  		const action = "ListAllSomething"
   104  
   105  		BeforeEach(func() {
   106  			gauge := &metric.Gauge{}
   107  			gauge.Set(123)
   108  
   109  			counter := &metric.Counter{}
   110  			counter.IncDelta(10)
   111  
   112  			monitor.ConcurrentRequests[action] = gauge
   113  			monitor.ConcurrentRequestsLimitHit[action] = counter
   114  		})
   115  
   116  		It("emits", func() {
   117  			Eventually(events).Should(
   118  				ContainElement(
   119  					MatchFields(IgnoreExtras, Fields{
   120  						"Name":  Equal("concurrent requests"),
   121  						"Value": Equal(float64(123)),
   122  						"Attributes": Equal(map[string]string{
   123  							"action": action,
   124  						}),
   125  					}),
   126  				),
   127  			)
   128  
   129  			Eventually(events).Should(
   130  				ContainElement(
   131  					MatchFields(IgnoreExtras, Fields{
   132  						"Name":  Equal("concurrent requests limit hit"),
   133  						"Value": Equal(float64(10)),
   134  						"Attributes": Equal(map[string]string{
   135  							"action": action,
   136  						}),
   137  					}),
   138  				),
   139  			)
   140  		})
   141  	})
   142  
   143  	Context("limit-active-tasks metrics", func() {
   144  		labels := metric.TasksWaitingLabels{
   145  			TeamId:     "42",
   146  			WorkerTags: "tester",
   147  			Platform:   "darwin",
   148  		}
   149  
   150  		BeforeEach(func() {
   151  			gauge := &metric.Gauge{}
   152  			gauge.Set(123)
   153  			monitor.TasksWaiting[labels] = gauge
   154  		})
   155  		It("emits", func() {
   156  			Eventually(events).Should(
   157  				ContainElement(
   158  					MatchFields(IgnoreExtras, Fields{
   159  						"Name":  Equal("tasks waiting"),
   160  						"Value": Equal(float64(123)),
   161  						"Attributes": Equal(map[string]string{
   162  							"teamId":     labels.TeamId,
   163  							"workerTags": labels.WorkerTags,
   164  							"platform":   labels.Platform,
   165  						}),
   166  					}),
   167  				),
   168  			)
   169  		})
   170  	})
   171  })