Skip to content

Commit

Permalink
Multi-System Trace Integration Test (#318)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeffrey Chien <[email protected]>
  • Loading branch information
okankoAMZ and jefchien authored Aug 10, 2023
1 parent fbd0044 commit 11f3c4e
Show file tree
Hide file tree
Showing 14 changed files with 1,126 additions and 25 deletions.
2 changes: 2 additions & 0 deletions generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var testTypeToTestConfig = map[string][]testConfig{
{testDir: "./test/collection_interval"},
{testDir: "./test/metric_dimension"},
{testDir: "./test/restart"},
{testDir: "./test/xray"},
{testDir: "./test/otlp"},
{
testDir: "./test/acceptance",
targets: map[string]map[string]struct{}{"os": {"ubuntu-20.04": {}}},
Expand Down
41 changes: 33 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
collectd.org v0.5.0
github.com/DataDog/datadog-go v4.8.3+incompatible
github.com/aws/aws-sdk-go v1.44.262
github.com/aws/aws-sdk-go-v2 v1.18.0
github.com/aws/aws-sdk-go-v2 v1.19.0
github.com/aws/aws-sdk-go-v2/config v1.18.10
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.0
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.21
Expand All @@ -22,25 +22,33 @@ require (
github.com/aws/aws-sdk-go-v2/service/ecs v1.23.2
github.com/aws/aws-sdk-go-v2/service/s3 v1.30.1
github.com/aws/aws-sdk-go-v2/service/ssm v1.33.0
github.com/cenkalti/backoff/v4 v4.2.0
github.com/aws/aws-sdk-go-v2/service/xray v1.16.14
github.com/aws/aws-xray-sdk-go v1.8.1
github.com/cenkalti/backoff/v4 v4.2.1
github.com/google/uuid v1.3.0
github.com/mitchellh/mapstructure v1.5.0
github.com/prozz/aws-embedded-metrics-golang v1.2.0
github.com/qri-io/jsonschema v0.2.1
github.com/shirou/gopsutil/v3 v3.23.3
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.3
go.opentelemetry.io/contrib/propagators/aws v1.17.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
go.uber.org/multierr v1.9.0
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
golang.org/x/sys v0.6.0
golang.org/x/sys v0.8.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.33 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.35 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.28 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.18 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.13.20 // indirect
Expand All @@ -54,17 +62,34 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.18.2 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/qri-io/jsonpointer v0.1.1 // indirect
github.com/shoenig/go-m1cpu v0.1.4 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.34.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
476 changes: 463 additions & 13 deletions go.sum

Large diffs are not rendered by default.

135 changes: 135 additions & 0 deletions test/otlp/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package otlp

import (
"context"
"errors"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/util/common"
"go.opentelemetry.io/contrib/propagators/aws/xray"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/maps"
)

var generatorError = errors.New("Generator error")

const (
serviceName = "load-generator"
attributeKeyAwsXrayAnnotations = "aws.xray.annotations"
)

type OtlpTracesGenerator struct {
common.TraceGenerator
common.TraceGeneratorInterface
}

func (g *OtlpTracesGenerator) StartSendingTraces(ctx context.Context) error {
client, shutdown, err := setupClient(ctx)
if err != nil {
return err
}
defer shutdown(ctx)
ticker := time.NewTicker(g.Cfg.Interval)
for {
select {
case <-g.Done:
ticker.Stop()
return client.ForceFlush(ctx)
case <-ticker.C:
if err = g.Generate(ctx); err != nil {
return err
}
}
}
}
func (g *OtlpTracesGenerator) StopSendingTraces() {
close(g.Done)
}
func newLoadGenerator(cfg *common.TraceGeneratorConfig) *OtlpTracesGenerator {
return &OtlpTracesGenerator{
TraceGenerator: common.TraceGenerator{
Cfg: cfg,
Done: make(chan struct{}),
SegmentsGenerationCount: 0,
SegmentsEndedCount: 0,
},
}
}
func (g *OtlpTracesGenerator) Generate(ctx context.Context) error {
tracer := otel.Tracer("tracer")
g.SegmentsGenerationCount++
_, span := tracer.Start(ctx, "example-span", trace.WithSpanKind(trace.SpanKindServer))
defer func() {
span.End()
g.SegmentsEndedCount++
}()

if len(g.Cfg.Annotations) > 0 {
span.SetAttributes(attribute.StringSlice(attributeKeyAwsXrayAnnotations, maps.Keys(g.Cfg.Annotations)))
}
span.SetAttributes(g.Cfg.Attributes...)
return nil
}

func (g *OtlpTracesGenerator) GetSegmentCount() (int, int) {
return g.SegmentsGenerationCount, g.SegmentsEndedCount
}

func (g *OtlpTracesGenerator) GetAgentConfigPath() string {
return g.AgentConfigPath
}
func (g *OtlpTracesGenerator) GetAgentRuntime() time.Duration {
return g.AgentRuntime
}
func (g *OtlpTracesGenerator) GetName() string {
return g.Name
}
func (g *OtlpTracesGenerator) GetGeneratorConfig() *common.TraceGeneratorConfig {
return g.Cfg
}

func setupClient(ctx context.Context) (*sdktrace.TracerProvider, func(context.Context) error, error) {
res := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(serviceName),
)

tp, err := setupTraceProvider(ctx, res)
if err != nil {
return nil, nil, err
}

otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(xray.Propagator{})

return tp, func(context.Context) (err error) {
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

err = tp.Shutdown(timeoutCtx)
if err != nil {
return err
}
return nil
}, nil
}

func setupTraceProvider(ctx context.Context, res *resource.Resource) (*sdktrace.TracerProvider, error) {
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure())
if err != nil {
return nil, err
}

return sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(res),
sdktrace.WithIDGenerator(xray.NewIDGenerator()),
), nil
}
8 changes: 8 additions & 0 deletions test/otlp/resources/otlp-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"traces": {
"traces_collected": {
"otlp": {
}
}
}
}
69 changes: 69 additions & 0 deletions test/otlp/trace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package otlp

