-
Notifications
You must be signed in to change notification settings - Fork 30
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}
} |
This commit includes two metrics: 1. a metric that reports dial latency 2. a metric that reports open connections Fixes #15.
kurtisvg
suggested changes
Aug 14, 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. |
kurtisvg
reviewed
Aug 25, 2021
Also, fix aggregation buckets for latency.
kurtisvg
approved these changes
Aug 25, 2021
This was referenced Apr 5, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit includes two metrics:
Fixes #15.