github.com/blend/go-sdk@v1.20220411.3/stats/dbstats/listeners_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package dbstats
     9  
    10  import (
    11  	"context"
    12  	"io"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/db"
    18  	"github.com/blend/go-sdk/logger"
    19  	"github.com/blend/go-sdk/stats"
    20  )
    21  
    22  func TestAddListeners(t *testing.T) {
    23  	assert := assert.New(t)
    24  
    25  	log := logger.None()
    26  	AddListeners(nil, nil)
    27  	assert.False(log.HasListener(db.QueryFlag, stats.ListenerNameStats))
    28  	AddListeners(log, stats.NewMockCollector(32))
    29  	assert.True(log.HasListener(db.QueryFlag, stats.ListenerNameStats))
    30  }
    31  
    32  func TestAddListenersStats(t *testing.T) {
    33  	assert := assert.New(t)
    34  
    35  	log := logger.All(logger.OptOutput(io.Discard))
    36  	defer log.Close()
    37  	collector := stats.NewMockCollector(32)
    38  
    39  	AddListeners(log, collector)
    40  
    41  	log.TriggerContext(context.Background(), db.NewQueryEvent("select 'ok!'", time.Second))
    42  
    43  	qm := <-collector.Metrics
    44  	assert.Equal(MetricNameDBQuery, qm.Name)
    45  	assert.Equal(1, qm.Count)
    46  	assert.NotEmpty(qm.Tags)
    47  
    48  	qm = <-collector.Metrics
    49  	assert.Equal(MetricNameDBQueryElapsedLast, qm.Name)
    50  	assert.Equal(1000, qm.Gauge)
    51  	assert.NotEmpty(qm.Tags)
    52  
    53  	qm = <-collector.Metrics
    54  	assert.Equal(MetricNameDBQueryElapsed, qm.Name)
    55  	assert.Equal(1000, qm.Histogram)
    56  	assert.NotEmpty(qm.Tags)
    57  }