golang.org/x/playground@v0.0.0-20230418134305-14ebe15bcd59/sandbox/metrics.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "go.opencensus.io/plugin/ochttp" 9 "go.opencensus.io/stats" 10 "go.opencensus.io/stats/view" 11 "go.opencensus.io/tag" 12 ) 13 14 var ( 15 kContainerCreateSuccess = tag.MustNewKey("go-playground/sandbox/container_create_success") 16 mContainers = stats.Int64("go-playground/sandbox/container_count", "number of sandbox containers", stats.UnitDimensionless) 17 mUnwantedContainers = stats.Int64("go-playground/sandbox/unwanted_container_count", "number of sandbox containers that are unexpectedly running", stats.UnitDimensionless) 18 mMaxContainers = stats.Int64("go-playground/sandbox/max_container_count", "target number of sandbox containers", stats.UnitDimensionless) 19 mContainerCreateLatency = stats.Float64("go-playground/sandbox/container_create_latency", "", stats.UnitMilliseconds) 20 21 containerCount = &view.View{ 22 Name: "go-playground/sandbox/container_count", 23 Description: "Number of running sandbox containers", 24 TagKeys: nil, 25 Measure: mContainers, 26 Aggregation: view.LastValue(), 27 } 28 unwantedContainerCount = &view.View{ 29 Name: "go-playground/sandbox/unwanted_container_count", 30 Description: "Number of running sandbox containers that are not being tracked by the sandbox", 31 TagKeys: nil, 32 Measure: mUnwantedContainers, 33 Aggregation: view.LastValue(), 34 } 35 maxContainerCount = &view.View{ 36 Name: "go-playground/sandbox/max_container_count", 37 Description: "Maximum number of containers to create", 38 TagKeys: nil, 39 Measure: mMaxContainers, 40 Aggregation: view.LastValue(), 41 } 42 containerCreateCount = &view.View{ 43 Name: "go-playground/sandbox/container_create_count", 44 Description: "Number of containers created", 45 Measure: mContainerCreateLatency, 46 TagKeys: []tag.Key{kContainerCreateSuccess}, 47 Aggregation: view.Count(), 48 } 49 containerCreationLatency = &view.View{ 50 Name: "go-playground/sandbox/container_create_latency", 51 Description: "Latency distribution of container creation", 52 Measure: mContainerCreateLatency, 53 Aggregation: ochttp.DefaultLatencyDistribution, 54 } 55 ) 56 57 // Customizations of ochttp views. Views are updated as follows: 58 // - The views are prefixed with go-playground-sandbox. 59 // - ochttp.KeyServerRoute is added as a tag to label metrics per-route. 60 var ( 61 ServerRequestCountView = &view.View{ 62 Name: "go-playground-sandbox/http/server/request_count", 63 Description: "Count of HTTP requests started", 64 Measure: ochttp.ServerRequestCount, 65 TagKeys: []tag.Key{ochttp.KeyServerRoute}, 66 Aggregation: view.Count(), 67 } 68 ServerRequestBytesView = &view.View{ 69 Name: "go-playground-sandbox/http/server/request_bytes", 70 Description: "Size distribution of HTTP request body", 71 Measure: ochttp.ServerRequestBytes, 72 TagKeys: []tag.Key{ochttp.KeyServerRoute}, 73 Aggregation: ochttp.DefaultSizeDistribution, 74 } 75 ServerResponseBytesView = &view.View{ 76 Name: "go-playground-sandbox/http/server/response_bytes", 77 Description: "Size distribution of HTTP response body", 78 Measure: ochttp.ServerResponseBytes, 79 TagKeys: []tag.Key{ochttp.KeyServerRoute}, 80 Aggregation: ochttp.DefaultSizeDistribution, 81 } 82 ServerLatencyView = &view.View{ 83 Name: "go-playground-sandbox/http/server/latency", 84 Description: "Latency distribution of HTTP requests", 85 Measure: ochttp.ServerLatency, 86 TagKeys: []tag.Key{ochttp.KeyServerRoute}, 87 Aggregation: ochttp.DefaultLatencyDistribution, 88 } 89 ServerRequestCountByMethod = &view.View{ 90 Name: "go-playground-sandbox/http/server/request_count_by_method", 91 Description: "Server request count by HTTP method", 92 TagKeys: []tag.Key{ochttp.Method, ochttp.KeyServerRoute}, 93 Measure: ochttp.ServerRequestCount, 94 Aggregation: view.Count(), 95 } 96 ServerResponseCountByStatusCode = &view.View{ 97 Name: "go-playground-sandbox/http/server/response_count_by_status_code", 98 Description: "Server response count by status code", 99 TagKeys: []tag.Key{ochttp.StatusCode, ochttp.KeyServerRoute}, 100 Measure: ochttp.ServerLatency, 101 Aggregation: view.Count(), 102 } 103 ) 104 105 // views should contain all measurements. All *view.View added to this 106 // slice will be registered and exported to the metric service. 107 var views = []*view.View{ 108 containerCount, 109 unwantedContainerCount, 110 maxContainerCount, 111 containerCreateCount, 112 containerCreationLatency, 113 ServerRequestCountView, 114 ServerRequestBytesView, 115 ServerResponseBytesView, 116 ServerLatencyView, 117 ServerRequestCountByMethod, 118 ServerResponseCountByStatusCode, 119 }