mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
metric logger
This commit is contained in:
@@ -1,27 +1,24 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
titancommon "github.com/iron-io/titan/common"
|
titancommon "github.com/iron-io/titan/common"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LogMetric(ctx context.Context, name string, metricType string, value interface{}) {
|
type Logger interface {
|
||||||
|
Log(context.Context, map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Metric map[string]interface{}
|
||||||
|
|
||||||
|
func NewMetricLogger() *MetricLogger {
|
||||||
|
return &MetricLogger{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type MetricLogger struct{}
|
||||||
|
|
||||||
|
func (l *MetricLogger) Log(ctx context.Context, metric map[string]interface{}) {
|
||||||
log := titancommon.Logger(ctx)
|
log := titancommon.Logger(ctx)
|
||||||
log.WithFields(logrus.Fields{
|
log.WithFields(logrus.Fields(metric)).Info()
|
||||||
"metric": name, "type": metricType, "value": value}).Info()
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogMetricGauge(ctx context.Context, name string, value int) {
|
|
||||||
LogMetric(ctx, name, "gauge", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogMetricCount(ctx context.Context, name string, value int) {
|
|
||||||
LogMetric(ctx, name, "count", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogMetricTime(ctx context.Context, name string, time time.Duration) {
|
|
||||||
LogMetric(ctx, name, "time", time)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ type Config struct {
|
|||||||
type Runner struct {
|
type Runner struct {
|
||||||
driver drivers.Driver
|
driver drivers.Driver
|
||||||
taskQueue chan *containerTask
|
taskQueue chan *containerTask
|
||||||
|
ml Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -45,7 +46,7 @@ var (
|
|||||||
WaitMemoryTimeout = 10 * time.Second
|
WaitMemoryTimeout = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func New() (*Runner, error) {
|
func New(metricLogger Logger) (*Runner, error) {
|
||||||
// TODO: Is this really required for Titan's driver?
|
// TODO: Is this really required for Titan's driver?
|
||||||
// Can we remove it?
|
// Can we remove it?
|
||||||
env := common.NewEnvironment(func(e *common.Environment) {})
|
env := common.NewEnvironment(func(e *common.Environment) {})
|
||||||
@@ -59,6 +60,7 @@ func New() (*Runner, error) {
|
|||||||
r := &Runner{
|
r := &Runner{
|
||||||
driver: driver,
|
driver: driver,
|
||||||
taskQueue: make(chan *containerTask, 100),
|
taskQueue: make(chan *containerTask, 100),
|
||||||
|
ml: metricLogger,
|
||||||
}
|
}
|
||||||
|
|
||||||
go r.queueHandler()
|
go r.queueHandler()
|
||||||
@@ -93,11 +95,11 @@ func (r *Runner) queueHandler() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
metricBaseName := fmt.Sprintf("run.%s.", task.cfg.AppName)
|
metricBaseName := fmt.Sprintf("run.%s.", task.cfg.AppName)
|
||||||
LogMetricTime(task.ctx, (metricBaseName + "waittime"), waitTime)
|
r.ml.Log(task.ctx, Metric{"name": (metricBaseName + "waittime"), "type": "time", "value": waitTime})
|
||||||
|
|
||||||
if timedOut {
|
if timedOut {
|
||||||
// Send to a signal to this task saying it cannot run
|
// Send to a signal to this task saying it cannot run
|
||||||
LogMetricCount(task.ctx, (metricBaseName + "timeout"), 1)
|
r.ml.Log(task.ctx, Metric{"name": (metricBaseName + "timeout"), "type": "count", "value": 1})
|
||||||
task.canRun <- false
|
task.canRun <- false
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -118,7 +120,7 @@ func (r *Runner) Run(ctx context.Context, cfg *Config) (drivers.RunResult, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
metricBaseName := fmt.Sprintf("run.%s.", cfg.AppName)
|
metricBaseName := fmt.Sprintf("run.%s.", cfg.AppName)
|
||||||
LogMetricCount(ctx, (metricBaseName + "requests"), 1)
|
r.ml.Log(ctx, Metric{"name": (metricBaseName + "requests"), "type": "count", "value": 1})
|
||||||
|
|
||||||
closer, err := r.driver.Prepare(ctx, ctask)
|
closer, err := r.driver.Prepare(ctx, ctask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -133,7 +135,7 @@ func (r *Runner) Run(ctx context.Context, cfg *Config) (drivers.RunResult, error
|
|||||||
case r.taskQueue <- ctask:
|
case r.taskQueue <- ctask:
|
||||||
default:
|
default:
|
||||||
// If queue is full, return error
|
// If queue is full, return error
|
||||||
LogMetricCount(ctx, "queue.full", 1)
|
r.ml.Log(ctx, Metric{"name": "queue.full", "type": "count", "value": 1})
|
||||||
return nil, ErrFullQueue
|
return nil, ErrFullQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,6 +144,8 @@ func (r *Runner) Run(ctx context.Context, cfg *Config) (drivers.RunResult, error
|
|||||||
// This task timed out, not available memory
|
// This task timed out, not available memory
|
||||||
return nil, ErrTimeOutNoMemory
|
return nil, ErrTimeOutNoMemory
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
r.ml.Log(ctx, Metric{"name": (metricBaseName + "waittime"), "type": "time", "value": 0})
|
||||||
}
|
}
|
||||||
|
|
||||||
metricStart := time.Now()
|
metricStart := time.Now()
|
||||||
@@ -151,13 +155,13 @@ func (r *Runner) Run(ctx context.Context, cfg *Config) (drivers.RunResult, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if result.Status() == "success" {
|
if result.Status() == "success" {
|
||||||
LogMetricCount(ctx, (metricBaseName + "succeeded"), 1)
|
r.ml.Log(ctx, Metric{"name": (metricBaseName + "succeeded"), "type": "count", "value": 1})
|
||||||
} else {
|
} else {
|
||||||
LogMetricCount(ctx, (metricBaseName + "error"), 1)
|
r.ml.Log(ctx, Metric{"name": (metricBaseName + "error"), "type": "count", "value": 1})
|
||||||
}
|
}
|
||||||
|
|
||||||
metricElapsed := time.Since(metricStart)
|
metricElapsed := time.Since(metricStart)
|
||||||
LogMetricTime(ctx, (metricBaseName + "time"), metricElapsed)
|
r.ml.Log(ctx, Metric{"name": (metricBaseName + "time"), "type": "time", "value": metricElapsed})
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
@@ -188,6 +192,9 @@ func selectDriver(driver string, env *common.Environment, conf *driverscommon.Co
|
|||||||
|
|
||||||
func dynamicSizing(reqMem uint64) int {
|
func dynamicSizing(reqMem uint64) int {
|
||||||
const tooBig = 322122547200 // #300GB or 0, biggest aws instance is 244GB
|
const tooBig = 322122547200 // #300GB or 0, biggest aws instance is 244GB
|
||||||
|
if reqMem == 0 {
|
||||||
|
reqMem = 128
|
||||||
|
}
|
||||||
|
|
||||||
availableMemory, err := checkCgroup()
|
availableMemory, err := checkCgroup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRunnerHello(t *testing.T) {
|
func TestRunnerHello(t *testing.T) {
|
||||||
runner, err := New()
|
runner, err := New(NewMetricLogger())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Test error during New() - %s", err)
|
t.Fatalf("Test error during New() - %s", err)
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,7 @@ func TestRunnerHello(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunnerError(t *testing.T) {
|
func TestRunnerError(t *testing.T) {
|
||||||
runner, err := New()
|
runner, err := New(NewMetricLogger())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Test error during New() - %s", err)
|
t.Fatalf("Test error during New() - %s", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func testRouter() *gin.Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testRunner(t *testing.T) *runner.Runner {
|
func testRunner(t *testing.T) *runner.Runner {
|
||||||
r, err := runner.New()
|
r, err := runner.New(runner.NewMetricLogger())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Test: failed to create new runner")
|
t.Fatal("Test: failed to create new runner")
|
||||||
}
|
}
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -20,7 +20,8 @@ func main() {
|
|||||||
log.WithError(err).Fatalln("Invalid DB url.")
|
log.WithError(err).Fatalln("Invalid DB url.")
|
||||||
}
|
}
|
||||||
|
|
||||||
runner, err := runner.New()
|
metricLogger := runner.NewMetricLogger()
|
||||||
|
runner, err := runner.New(metricLogger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatalln("Failed to create a runner")
|
log.WithError(err).Fatalln("Failed to create a runner")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user