-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EMF concurrency test to check for log corruption. (#351)
- Loading branch information
Showing
5 changed files
with
278 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package emf_concurrent | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent-test/environment" | ||
"github.com/aws/amazon-cloudwatch-agent-test/util/awsservice" | ||
"github.com/aws/amazon-cloudwatch-agent-test/util/common" | ||
) | ||
|
||
const ( | ||
testRuntime = 10 * time.Minute | ||
threadCount = 15 | ||
connectionCount = 5 | ||
interval = 500 * time.Millisecond | ||
emfAddress = "0.0.0.0:25888" | ||
) | ||
|
||
var ( | ||
// queryString checks that both metric values are the same and have the same expected unit. | ||
queryString = fmt.Sprintf("filter ispresent(%[1]s) and ispresent(%[2]s) and (%[1]s != %[2]s or (_aws.CloudWatchMetrics.0.Metrics.0.Unit!=%[3]q) or (_aws.CloudWatchMetrics.0.Metrics.1.Unit!=%[3]q))", metricName1, metricName2, metricUnit) | ||
) | ||
|
||
func init() { | ||
environment.RegisterEnvironmentMetaDataFlags() | ||
} | ||
|
||
func TestConcurrent(t *testing.T) { | ||
env := environment.GetEnvironmentMetaData() | ||
|
||
common.CopyFile(filepath.Join("testdata", "config.json"), common.ConfigOutputPath) | ||
require.NoError(t, common.StartAgent(common.ConfigOutputPath, true, false)) | ||
|
||
// wait for agent to start up | ||
time.Sleep(5 * time.Second) | ||
|
||
e := &emitter{ | ||
interval: interval, | ||
logGroupName: fmt.Sprintf("emf-test-group-%s", env.InstanceId), | ||
logStreamName: fmt.Sprintf("emf-test-stream-%s", env.InstanceId), | ||
dimension: env.CwaCommitSha, | ||
done: make(chan struct{}), | ||
} | ||
|
||
defer awsservice.DeleteLogGroup(e.logGroupName) | ||
|
||
tcpAddr, err := net.ResolveTCPAddr("tcp", emfAddress) | ||
if err != nil { | ||
log.Fatalf("invalid tcp emfAddress (%s): %v", emfAddress, err) | ||
} | ||
|
||
var conns []*net.TCPConn | ||
for i := 0; i < connectionCount; i++ { | ||
var conn *net.TCPConn | ||
conn, err = net.DialTCP("tcp", nil, tcpAddr) | ||
if err != nil { | ||
log.Fatalf("unable to connect to address (%s): %v", emfAddress, err) | ||
} | ||
conns = append(conns, conn) | ||
} | ||
|
||
log.Printf("Starting EMF emitters for log group (%s)/stream (%s)", e.logGroupName, e.logStreamName) | ||
startTime := time.Now() | ||
for i := 0; i < threadCount; i++ { | ||
e.wg.Add(1) | ||
go e.start(conns[i%len(conns)]) | ||
} | ||
time.Sleep(testRuntime) | ||
close(e.done) | ||
log.Println("Stopping EMF emitters") | ||
e.wg.Wait() | ||
common.StopAgent() | ||
endTime := time.Now() | ||
|
||
assert.Lenf(t, awsservice.GetLogStreamNames(e.logGroupName), 1, "Detected corruption: multiple streams found") | ||
log.Printf("Starting query for log group (%s): %s", e.logGroupName, queryString) | ||
got, err := awsservice.GetLogQueryStats(e.logGroupName, startTime.Unix(), endTime.Unix(), queryString) | ||
require.NoError(t, err, "Unable to get log query stats") | ||
assert.NotZero(t, got.RecordsScanned, "No records found in CloudWatch Logs") | ||
assert.Zerof(t, got.RecordsMatched, "Detected corruption: %v/%v records matched", got.RecordsMatched, got.RecordsScanned) | ||
} |
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,109 @@ | ||
package emf_concurrent | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
metadataName = "_aws" | ||
namespace = "ConcurrentEMFTest" | ||
metricName1 = "ExecutionTime" | ||
metricName2 = "DuplicateExecutionTime" | ||
metricValue = 1.23456789 | ||
metricUnit = "Seconds" | ||
dimensionName = "Dimension" | ||
randomName = "Random" | ||
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
) | ||
|
||
var ( | ||
newLineChar = []byte("\n") | ||
) | ||
|
||
type Metadata struct { | ||
Timestamp int64 `json:"Timestamp"` | ||
LogGroupName string `json:"LogGroupName"` | ||
LogStreamName string `json:"LogStreamName"` | ||
CloudWatchMetrics []CWMetric `json:"CloudWatchMetrics"` | ||
} | ||
|
||
type CWMetric struct { | ||
Namespace string `json:"Namespace"` | ||
Dimensions [][]string `json:"Dimensions"` | ||
Metrics []Metric `json:"Metrics"` | ||
} | ||
|
||
type Metric struct { | ||
Name string `json:"Name"` | ||
Unit string `json:"Unit"` | ||
} | ||
|
||
type emitter struct { | ||
wg sync.WaitGroup | ||
done chan struct{} | ||
interval time.Duration | ||
logGroupName string | ||
logStreamName string | ||
dimension string | ||
} | ||
|
||
func (e *emitter) start(conn *net.TCPConn) { | ||
defer e.wg.Done() | ||
ticker := time.NewTicker(e.interval) | ||
metadata := e.createMetadata() | ||
for { | ||
select { | ||
case <-e.done: | ||
ticker.Stop() | ||
return | ||
case <-ticker.C: | ||
metadata.Timestamp = time.Now().UnixMilli() | ||
_, _ = conn.Write(e.createEmfLog(metadata)) | ||
} | ||
} | ||
} | ||
|
||
func (e *emitter) createMetadata() *Metadata { | ||
return &Metadata{ | ||
Timestamp: time.Now().UnixMilli(), | ||
LogGroupName: e.logGroupName, | ||
LogStreamName: e.logStreamName, | ||
CloudWatchMetrics: []CWMetric{ | ||
{ | ||
Namespace: namespace, | ||
Dimensions: [][]string{{dimensionName}}, | ||
Metrics: []Metric{ | ||
{Name: metricName1, Unit: metricUnit}, | ||
{Name: metricName2, Unit: metricUnit}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (e *emitter) createEmfLog(metadata *Metadata) []byte { | ||
r := rand.Intn(99) + 1 | ||
emfLog := map[string]interface{}{ | ||
metadataName: metadata, | ||
dimensionName: e.dimension, | ||
metricName1: metricValue, | ||
metricName2: metricValue, | ||
// introduces variability in payload size | ||
randomName: fmt.Sprintf("https://www.amazon.com/%s", randString(r)), | ||
} | ||
content, _ := json.Marshal(emfLog) | ||
return append(content, newLineChar...) | ||
} | ||
|
||
func randString(n int) string { | ||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letters[rand.Intn(len(letters))] | ||
} | ||
return string(b) | ||
} |
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,11 @@ | ||
{ | ||
"agent": { | ||
"debug": true | ||
}, | ||
"logs": { | ||
"metrics_collected": { | ||
"emf": { | ||
} | ||
} | ||
} | ||
} |
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