mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Copy logs up to v2 endpoints (#1207)
Copies the log endpoints up to the V2 endpoints, in a similar way to the call endpoints. The main change is to when logs are inserted into S3. The signature of the function has been changed to take the whole call object, rather than just the app and call id's. This allows the function to switch between calls for Routes and those for Fns. Obviously this switching can be removed when v1 is removed. In the sql implementation it inserts with both appID and fnID, this allows the two get's to work, and the down grade of the migration. When the v1 logs are removed, the appId can be dropped. The log fetch test and error messages have been changed to be FnID specific.
This commit is contained in:
@@ -46,10 +46,10 @@ func (m *metricls) GetCalls(ctx context.Context, filter *models.CallFilter) (*mo
|
||||
return m.ls.GetCalls(ctx, filter)
|
||||
}
|
||||
|
||||
func (m *metricls) InsertLog(ctx context.Context, appName, callID string, callLog io.Reader) error {
|
||||
func (m *metricls) InsertLog(ctx context.Context, call *models.Call, callLog io.Reader) error {
|
||||
ctx, span := trace.StartSpan(ctx, "ls_insert_log")
|
||||
defer span.End()
|
||||
return m.ls.InsertLog(ctx, appName, callID, callLog)
|
||||
return m.ls.InsertLog(ctx, call, callLog)
|
||||
}
|
||||
|
||||
func (m *metricls) GetLog(ctx context.Context, appName, callID string) (io.Reader, error) {
|
||||
|
||||
@@ -32,13 +32,21 @@ func NewMock(args ...interface{}) models.LogStore {
|
||||
return &mocker
|
||||
}
|
||||
|
||||
func (m *mock) InsertLog(ctx context.Context, appID, callID string, callLog io.Reader) error {
|
||||
func (m *mock) InsertLog(ctx context.Context, call *models.Call, callLog io.Reader) error {
|
||||
bytes, err := ioutil.ReadAll(callLog)
|
||||
m.Logs[callID] = bytes
|
||||
m.Logs[call.ID] = bytes
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *mock) GetLog(ctx context.Context, appID, callID string) (io.Reader, error) {
|
||||
func (m *mock) GetLog1(ctx context.Context, appID, callID string) (io.Reader, error) {
|
||||
logEntry, ok := m.Logs[callID]
|
||||
if !ok {
|
||||
return nil, models.ErrCallLogNotFound
|
||||
}
|
||||
return bytes.NewReader(logEntry), nil
|
||||
}
|
||||
|
||||
func (m *mock) GetLog(ctx context.Context, fnID, callID string) (io.Reader, error) {
|
||||
logEntry, ok := m.Logs[callID]
|
||||
if !ok {
|
||||
return nil, models.ErrCallLogNotFound
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
logTesting "github.com/fnproject/fn/api/logs/testing"
|
||||
"testing"
|
||||
|
||||
logTesting "github.com/fnproject/fn/api/logs/testing"
|
||||
)
|
||||
|
||||
func TestMock(t *testing.T) {
|
||||
|
||||
@@ -136,13 +136,20 @@ func (s3StoreProvider) New(ctx context.Context, u *url.URL) (models.LogStore, er
|
||||
return store, nil
|
||||
}
|
||||
|
||||
func (s *store) InsertLog(ctx context.Context, appID, callID string, callLog io.Reader) error {
|
||||
func (s *store) InsertLog(ctx context.Context, call *models.Call, callLog io.Reader) error {
|
||||
ctx, span := trace.StartSpan(ctx, "s3_insert_log")
|
||||
defer span.End()
|
||||
|
||||
// wrap original reader in a decorator to keep track of read bytes without buffering
|
||||
cr := &countingReader{r: callLog}
|
||||
objectName := logKey(appID, callID)
|
||||
|
||||
objectName := ""
|
||||
if call.FnID != "" {
|
||||
objectName = logKey(call.FnID, call.ID)
|
||||
} else {
|
||||
objectName = logKey(call.AppID, call.ID)
|
||||
}
|
||||
|
||||
params := &s3manager.UploadInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(objectName),
|
||||
@@ -317,10 +324,6 @@ func logKey(appID, callID string) string {
|
||||
return logKeyPrefix + appID + "/" + callID
|
||||
}
|
||||
|
||||
func logKey2(callID string) string {
|
||||
return logKeyPrefix + callID
|
||||
}
|
||||
|
||||
// GetCalls1 returns a list of calls that satisfy the given CallFilter. If no
|
||||
// calls exist, an empty list and a nil error are returned.
|
||||
// NOTE: this relies on call ids being lexicographically sortable and <= 16 byte
|
||||
|
||||
@@ -145,11 +145,11 @@ func Test(t *testing.T, fnl models.LogStore) {
|
||||
call.ID = id.New().String()
|
||||
logText := "test"
|
||||
log := strings.NewReader(logText)
|
||||
err := fnl.InsertLog(ctx, call.AppID, call.ID, log)
|
||||
err := fnl.InsertLog(ctx, call, log)
|
||||
if err != nil {
|
||||
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
|
||||
}
|
||||
logEntry, err := fnl.GetLog(ctx, call.AppID, call.ID)
|
||||
logEntry, err := fnl.GetLog(ctx, call.FnID, call.ID)
|
||||
var b bytes.Buffer
|
||||
io.Copy(&b, logEntry)
|
||||
if !strings.Contains(b.String(), logText) {
|
||||
@@ -160,7 +160,7 @@ func Test(t *testing.T, fnl models.LogStore) {
|
||||
|
||||
t.Run("call-log-not-found", func(t *testing.T) {
|
||||
call.ID = id.New().String()
|
||||
_, err := fnl.GetLog(ctx, call.AppID, call.ID)
|
||||
_, err := fnl.GetLog(ctx, call.FnID, call.ID)
|
||||
if err != models.ErrCallLogNotFound {
|
||||
t.Fatal("GetLog should return not found, but got:", err)
|
||||
}
|
||||
@@ -172,7 +172,7 @@ func Test(t *testing.T, fnl models.LogStore) {
|
||||
call.Error = "ya dun goofed"
|
||||
call.StartedAt = common.DateTime(time.Now())
|
||||
call.CompletedAt = common.DateTime(time.Now())
|
||||
call.AppID = testApp.Name
|
||||
call.AppID = testApp.ID
|
||||
call.FnID = testFn.ID
|
||||
|
||||
t.Run("call-insert", func(t *testing.T) {
|
||||
@@ -212,7 +212,7 @@ func Test(t *testing.T, fnl models.LogStore) {
|
||||
t.Fatalf("Test GetCall: completed_at mismatch `%v` `%v`", call.CompletedAt, newCall.CompletedAt)
|
||||
}
|
||||
if call.AppID != newCall.AppID {
|
||||
t.Fatalf("Test GetCall: app_name mismatch `%v` `%v`", call.AppID, newCall.AppID)
|
||||
t.Fatalf("Test GetCall: fn id mismatch `%v` `%v`", call.FnID, newCall.FnID)
|
||||
}
|
||||
if call.Path != newCall.Path {
|
||||
t.Fatalf("Test GetCall: path mismatch `%v` `%v`", call.Path, newCall.Path)
|
||||
|
||||
@@ -16,14 +16,14 @@ type validator struct {
|
||||
}
|
||||
|
||||
// callID or appID will never be empty.
|
||||
func (v *validator) InsertLog(ctx context.Context, appID, callID string, callLog io.Reader) error {
|
||||
if callID == "" {
|
||||
func (v *validator) InsertLog(ctx context.Context, call *models.Call, callLog io.Reader) error {
|
||||
if call.ID == "" {
|
||||
return models.ErrDatastoreEmptyCallID
|
||||
}
|
||||
if appID == "" {
|
||||
return models.ErrMissingAppID
|
||||
if call.AppID == "" && call.FnID == "" {
|
||||
return models.ErrMissingFnID
|
||||
}
|
||||
return v.LogStore.InsertLog(ctx, appID, callID, callLog)
|
||||
return v.LogStore.InsertLog(ctx, call, callLog)
|
||||
}
|
||||
|
||||
// callID or appID will never be empty.
|
||||
@@ -32,7 +32,7 @@ func (v *validator) GetLog(ctx context.Context, appID, callID string) (io.Reader
|
||||
return nil, models.ErrDatastoreEmptyCallID
|
||||
}
|
||||
if appID == "" {
|
||||
return nil, models.ErrMissingAppID
|
||||
return nil, models.ErrMissingFnID
|
||||
}
|
||||
return v.LogStore.GetLog(ctx, appID, callID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user