Making logs app-bound

Partially-Closes: #302
This commit is contained in:
Denis Makogon
2017-09-11 10:57:53 +03:00
parent 26b047a856
commit 774d53662f
8 changed files with 42 additions and 39 deletions

View File

@@ -51,10 +51,11 @@ func NewFuncLogger(ctx context.Context, appName, path, image, reqID string, logD
// we don't need to log per line to db, but we do need to limit it // we don't need to log per line to db, but we do need to limit it
limitw := newLimitWriter(MB, &dbWriter{ limitw := newLimitWriter(MB, &dbWriter{
Buffer: dbuf, Buffer: dbuf,
db: logDB, db: logDB,
ctx: ctx, ctx: ctx,
reqID: reqID, reqID: reqID,
appName: appName,
}) })
// TODO / NOTE: we want linew to be first because limitw may error if limit // TODO / NOTE: we want linew to be first because limitw may error if limit
@@ -177,15 +178,16 @@ func (li *lineWriter) Close() error {
type dbWriter struct { type dbWriter struct {
*bytes.Buffer *bytes.Buffer
db models.LogStore db models.LogStore
ctx context.Context ctx context.Context
reqID string reqID string
appName string
} }
func (w *dbWriter) Close() error { func (w *dbWriter) Close() error {
span, ctx := opentracing.StartSpanFromContext(context.Background(), "agent_log_write") span, ctx := opentracing.StartSpanFromContext(context.Background(), "agent_log_write")
defer span.Finish() defer span.Finish()
return w.db.InsertLog(ctx, w.reqID, w.String()) return w.db.InsertLog(ctx, w.appName, w.reqID, w.String())
} }
func (w *dbWriter) Write(b []byte) (int, error) { func (w *dbWriter) Write(b []byte) (int, error) {

View File

@@ -100,22 +100,22 @@ func (m *metricds) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*
return m.ds.GetCalls(ctx, filter) return m.ds.GetCalls(ctx, filter)
} }
func (m *metricds) InsertLog(ctx context.Context, callID string, callLog string) error { func (m *metricds) InsertLog(ctx context.Context, appName, callID, callLog string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_log") span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_log")
defer span.Finish() defer span.Finish()
return m.ds.InsertLog(ctx, callID, callLog) return m.ds.InsertLog(ctx, appName, callID, callLog)
} }
func (m *metricds) GetLog(ctx context.Context, callID string) (*models.CallLog, error) { func (m *metricds) GetLog(ctx context.Context, appName, callID string) (*models.CallLog, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_log") span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_log")
defer span.Finish() defer span.Finish()
return m.ds.GetLog(ctx, callID) return m.ds.GetLog(ctx, appName, callID)
} }
func (m *metricds) DeleteLog(ctx context.Context, callID string) error { func (m *metricds) DeleteLog(ctx context.Context, appName, callID string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_delete_log") span, ctx := opentracing.StartSpanFromContext(ctx, "ds_delete_log")
defer span.Finish() defer span.Finish()
return m.ds.DeleteLog(ctx, callID) return m.ds.DeleteLog(ctx, appName, callID)
} }
// instant & no context ;) // instant & no context ;)

View File

@@ -138,8 +138,8 @@ func (v *validator) GetCall(ctx context.Context, appName, callID string) (*model
return v.Datastore.GetCall(ctx, appName, callID) return v.Datastore.GetCall(ctx, appName, callID)
} }
func (v *validator) DeleteLog(ctx context.Context, callID string) error { func (v *validator) DeleteLog(ctx context.Context, appName, callID string) error {
return v.Datastore.DeleteLog(ctx, callID) return v.Datastore.DeleteLog(ctx, appName, callID)
} }
// GetDatabase returns the underlying sqlx database implementation // GetDatabase returns the underlying sqlx database implementation

View File

