mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
make fn logger more reasonable
something still feels off with this, but i tinkered with it for a day-ish and didn't come up with anything a whole lot better. doing a lot of the maneuvering in the caller seemed better but it was just bloating up GetCall so went back to having it basically like it was, but returning the limited underlying buffer to read from so we can ship to the db. some small changes to the LogStore interface, swapped it to take an io.Reader instead of a string for more flexibility in the future while essentially maintaining the same level of performance that we have now. i'm guessing in the not so distant future we'll ship these to some s3 like service and it would be better to stream them in than carry around a giant string anyway. also, carrying around up to 1MB buffers in memory isn't great, we may want to switch to file backed logs for calls, too. using io.Reader for logs should make #279 more reasonable if/once we move to some s3-like thing, we can stream from the log storage service direct to clients. this fixes the span being out of whack and allows the 'right' context to be used to upload logs (next to inserting the call). deletes the dbWriter we had, and we just do this in call.End now (which makes sense to me at least). removes the dupe code for making an stderr for hot / cold and simplifies the way to get a func logger (no more 7 param methods yay). closes #298
This commit is contained in:
@@ -2,6 +2,7 @@ package datastoreutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/fnproject/fn/api/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -100,7 +101,7 @@ func (m *metricds) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*
|
||||
return m.ds.GetCalls(ctx, filter)
|
||||
}
|
||||
|
||||
func (m *metricds) InsertLog(ctx context.Context, appName, callID, callLog string) error {
|
||||
func (m *metricds) InsertLog(ctx context.Context, appName, callID string, callLog io.Reader) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_log")
|
||||
defer span.Finish()
|
||||
return m.ds.InsertLog(ctx, appName, callID, callLog)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -600,9 +601,21 @@ func (ds *sqlStore) GetCalls(ctx context.Context, filter *models.CallFilter) ([]
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (ds *sqlStore) InsertLog(ctx context.Context, appName, callID, callLog string) error {
|
||||
func (ds *sqlStore) InsertLog(ctx context.Context, appName, callID string, logR io.Reader) error {
|
||||
// coerce this into a string for sql
|
||||
var log string
|
||||
if stringer, ok := logR.(fmt.Stringer); ok {
|
||||
log = stringer.String()
|
||||
} else {
|
||||
// TODO we could optimize for Size / buffer pool, but atm we aren't hitting
|
||||
// this code path anyway (a fallback)
|
||||
var b bytes.Buffer
|
||||
io.Copy(&b, logR)
|
||||
log = b.String()
|
||||
}
|
||||
|
||||
query := ds.db.Rebind(`INSERT INTO logs (id, app_name, log) VALUES (?, ?, ?);`)
|
||||
_, err := ds.db.ExecContext(ctx, query, callID, appName, callLog)
|
||||
_, err := ds.db.ExecContext(ctx, query, callID, appName, log)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user