Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial support for metrics #40

Merged
merged 10 commits into from
Aug 25, 2021
Merged

feat: add initial support for metrics #40

merged 10 commits into from
Aug 25, 2021

Conversation

enocom
Copy link
Member

@enocom enocom commented Aug 12, 2021

This commit includes two metrics:

  1. a metric that reports dial latency
  2. a metric that reports open connections

Fixes #15.

@enocom enocom requested a review from kurtisvg August 12, 2021 23:23
@enocom
Copy link
Member Author

enocom commented Aug 13, 2021

Here's what I've been using to test these metrics:

package main

import (
	"context"
	"fmt"
	"log"
	"net"
	"os"
	"sync"
	"time"

	"cloud.google.com/go/cloudsqlconn"
	"contrib.go.opencensus.io/exporter/stackdriver"
	"github.com/jackc/pgx/v4/pgxpool"
	"go.opencensus.io/stats/view"
)

type customMetricsExporter struct{}

func (ce *customMetricsExporter) ExportView(vd *view.Data) {
	log.Printf("vd.View: %+v\n%#v\n", vd.View, vd.Rows)
	for i, row := range vd.Rows {
		log.Printf("\tRow: %#d: %#v", i, row)

		log.Printf("%#v", row.Data)
	}
}

func main() {
	ctx := context.Background()
	pgUser := os.Getenv("POSTGRES_USER")
	pgPass := os.Getenv("POSTGRES_PASS")
	pgDB := os.Getenv("POSTGRES_DB")
	connName := os.Getenv("POSTGRES_CONNECTION_NAME")

	sd, err := stackdriver.NewExporter(stackdriver.Options{
		ProjectID:         "MY_COOL_PROJECT_ID",
		ReportingInterval: 60 * time.Second,
	})
	if err != nil {
		log.Fatalf("Failed to create the Stackdriver exporter: %v", err)
	}
	// It is imperative to invoke flush before your main function exits
	defer sd.Flush()

	// Start the metrics exporter
	sd.StartMetricsExporter()
	defer sd.StopMetricsExporter()

        // uncomment the following line and comment-out the above exporter to use a console exporter
	// view.RegisterExporter(new(customMetricsExporter))

	// Configure the driver to connect to the database
	dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", pgUser, pgPass, pgDB)
	config, err := pgxpool.ParseConfig(dsn)
	if err != nil {
		log.Fatalf("failed to parse pgx config: %v", err)
	}

	d, err := cloudsqlconn.NewDialer(context.Background())
	if err != nil {
		panic(err)
	}
	// Tell the driver to use the Cloud SQL Go Connector to create connections
	config.ConnConfig.DialFunc = func(ctx context.Context, network string, instance string) (net.Conn, error) {
		return d.Dial(ctx, connName)
	}
	defer d.Close()

	// Interact with the driver directly as you normally would
	conn, connErr := pgxpool.ConnectConfig(ctx, config)
	if connErr != nil {
		log.Fatalf("failed to connect: %s", connErr)
		return
	}

	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go func() {
			var t time.Time
			if err := conn.QueryRow(ctx, "select now()").Scan(&t); err != nil {
				log.Println("error", err.Error())
			}
			log.Println(t)
			wg.Done()
		}()
	}
	wg.Wait()
	conn.Close()
	select {}
}

enocom added 4 commits August 13, 2021 08:44
This commit includes two metrics:

1. a metric that reports dial latency
2. a metric that reports open connections

Fixes #15.
@enocom
Copy link
Member Author

enocom commented Aug 19, 2021

Here's what these metrics look like in action:

metrics

And here's a heatmap version of the dial latency:

Screenshot from 2021-08-19 15-26-01

@enocom enocom requested a review from kurtisvg August 20, 2021 21:22
@enocom
Copy link
Member Author

enocom commented Aug 20, 2021

Aside from the two open issues above, this is ready for another review. Notable in the recent changes: I've dropped the atomic counter management in favor of an accurate and simpler sum aggregation.

@enocom
Copy link
Member Author

enocom commented Aug 25, 2021

dialer-id

Here's what the unique ID (a UUID) per dialer looks like.

Also, fix aggregation buckets for latency.
@enocom
Copy link
Member Author

enocom commented Aug 25, 2021

trace

And here's trace with the dialer ID too.

@enocom enocom requested a review from kurtisvg August 25, 2021 21:45
@enocom enocom merged commit ee396ff into main Aug 25, 2021
@enocom enocom deleted the metrics branch August 25, 2021 22:17
This was referenced Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for metrics / tracing
2 participants