Skip to content

Commit

Permalink
sandbox: add container creation metrics
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
toothrot committed Apr 27, 2020
1 parent 26d50f8 commit 66a9e4b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
31 changes: 25 additions & 6 deletions sandbox/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
)

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

containerCount = &view.View{
Name: "go-playground/sandbox/container_count",
Expand All @@ -50,7 +49,25 @@ var (
Measure: mMaxContainers,
Aggregation: view.LastValue(),
}
containerCreateCount = &view.View{
Name: "go-playground/sandbox/container_create_count",
Description: "Number of containers created",
Measure: mContainerCreateLatency,
TagKeys: []tag.Key{kContainerCreateSuccess},
Aggregation: view.Count(),
}
containerCreationLatency = &view.View{
Name: "go-playground/sandbox/container_create_latency",
Description: "Latency distribution of container creation",
Measure: mContainerCreateLatency,
Aggregation: ochttp.DefaultLatencyDistribution,
}
)

// Customizations of ochttp views. Views are updated as follows:
// * The views are prefixed with go-playground-sandbox.
// * ochttp.KeyServerRoute is added as a tag to label metrics per-route.
var (
ServerRequestCountView = &view.View{
Name: "go-playground-sandbox/http/server/request_count",
Description: "Count of HTTP requests started",
Expand Down Expand Up @@ -104,6 +121,8 @@ func newMetricService() (*metricService, error) {
containerCount,
unwantedContainerCount,
maxContainerCount,
containerCreateCount,
containerCreationLatency,
ServerRequestCountView,
ServerRequestBytesView,
ServerResponseBytesView,
Expand Down
12 changes: 12 additions & 0 deletions sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
"golang.org/x/playground/internal"
"golang.org/x/playground/sandbox/sandboxtypes"
Expand Down Expand Up @@ -419,6 +420,17 @@ func getContainer(ctx context.Context) (*Container, error) {
}

func startContainer(ctx context.Context) (c *Container, err error) {
start := time.Now()
defer func() {
status := "success"
if err != nil {
status = "error"
}
// Ignore error. The only error can be invalid tag key or value length, which we know are safe.
_ = stats.RecordWithTags(ctx, []tag.Mutator{tag.Upsert(kContainerCreateSuccess, status)},
mContainerCreateLatency.M(float64(time.Since(start))/float64(time.Millisecond)))
}()

name := "play_run_" + randHex(8)
setContainerWanted(name, true)
cmd := exec.Command("docker", "run",
Expand Down

0 comments on commit 66a9e4b

Please sign in to comment.