-
Couldn't load subscription status.
- Fork 395
Batch traces before making API calls #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5dbf8d1
e13d1af
b54d242
a98c16f
5a2a6a7
ea9a3d0
340dbf6
0e28d99
d50d9eb
dc7c6f4
9cc07c8
129dcd1
7766140
cc449e6
f601e96
d78fb13
dd7f5af
41d3b24
23bd133
fda0196
4f111ba
59513d5
e46cac5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from mock import MagicMock, patch | ||
| import os | ||
| import sys | ||
| import unittest | ||
|
|
||
| sys.modules["datadog_lambda.wrapper"] = MagicMock() | ||
| sys.modules["datadog_lambda.metric"] = MagicMock() | ||
| sys.modules["datadog"] = MagicMock() | ||
| sys.modules["requests"] = MagicMock() | ||
|
|
||
| env_patch = patch.dict(os.environ, {"DD_API_KEY": "11111111111111111111111111111111"}) | ||
| env_patch.start() | ||
| from lambda_function import batch_trace_payloads | ||
|
|
||
| env_patch.stop() | ||
|
|
||
|
|
||
| class TestBatchTracePayloads(unittest.TestCase): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice test |
||
| def test_batch_trace_payloads(self): | ||
| trace_payloads = [ | ||
| {"tags": "tag1:value", "message": '{"traces":[[{"trace_id":"1"}]]}\n',}, | ||
| { | ||
| "tags": "tag1:value", | ||
| "message": '{"traces":[[{"trace_id":"2"}, {"trace_id":"3"}]]}\n', | ||
| }, | ||
| { | ||
| "tags": "tag2:value", | ||
| "message": '{"traces":[[{"trace_id":"4"}], [{"trace_id":"5"}]]}\n', | ||
| }, | ||
| ] | ||
|
|
||
| batched_payloads = batch_trace_payloads(trace_payloads) | ||
|
|
||
| expected_batched_payloads = [ | ||
| { | ||
| "tags": "tag1:value", | ||
| "message": '{"traces": [[{"trace_id": "1"}], [{"trace_id": "2"}, {"trace_id": "3"}]]}', | ||
| }, | ||
| { | ||
| "tags": "tag2:value", | ||
| "message": '{"traces": [[{"trace_id": "4"}], [{"trace_id": "5"}]]}', | ||
| }, | ||
| ] | ||
|
|
||
| self.assertEqual(batched_payloads, expected_batched_payloads) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,10 @@ import ( | |
|
|
||
| "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring/trace_forwarder/internal/apm" | ||
| ) | ||
| import "github.com/DataDog/datadog-agent/pkg/trace/obfuscate" | ||
| import ( | ||
| "github.com/DataDog/datadog-agent/pkg/trace/obfuscate" | ||
| "github.com/DataDog/datadog-agent/pkg/trace/pb" | ||
| ) | ||
|
|
||
| var ( | ||
| obfuscator *obfuscate.Obfuscator | ||
|
|
@@ -45,35 +48,46 @@ func Configure(rootURL, apiKey string) { | |
| edgeConnection = apm.CreateTraceEdgeConnection(localRootURL, localAPIKey) | ||
| } | ||
|
|
||
| // ForwardTrace will perform filtering and log forwarding to the trace intake | ||
| // ForwardTraces will perform filtering and log forwarding to the trace intake | ||
| // returns 0 on success, 1 on error | ||
| //export ForwardTrace | ||
| func ForwardTrace(content string, tags string) int { | ||
| //export ForwardTraces | ||
| func ForwardTraces(content string, tags string) int { | ||
| tracePayloads, err := apm.ProcessTrace(content, obfuscator, tags) | ||
| if err != nil { | ||
| fmt.Printf("Couldn't forward trace: %v", err) | ||
| fmt.Printf("Couldn't forward traces: %v", err) | ||
| return 1 | ||
| } | ||
| hadErr := false | ||
|
|
||
| for _, tracePayload := range tracePayloads { | ||
| combinedPayload := combinePayloads(tracePayloads) | ||
|
|
||
| err = edgeConnection.SendTraces(context.Background(), tracePayload, 3) | ||
| if err != nil { | ||
| fmt.Printf("Failed to send traces with error %v\n", err) | ||
| hadErr = true | ||
| } | ||
| stats := apm.ComputeAPMStats(tracePayload) | ||
| err = edgeConnection.SendStats(context.Background(), stats, 3) | ||
| if err != nil { | ||
| fmt.Printf("Failed to send trace stats with error %v\n", err) | ||
| hadErr = true | ||
| } | ||
| err = edgeConnection.SendTraces(context.Background(), combinedPayload, 3) | ||
| if err != nil { | ||
| fmt.Printf("Failed to send traces with error %v\n", err) | ||
| return 1 | ||
| } | ||
| if hadErr { | ||
|
|
||
| stats := apm.ComputeAPMStats(combinedPayload) | ||
| err = edgeConnection.SendStats(context.Background(), stats, 3) | ||
| if err != nil { | ||
| fmt.Printf("Failed to send trace stats with error %v\n", err) | ||
| return 1 | ||
| } | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| // Combine payloads into one | ||
| // Assumes that all payloads have the same HostName and Env | ||
| func combinePayloads(tracePayloads []*pb.TracePayload) *pb.TracePayload { | ||
| combinedPayload := &pb.TracePayload{ | ||
| HostName: tracePayloads[0].HostName, | ||
| Env: tracePayloads[0].Env, | ||
| Traces: make([]*pb.APITrace, 0), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: do we already know the size this slice will be? Maybe we can pre-allocate |
||
| } | ||
| for _, tracePayload := range tracePayloads { | ||
| combinedPayload.Traces = append(combinedPayload.Traces, tracePayload.Traces...) | ||
| } | ||
| return combinedPayload | ||
| } | ||
|
|
||
| func main() {} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are these constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are:
I copy-pasted these specific numbers from the other usage of DatadogBatcher in this file, which is for batching logs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we refactor them into constant variables?