@@ -61,6 +61,7 @@ var tables = [...]string{`CREATE TABLE IF NOT EXISTS routes (
`CREATE TABLE IF NOT EXISTS logs ( `CREATE TABLE IF NOT EXISTS logs (
id varchar(256) NOT NULL PRIMARY KEY, id varchar(256) NOT NULL PRIMARY KEY,
app_name varchar(256) NOT NULL,
log text NOT NULL log text NOT NULL
);`, );`,
} }
@@ -589,15 +590,15 @@ func (ds *sqlStore) GetCalls(ctx context.Context, filter *models.CallFilter) ([]
return res, nil return res, nil
} }
func (ds *sqlStore) InsertLog(ctx context.Context, callID, callLog string) error { func (ds *sqlStore) InsertLog(ctx context.Context, appName, callID, callLog string) error {
query := ds.db.Rebind(`INSERT INTO logs (id, log) VALUES (?, ?);`) query := ds.db.Rebind(`INSERT INTO logs (id, app_name, log) VALUES (?, ?, ?);`)
_, err := ds.db.ExecContext(ctx, query, callID, callLog) _, err := ds.db.ExecContext(ctx, query, callID, appName, callLog)
return err return err
} }
func (ds *sqlStore) GetLog(ctx context.Context, callID string) (*models.CallLog, error) { func (ds *sqlStore) GetLog(ctx context.Context, appName, callID string) (*models.CallLog, error) {
query := ds.db.Rebind(`SELECT log FROM logs WHERE id=?`) query := ds.db.Rebind(`SELECT log FROM logs WHERE id=? AND app_name=?`)
row := ds.db.QueryRowContext(ctx, query, callID) row := ds.db.QueryRowContext(ctx, query, callID, appName)
var log string var log string
err := row.Scan(&log) err := row.Scan(&log)
@@ -614,9 +615,9 @@ func (ds *sqlStore) GetLog(ctx context.Context, callID string) (*models.CallLog,
}, nil }, nil
} }
func (ds *sqlStore) DeleteLog(ctx context.Context, callID string) error { func (ds *sqlStore) DeleteLog(ctx context.Context, appName, callID string) error {
query := ds.db.Rebind(`DELETE FROM logs WHERE id=?`) query := ds.db.Rebind(`DELETE FROM logs WHERE id=? AND app_name=?`)
_, err := ds.db.ExecContext(ctx, query, callID) _, err := ds.db.ExecContext(ctx, query, callID, appName)
return err return err
} }

View File

@@ -27,12 +27,12 @@ func (m *mock) SetDatastore(ctx context.Context, ds models.Datastore) {
m.ds = ds m.ds = ds
} }
func (m *mock) InsertLog(ctx context.Context, callID string, callLog string) error { func (m *mock) InsertLog(ctx context.Context, appName, callID, callLog string) error {
m.Logs[callID] = &models.CallLog{CallID: callID, Log: callLog} m.Logs[callID] = &models.CallLog{CallID: callID, Log: callLog}
return nil return nil
} }
func (m *mock) GetLog(ctx context.Context, callID string) (*models.CallLog, error) { func (m *mock) GetLog(ctx context.Context, appName, callID string) (*models.CallLog, error) {
logEntry := m.Logs[callID] logEntry := m.Logs[callID]
if logEntry == nil { if logEntry == nil {
return nil, errors.New("Call log not found") return nil, errors.New("Call log not found")
@@ -41,7 +41,7 @@ func (m *mock) GetLog(ctx context.Context, callID string) (*models.CallLog, erro
return m.Logs[callID], nil return m.Logs[callID], nil
} }
func (m *mock) DeleteLog(ctx context.Context, callID string) error { func (m *mock) DeleteLog(ctx context.Context, appName, callID string) error {
delete(m.Logs, callID) delete(m.Logs, callID)
return nil return nil
} }

View File

@@ -44,7 +44,7 @@ func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
if err != nil { if err != nil {
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err) t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
} }
err = fnl.InsertLog(ctx, call.ID, "test") err = fnl.InsertLog(ctx, call.AppName, call.ID, "test")
if err != nil { if err != nil {
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err) t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
} }
@@ -56,11 +56,11 @@ func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
if err != nil { if err != nil {
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err) t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
} }
err = fnl.InsertLog(ctx, call.ID, logText) err = fnl.InsertLog(ctx, call.AppName, call.ID, logText)
if err != nil { if err != nil {
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err) t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
} }
logEntry, err := fnl.GetLog(ctx, call.ID) logEntry, err := fnl.GetLog(ctx, call.AppName, call.ID)
if !strings.Contains(logEntry.Log, logText) { if !strings.Contains(logEntry.Log, logText) {
t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+ t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+
"Expected: `%v`. Got `%v`.", logText, logEntry.Log) "Expected: `%v`. Got `%v`.", logText, logEntry.Log)
@@ -73,16 +73,16 @@ func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
if err != nil { if err != nil {
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err) t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
} }
err = fnl.InsertLog(ctx, call.ID, logText) err = fnl.InsertLog(ctx, call.AppName, call.ID, logText)
if err != nil { if err != nil {
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err) t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
} }
logEntry, err := fnl.GetLog(ctx, call.ID) logEntry, err := fnl.GetLog(ctx, call.AppName, call.ID)
if !strings.Contains(logEntry.Log, logText) { if !strings.Contains(logEntry.Log, logText) {
t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+ t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+
"Expected: `%v`. Got `%v`.", logText, logEntry.Log) "Expected: `%v`. Got `%v`.", logText, logEntry.Log)
} }
err = fnl.DeleteLog(ctx, call.ID) err = fnl.DeleteLog(ctx, call.AppName, call.ID)
if err != nil { if err != nil {
t.Fatalf("Test DeleteLog(ctx, call.ID): unexpected error during deleting log `%v`", err) t.Fatalf("Test DeleteLog(ctx, call.ID): unexpected error during deleting log `%v`", err)
} }

View File

@@ -11,13 +11,13 @@ type LogStore interface {
// InsertLog will insert the log at callID, overwriting if it previously // InsertLog will insert the log at callID, overwriting if it previously
// existed. // existed.
InsertLog(ctx context.Context, callID string, callLog string) error InsertLog(ctx context.Context, appName, callID string, callLog string) error
// GetLog will return the log at callID, an error will be returned if the log // GetLog will return the log at callID, an error will be returned if the log
// cannot be found. // cannot be found.
GetLog(ctx context.Context, callID string) (*CallLog, error) GetLog(ctx context.Context, appName, callID string) (*CallLog, error)
// DeleteLog will remove the log at callID, it will not return an error if // DeleteLog will remove the log at callID, it will not return an error if
// the log does not exist before removal. // the log does not exist before removal.
DeleteLog(ctx context.Context, callID string) error DeleteLog(ctx context.Context, appName, callID string) error
} }

View File

@@ -18,7 +18,7 @@ func (s *Server) handleCallLogGet(c *gin.Context) {
return return
} }
callObj, err := s.LogDB.GetLog(ctx, callID) callObj, err := s.LogDB.GetLog(ctx, appName, callID)
if err != nil { if err != nil {
handleErrorResponse(c, err) handleErrorResponse(c, err)
return return
@@ -37,7 +37,7 @@ func (s *Server) handleCallLogDelete(c *gin.Context) {
handleErrorResponse(c, err) handleErrorResponse(c, err)
return return
} }
err = s.LogDB.DeleteLog(ctx, callID) err = s.LogDB.DeleteLog(ctx, appName, callID)
if err != nil { if err != nil {
handleErrorResponse(c, err) handleErrorResponse(c, err)
return return