-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial support for metrics (#40)
This commit includes two metrics: 1. a metric that reports dial latency 2. a metric that reports open connections Fixes #15.
- Loading branch information
Showing
7 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package trace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
"go.opencensus.io/tag" | ||
) | ||
|
||
var ( | ||
keyInstance, _ = tag.NewKey("cloudsql_instance") | ||
keyDialerID, _ = tag.NewKey("cloudsql_dialer_id") | ||
) | ||
|
||
var ( | ||
mLatencyMS = stats.Int64( | ||
"/cloudsqlconn/latency", | ||
"The latency in milliseconds per Dial", | ||
stats.UnitMilliseconds, | ||
) | ||
latencyView = &view.View{ | ||
Name: "/cloudsqlconn/dial_latency", | ||
Measure: mLatencyMS, | ||
Description: "The distribution of dialer latencies (ms)", | ||
// Latency in buckets, e.g., >=0ms, >=100ms, etc. | ||
Aggregation: view.Distribution(0, 5, 25, 100, 250, 500, 1000, 2000, 5000, 30000), | ||
TagKeys: []tag.Key{keyInstance, keyDialerID}, | ||
} | ||
) | ||
|
||
var ( | ||
mConnections = stats.Int64( | ||
"/cloudsqlconn/connection", | ||
"A connect or disconnect event to Cloud SQL", | ||
stats.UnitDimensionless, | ||
) | ||
connectionsView = &view.View{ | ||
Name: "/cloudsqlconn/open_connections", | ||
Measure: mConnections, | ||
Description: "The sum of Cloud SQL connections", | ||
Aggregation: view.Sum(), | ||
TagKeys: []tag.Key{keyInstance, keyDialerID}, | ||
} | ||
) | ||
|
||
// RecordDialLatency records a latency value for a call to dial. | ||
func RecordDialLatency(ctx context.Context, instance, dialerID string, latency int64) { | ||
// tag.New creates a new context and errors only if the new tag already | ||
// exists in the provided context. Since we're adding tags within this | ||
// package only, we can be confident that there were be no duplicate tags | ||
// and so can ignore the error. | ||
ctx, _ = tag.New(ctx, tag.Upsert(keyInstance, instance), tag.Upsert(keyDialerID, dialerID)) | ||
stats.Record(ctx, mLatencyMS.M(latency)) | ||
} | ||
|
||
// RecordConnectionOpen reports a connection event. | ||
func RecordConnectionOpen(ctx context.Context, instance, dialerID string) { | ||
// Why are we ignoring this error? See above under RecordDialLatency. | ||
ctx, _ = tag.New(ctx, tag.Upsert(keyInstance, instance), tag.Upsert(keyDialerID, dialerID)) | ||
stats.Record(ctx, mConnections.M(1)) | ||
} | ||
|
||
// RecordConnectionClose records a disconnect event. | ||
func RecordConnectionClose(ctx context.Context, instance, dialerID string) { | ||
// Why are we ignoring this error? See above under RecordDialLatency. | ||
ctx, _ = tag.New(ctx, tag.Upsert(keyInstance, instance), tag.Upsert(keyDialerID, dialerID)) | ||
stats.Record(ctx, mConnections.M(-1)) | ||
} | ||
|
||
// InitMetrics registers all views. Without registering views, metrics will not | ||
// be reported. If any names of the registered views conflict, this function | ||
// returns an error to indicate a configuration problem. | ||
func InitMetrics() error { | ||
if err := view.Register(latencyView, connectionsView); err != nil { | ||
return fmt.Errorf("failed to initialize metrics: %v", err) | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package trace_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"cloud.google.com/go/cloudsqlconn/internal/trace" | ||
) | ||
|
||
func TestMetricsInitializes(t *testing.T) { | ||
if err := trace.InitMetrics(); err != nil { | ||
t.Fatalf("want no error, got = %v", err) | ||
} | ||
} |
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