mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
add opentracing spans for metrics
This commit is contained in:
committed by
Travis Reeder
parent
1cc1a5ad49
commit
dc5e67b6d2
122
api/datastore/internal/datastoreutil/metrics.go
Normal file
122
api/datastore/internal/datastoreutil/metrics.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package datastoreutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
)
|
||||
|
||||
func MetricDS(ds models.Datastore) models.Datastore {
|
||||
return &metricds{ds}
|
||||
}
|
||||
|
||||
type metricds struct {
|
||||
ds models.Datastore
|
||||
}
|
||||
|
||||
func (m *metricds) GetApp(ctx context.Context, appName string) (*models.App, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_app")
|
||||
defer span.Finish()
|
||||
return m.ds.GetApp(ctx, appName)
|
||||
}
|
||||
|
||||
func (m *metricds) GetApps(ctx context.Context, filter *models.AppFilter) ([]*models.App, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_apps")
|
||||
defer span.Finish()
|
||||
return m.ds.GetApps(ctx, filter)
|
||||
}
|
||||
|
||||
func (m *metricds) InsertApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_app")
|
||||
defer span.Finish()
|
||||
return m.ds.InsertApp(ctx, app)
|
||||
}
|
||||
|
||||
func (m *metricds) UpdateApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_update_app")
|
||||
defer span.Finish()
|
||||
return m.ds.UpdateApp(ctx, app)
|
||||
}
|
||||
|
||||
func (m *metricds) RemoveApp(ctx context.Context, appName string) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_remove_app")
|
||||
defer span.Finish()
|
||||
return m.ds.RemoveApp(ctx, appName)
|
||||
}
|
||||
|
||||
func (m *metricds) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_route")
|
||||
defer span.Finish()
|
||||
return m.ds.GetRoute(ctx, appName, routePath)
|
||||
}
|
||||
|
||||
func (m *metricds) GetRoutes(ctx context.Context, filter *models.RouteFilter) (routes []*models.Route, err error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_routes")
|
||||
defer span.Finish()
|
||||
return m.ds.GetRoutes(ctx, filter)
|
||||
}
|
||||
|
||||
func (m *metricds) GetRoutesByApp(ctx context.Context, appName string, filter *models.RouteFilter) (routes []*models.Route, err error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_routes_by_app")
|
||||
defer span.Finish()
|
||||
return m.ds.GetRoutesByApp(ctx, appName, filter)
|
||||
}
|
||||
|
||||
func (m *metricds) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_route")
|
||||
defer span.Finish()
|
||||
return m.ds.InsertRoute(ctx, route)
|
||||
}
|
||||
|
||||
func (m *metricds) UpdateRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_update_route")
|
||||
defer span.Finish()
|
||||
return m.ds.UpdateRoute(ctx, route)
|
||||
}
|
||||
|
||||
func (m *metricds) RemoveRoute(ctx context.Context, appName, routePath string) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_remove_route")
|
||||
defer span.Finish()
|
||||
return m.ds.RemoveRoute(ctx, appName, routePath)
|
||||
}
|
||||
|
||||
func (m *metricds) InsertTask(ctx context.Context, task *models.Task) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_task")
|
||||
defer span.Finish()
|
||||
return m.ds.InsertTask(ctx, task)
|
||||
}
|
||||
|
||||
func (m *metricds) GetTask(ctx context.Context, callID string) (*models.FnCall, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_task")
|
||||
defer span.Finish()
|
||||
return m.ds.GetTask(ctx, callID)
|
||||
}
|
||||
|
||||
func (m *metricds) GetTasks(ctx context.Context, filter *models.CallFilter) (models.FnCalls, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_tasks")
|
||||
defer span.Finish()
|
||||
return m.ds.GetTasks(ctx, filter)
|
||||
}
|
||||
|
||||
func (m *metricds) InsertLog(ctx context.Context, callID string, callLog string) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_log")
|
||||
defer span.Finish()
|
||||
return m.ds.InsertLog(ctx, callID, callLog)
|
||||
}
|
||||
|
||||
func (m *metricds) GetLog(ctx context.Context, callID string) (*models.FnCallLog, error) {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_log")
|
||||
defer span.Finish()
|
||||
return m.ds.GetLog(ctx, callID)
|
||||
}
|
||||
|
||||
func (m *metricds) DeleteLog(ctx context.Context, callID string) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_delete_log")
|
||||
defer span.Finish()
|
||||
return m.ds.DeleteLog(ctx, callID)
|
||||
}
|
||||
|
||||
// instant & no context ;)
|
||||
func (m *metricds) GetDatabase() *sqlx.DB { return m.ds.GetDatabase() }
|
||||
@@ -1,169 +0,0 @@
|
||||
package datastoreutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
)
|
||||
|
||||
// TODO scrap for sqlx
|
||||
|
||||
type RowScanner interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
func ScanLog(scanner RowScanner, log *models.FnCallLog) error {
|
||||
return scanner.Scan(
|
||||
&log.CallID,
|
||||
&log.Log,
|
||||
)
|
||||
}
|
||||
|
||||
func ScanRoute(scanner RowScanner, route *models.Route) error {
|
||||
var headerStr string
|
||||
var configStr string
|
||||
|
||||
err := scanner.Scan(
|
||||
&route.AppName,
|
||||
&route.Path,
|
||||
&route.Image,
|
||||
&route.Format,
|
||||
&route.Memory,
|
||||
&route.Type,
|
||||
&route.Timeout,
|
||||
&route.IdleTimeout,
|
||||
&headerStr,
|
||||
&configStr,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(headerStr) > 0 {
|
||||
err = json.Unmarshal([]byte(headerStr), &route.Headers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(configStr) > 0 {
|
||||
err = json.Unmarshal([]byte(configStr), &route.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ScanApp(scanner RowScanner, app *models.App) error {
|
||||
var configStr string
|
||||
|
||||
err := scanner.Scan(
|
||||
&app.Name,
|
||||
&configStr,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(configStr) > 0 {
|
||||
err = json.Unmarshal([]byte(configStr), &app.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func BuildFilterRouteQuery(filter *models.RouteFilter, whereStm, andStm string) (string, []interface{}) {
|
||||
if filter == nil {
|
||||
return "", nil
|
||||
}
|
||||
var b bytes.Buffer
|
||||
var args []interface{}
|
||||
|
||||
where := func(colOp, val string) {
|
||||
if val != "" {
|
||||
args = append(args, val)
|
||||
if len(args) == 1 {
|
||||
fmt.Fprintf(&b, whereStm, colOp)
|
||||
} else {
|
||||
//TODO: maybe better way to detect/driver SQL dialect-specific things
|
||||
if strings.Contains(whereStm, "$") {
|
||||
// PgSQL specific
|
||||
fmt.Fprintf(&b, andStm, colOp, len(args))
|
||||
} else {
|
||||
// MySQL specific
|
||||
fmt.Fprintf(&b, andStm, colOp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
where("path =", filter.Path)
|
||||
where("app_name =", filter.AppName)
|
||||
where("image =", filter.Image)
|
||||
|
||||
return b.String(), args
|
||||
}
|
||||
|
||||
func BuildFilterAppQuery(filter *models.AppFilter, whereStm string) (string, []interface{}) {
|
||||
if filter == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if filter.Name != "" {
|
||||
return whereStm, []interface{}{filter.Name}
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func BuildFilterCallQuery(filter *models.CallFilter, whereStm, andStm string) (string, []interface{}) {
|
||||
if filter == nil {
|
||||
return "", nil
|
||||
}
|
||||
var b bytes.Buffer
|
||||
var args []interface{}
|
||||
|
||||
where := func(colOp, val string) {
|
||||
if val != "" {
|
||||
args = append(args, val)
|
||||
if len(args) == 1 {
|
||||
fmt.Fprintf(&b, whereStm, colOp)
|
||||
} else {
|
||||
fmt.Fprintf(&b, andStm, colOp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
where("path =", filter.Path)
|
||||
where("app_name =", filter.AppName)
|
||||
|
||||
return b.String(), args
|
||||
}
|
||||
|
||||
func ScanCall(scanner RowScanner, call *models.FnCall) error {
|
||||
err := scanner.Scan(
|
||||
&call.ID,
|
||||
&call.CreatedAt,
|
||||
&call.StartedAt,
|
||||
&call.CompletedAt,
|
||||
&call.Status,
|
||||
&call.AppName,
|
||||
&call.Path,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return models.ErrCallNotFound
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user