import (
"path/filepath"
"testing"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/environment"
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
)

const (
agentRuntime = 5 * time.Minute
loadGeneratorInterval = 5 * time.Second
testSegmentCount = 20
)

func init() {
environment.RegisterEnvironmentMetaDataFlags()
}

func TestTraces(t *testing.T) {
env := environment.GetEnvironmentMetaData()
testCases := map[string]struct {
agentConfigPath string
generatorConfig *common.TraceGeneratorConfig
}{
"WithOTLP/Simple": {
agentConfigPath: filepath.Join("resources", "otlp-config.json"),
generatorConfig: &common.TraceGeneratorConfig{
Interval: loadGeneratorInterval,
Annotations: map[string]interface{}{
"test_type": "simple_otlp",
"instance_id": env.InstanceId,
"commit_sha": env.CwaCommitSha,
},
Metadata: map[string]map[string]interface{}{
"default": {
"custom_key": "custom_value",
},
},
Attributes: []attribute.KeyValue{
attribute.String("custom_key", "custom_value"),
attribute.String("test_type", "simple_otlp"),
attribute.String("instance_id", env.InstanceId),
attribute.String("commit_sha", env.CwaCommitSha),
},
},
},
}
t.Logf("Sanity check: number of test cases:%d", len(testCases))
for name, testCase := range testCases {

t.Run(name, func(t *testing.T) {

OtlpTestCfg := common.TraceTestConfig{
Generator: newLoadGenerator(testCase.generatorConfig),
Name: name,
AgentConfigPath: testCase.agentConfigPath,
AgentRuntime: agentRuntime,
}
err := common.TraceTest(t, OtlpTestCfg)
require.NoError(t, err, "TraceTest failed because %s", err)

})
}
}
Loading

0 comments on commit 11f3c4e

Please sign in to comment.