Skip to content

Commit 66a9e4b

Browse files
committed
sandbox: add container creation metrics
This change measures the latency and success of container creation. These metrics will help capacity planning and investigating production issues. Updates golang/go#25224 Updates golang/go#38530 Change-Id: Id7f373acb8741d4465c6e632badb188b6e855787 Reviewed-on: https://go-review.googlesource.com/c/playground/+/229980 Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 26d50f8 commit 66a9e4b

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

sandbox/metrics.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ import (
2121
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
2222
)
2323

24-
// Customizations of ochttp views. Views are updated as follows:
25-
// * The views are prefixed with go-playground-sandbox.
26-
// * ochttp.KeyServerRoute is added as a tag to label metrics per-route.
2724
var (
28-
mContainers = stats.Int64("go-playground/sandbox/container_count", "number of sandbox containers", stats.UnitDimensionless)
29-
mUnwantedContainers = stats.Int64("go-playground/sandbox/unwanted_container_count", "number of sandbox containers that are unexpectedly running", stats.UnitDimensionless)
30-
mMaxContainers = stats.Int64("go-playground/sandbox/max_container_count", "target number of sandbox containers", stats.UnitDimensionless)
25+
kContainerCreateSuccess = tag.MustNewKey("go-playground/sandbox/container_create_success")
26+
mContainers = stats.Int64("go-playground/sandbox/container_count", "number of sandbox containers", stats.UnitDimensionless)
27+
mUnwantedContainers = stats.Int64("go-playground/sandbox/unwanted_container_count", "number of sandbox containers that are unexpectedly running", stats.UnitDimensionless)
28+
mMaxContainers = stats.Int64("go-playground/sandbox/max_container_count", "target number of sandbox containers", stats.UnitDimensionless)
29+
mContainerCreateLatency = stats.Float64("go-playground/sandbox/container_create_latency", "", stats.UnitMilliseconds)
3130

3231
containerCount = &view.View{
3332
Name: "go-playground/sandbox/container_count",
@@ -50,7 +49,25 @@ var (
5049
Measure: mMaxContainers,
5150
Aggregation: view.LastValue(),
5251
}
52+
containerCreateCount = &view.View{
53+
Name: "go-playground/sandbox/container_create_count",
54+
Description: "Number of containers created",
55+
Measure: mContainerCreateLatency,
56+
TagKeys: []tag.Key{kContainerCreateSuccess},
57+
Aggregation: view.Count(),
58+
}
59+
containerCreationLatency = &view.View{
60+
Name: "go-playground/sandbox/container_create_latency",
61+
Description: "Latency distribution of container creation",
62+
Measure: mContainerCreateLatency,
63+
Aggregation: ochttp.DefaultLatencyDistribution,
64+
}
65+
)
5366

67+
// Customizations of ochttp views. Views are updated as follows:
68+
// * The views are prefixed with go-playground-sandbox.
69+
// * ochttp.KeyServerRoute is added as a tag to label metrics per-route.
70+
var (
5471
ServerRequestCountView = &view.View{
5572
Name: "go-playground-sandbox/http/server/request_count",
5673
Description: "Count of HTTP requests started",
@@ -104,6 +121,8 @@ func newMetricService() (*metricService, error) {
104121
containerCount,
105122
unwantedContainerCount,
106123
maxContainerCount,
124+
containerCreateCount,
125+
containerCreationLatency,
107126
ServerRequestCountView,
108127
ServerRequestBytesView,
109128
ServerResponseBytesView,

sandbox/sandbox.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"go.opencensus.io/plugin/ochttp"
3535
"go.opencensus.io/stats"
36+
"go.opencensus.io/tag"
3637
"go.opencensus.io/trace"
3738
"golang.org/x/playground/internal"
3839
"golang.org/x/playground/sandbox/sandboxtypes"
@@ -419,6 +420,17 @@ func getContainer(ctx context.Context) (*Container, error) {
419420
}
420421

421422
func startContainer(ctx context.Context) (c *Container, err error) {
423+
start := time.Now()
424+
defer func() {
425+
status := "success"
426+
if err != nil {
427+
status = "error"
428+
}
429+
// Ignore error. The only error can be invalid tag key or value length, which we know are safe.
430+
_ = stats.RecordWithTags(ctx, []tag.Mutator{tag.Upsert(kContainerCreateSuccess, status)},
431+
mContainerCreateLatency.M(float64(time.Since(start))/float64(time.Millisecond)))
432+
}()
433+
422434
name := "play_run_" + randHex(8)
423435
setContainerWanted(name, true)
424436
cmd := exec.Command("docker", "run",

0 commit comments

Comments
 (0)