mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fn: API stats and tags reoorganization (#1171)
Make sure we can apply extra tags if RegisterAPIViews() is provided with such tags. Deduplicate path/method/status and always apply these default tags to appropriate views.
This commit is contained in:
@@ -8,11 +8,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CreateView(measure stats.Measure, agg *view.Aggregation, tagKeys []string) *view.View {
|
func CreateView(measure stats.Measure, agg *view.Aggregation, tagKeys []string) *view.View {
|
||||||
|
keys := makeKeys(tagKeys)
|
||||||
|
return CreateViewWithTags(measure, agg, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateViewWithTags(measure stats.Measure, agg *view.Aggregation, tags []tag.Key) *view.View {
|
||||||
return &view.View{
|
return &view.View{
|
||||||
Name: measure.Name(),
|
Name: measure.Name(),
|
||||||
Description: measure.Description(),
|
Description: measure.Description(),
|
||||||
Measure: measure,
|
Measure: measure,
|
||||||
TagKeys: makeKeys(tagKeys),
|
TagKeys: tags,
|
||||||
Aggregation: agg,
|
Aggregation: agg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,14 +26,18 @@ func MakeMeasure(name string, desc string, unit string) *stats.Int64Measure {
|
|||||||
return stats.Int64(name, desc, unit)
|
return stats.Int64(name, desc, unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeKey(name string) tag.Key {
|
||||||
|
key, err := tag.NewKey(name)
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Fatalf("Cannot create tag %s", name)
|
||||||
|
}
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
func makeKeys(names []string) []tag.Key {
|
func makeKeys(names []string) []tag.Key {
|
||||||
tagKeys := make([]tag.Key, len(names))
|
tagKeys := make([]tag.Key, len(names))
|
||||||
for i, name := range names {
|
for i, name := range names {
|
||||||
key, err := tag.NewKey(name)
|
tagKeys[i] = MakeKey(name)
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
tagKeys[i] = key
|
|
||||||
}
|
}
|
||||||
return tagKeys
|
return tagKeys
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,23 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"go.opencensus.io/stats"
|
"go.opencensus.io/stats"
|
||||||
|
"go.opencensus.io/stats/view"
|
||||||
"go.opencensus.io/tag"
|
"go.opencensus.io/tag"
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
pathKey = common.MakeKey("path")
|
||||||
|
methodKey = common.MakeKey("method")
|
||||||
|
statusKey = common.MakeKey("status")
|
||||||
|
|
||||||
|
apiRequestCountMeasure = common.MakeMeasure("api/request_count", "Count of API requests started", stats.UnitDimensionless)
|
||||||
|
apiResponseCountMeasure = common.MakeMeasure("api/response_count", "API response count", stats.UnitDimensionless)
|
||||||
|
apiLatencyMeasure = common.MakeMeasure("api/latency", "Latency distribution of API requests", stats.UnitMilliseconds)
|
||||||
|
)
|
||||||
|
|
||||||
func optionalCorsWrap(r *gin.Engine) {
|
func optionalCorsWrap(r *gin.Engine) {
|
||||||
// By default no CORS are allowed unless one
|
// By default no CORS are allowed unless one
|
||||||
// or more Origins are defined by the API_CORS
|
// or more Origins are defined by the API_CORS
|
||||||
@@ -78,19 +89,31 @@ func traceWrap(c *gin.Context) {
|
|||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RegisterAPIViews(tagKeys []string, dist []float64) {
|
||||||
|
|
||||||
|
// default tags for request and response
|
||||||
|
reqTags := []tag.Key{pathKey, methodKey}
|
||||||
|
respTags := []tag.Key{pathKey, methodKey, statusKey}
|
||||||
|
|
||||||
|
// add extra tags if not already in default tags for req/resp
|
||||||
|
for _, key := range tagKeys {
|
||||||
|
if key != "path" && key != "method" && key != "status" {
|
||||||
|
reqTags = append(reqTags, common.MakeKey(key))
|
||||||
|
respTags = append(respTags, common.MakeKey(key))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := view.Register(
|
||||||
|
common.CreateViewWithTags(apiRequestCountMeasure, view.Count(), reqTags),
|
||||||
|
common.CreateViewWithTags(apiResponseCountMeasure, view.Count(), respTags),
|
||||||
|
common.CreateViewWithTags(apiLatencyMeasure, view.Distribution(dist...), respTags),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Fatal("cannot register view")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func apiMetricsWrap(s *Server) {
|
func apiMetricsWrap(s *Server) {
|
||||||
pathKey, err := tag.NewKey("path")
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
methodKey, err := tag.NewKey("method")
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
statusKey, err := tag.NewKey("status")
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
measure := func(engine *gin.Engine) func(*gin.Context) {
|
measure := func(engine *gin.Engine) func(*gin.Context) {
|
||||||
var routes gin.RoutesInfo
|
var routes gin.RoutesInfo
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/fnproject/fn/api/common"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"go.opencensus.io/stats"
|
|
||||||
"go.opencensus.io/stats/view"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
apiRequestCountMeasure = common.MakeMeasure("api/request_count", "Count of API requests started", stats.UnitDimensionless)
|
|
||||||
apiResponseCountMeasure = common.MakeMeasure("api/response_count", "API response count", stats.UnitDimensionless)
|
|
||||||
apiLatencyMeasure = common.MakeMeasure("api/latency", "Latency distribution of API requests", stats.UnitMilliseconds)
|
|
||||||
)
|
|
||||||
|
|
||||||
func RegisterAPIViews(tagKeys []string, dist []float64) {
|
|
||||||
err := view.Register(
|
|
||||||
common.CreateView(apiRequestCountMeasure, view.Count(), tagKeys),
|
|
||||||
common.CreateView(apiResponseCountMeasure, view.Count(), tagKeys),
|
|
||||||
common.CreateView(apiLatencyMeasure, view.Distribution(dist...), tagKeys),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
logrus.WithError(err).Fatal("cannot register view")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -26,7 +26,6 @@ func main() {
|
|||||||
|
|
||||||
func registerViews() {
|
func registerViews() {
|
||||||
keys := []string{"fn_appname", "fn_path"}
|
keys := []string{"fn_appname", "fn_path"}
|
||||||
apiKeys := []string{"path", "method", "status"}
|
|
||||||
|
|
||||||
latencyDist := []float64{1, 10, 50, 100, 250, 500, 1000, 10000, 60000, 120000}
|
latencyDist := []float64{1, 10, 50, 100, 250, 500, 1000, 10000, 60000, 120000}
|
||||||
|
|
||||||
@@ -51,5 +50,5 @@ func registerViews() {
|
|||||||
// Register s3 log views
|
// Register s3 log views
|
||||||
s3.RegisterViews(keys, latencyDist)
|
s3.RegisterViews(keys, latencyDist)
|
||||||
|
|
||||||
server.RegisterAPIViews(apiKeys, latencyDist)
|
server.RegisterAPIViews([]string{}, latencyDist)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user