-
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.
Multi-System Trace Integration Test (#318)
Co-authored-by: Jeffrey Chien <[email protected]>
- Loading branch information
Showing
14 changed files
with
1,126 additions
and
25 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
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 | ||
} |
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,8 @@ | ||
{ | ||
"traces": { | ||
"traces_collected": { | ||
"otlp": { | ||
} | ||
} | ||
} | ||
} |
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 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) | ||
|
||
}) | ||
} | ||
} |
Oops, something went wrong.