-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #789 from nyaruka/clogs4
Update to latest copy of clogs package
- Loading branch information
Showing
10 changed files
with
172 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package clogs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/nyaruka/gocommon/aws/dynamo" | ||
) | ||
|
||
// BatchPut writes multiple logs to DynamoDB in batches of 25. This should probably be a generic function in the | ||
// gocommon/dynamo package but need to think more about errors. | ||
func BatchPut(ctx context.Context, ds *dynamo.Service, table string, logs []*Log) error { | ||
for batch := range slices.Chunk(logs, 25) { | ||
writeReqs := make([]types.WriteRequest, len(batch)) | ||
|
||
for i, l := range batch { | ||
d, err := l.MarshalDynamo() | ||
if err != nil { | ||
return fmt.Errorf("error marshalling log: %w", err) | ||
} | ||
writeReqs[i] = types.WriteRequest{PutRequest: &types.PutRequest{Item: d}} | ||
} | ||
|
||
_, err := ds.Client.BatchWriteItem(ctx, &dynamodb.BatchWriteItemInput{ | ||
RequestItems: map[string][]types.WriteRequest{ds.TableName(table): writeReqs}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error writing logs to db: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package clogs_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/nyaruka/courier/utils/clogs" | ||
"github.com/nyaruka/gocommon/aws/dynamo" | ||
"github.com/nyaruka/gocommon/httpx" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLogs(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
defer httpx.SetRequestor(httpx.DefaultRequestor) | ||
httpx.SetRequestor(httpx.NewMockRequestor(map[string][]*httpx.MockResponse{ | ||
"http://ivr.com/start": {httpx.NewMockResponse(200, nil, []byte("OK"))}, | ||
"http://ivr.com/hangup": {httpx.NewMockResponse(400, nil, []byte("Oops"))}, | ||
})) | ||
|
||
clog1 := clogs.NewLog("type1", nil, []string{"sesame"}) | ||
clog2 := clogs.NewLog("type1", nil, []string{"sesame"}) | ||
|
||
req1, _ := httpx.NewRequest("GET", "http://ivr.com/start", nil, map[string]string{"Authorization": "Token sesame"}) | ||
trace1, err := httpx.DoTrace(http.DefaultClient, req1, nil, nil, -1) | ||
require.NoError(t, err) | ||
|
||
clog1.HTTP(trace1) | ||
clog1.End() | ||
|
||
req2, _ := httpx.NewRequest("GET", "http://ivr.com/hangup", nil, nil) | ||
trace2, err := httpx.DoTrace(http.DefaultClient, req2, nil, nil, -1) | ||
require.NoError(t, err) | ||
|
||
clog2.HTTP(trace2) | ||
clog2.Error(clogs.NewLogError("", "", "oops")) | ||
clog2.End() | ||
|
||
assert.NotEqual(t, clog1.UUID, clog2.UUID) | ||
assert.NotEqual(t, time.Duration(0), clog1.Elapsed) | ||
|
||
ds, err := dynamo.NewService("root", "tembatemba", "us-east-1", "http://localhost:6000", "Test") | ||
require.NoError(t, err) | ||
|
||
l1 := clogs.NewLog("test_type1", nil, nil) | ||
l1.Error(clogs.NewLogError("code1", "ext", "message")) | ||
|
||
l2 := clogs.NewLog("test_type2", nil, nil) | ||
l2.Error(clogs.NewLogError("code2", "ext", "message")) | ||
|
||
// write both logs to db | ||
err = clogs.BatchPut(ctx, ds, "ChannelLogs", []*clogs.Log{l1, l2}) | ||
assert.NoError(t, err) | ||
|
||
// read log 1 back from db | ||
l3 := &clogs.Log{} | ||
err = ds.GetItem(ctx, "ChannelLogs", map[string]types.AttributeValue{"UUID": &types.AttributeValueMemberS{Value: string(l1.UUID)}}, l3) | ||
assert.NoError(t, err) | ||
assert.Equal(t, l1.UUID, l3.UUID) | ||
assert.Equal(t, clogs.LogType("test_type1"), l3.Type) | ||
assert.Equal(t, []*clogs.LogError{clogs.NewLogError("code1", "ext", "message")}, l3.Errors) | ||
assert.Equal(t, l1.Elapsed, l3.Elapsed) | ||
assert.Equal(t, l1.CreatedOn.Truncate(time.Second), l3.CreatedOn) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.