Skip to content
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

Cherry-pick Timestamp Validation Removal commits for XRay Exporter #98

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/xray-exporter-allow-random-trace-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: awsxrayexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "add `exporter.awsxray.skiptimestampvalidation` Alpha feature gate to remove xray timestamp restriction on first 32 bits of trace id"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26041]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
27 changes: 27 additions & 0 deletions .chloggen/xray-exporter-w3c-id-beta-feature-gate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: awsxrayexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Change `exporter.awsxray.skiptimestampvalidation` feature gate from Alpha to Beta"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26553]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
6 changes: 3 additions & 3 deletions exporter/awsxrayexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ propagated by them using the `X-Amzn-Trace-Id` HTTP header. However, other gener
supported by replacing fully-random Trace IDs with X-Ray formatted Trace IDs where necessary:

> AWS X-Ray IDs are the same size as W3C Trace Context IDs but differ in that the first 32 bits of a Trace ID
> is the Unix epoch time when the trace was started. Since X-Ray only allows submission of Trace IDs from the
> past 30 days, received Trace IDs are checked and spans without a valid timestamp are dropped.
> is the Unix epoch time when the trace was started. Note that X-Ray only allows submission of Trace IDs from
> the past 30 days, otherwise the trace is dropped by X-Ray. The Exporter will not validate this timestamp.

This means in order for spans to appear in X-Ray, the client SDK MUST use an X-Ray ID generator. For more
This means that until X-Ray supports Trace Ids consisting of fully random bits, in order for spans to appear in X-Ray, the client SDK MUST use an X-Ray ID generator. For more
information, see
[configuring the X-Ray exporter](https://aws-otel.github.io/docs/getting-started/x-ray#configuring-the-aws-x-ray-exporter).

Expand Down
3 changes: 2 additions & 1 deletion exporter/awsxrayexporter/awsxray.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func extractResourceSpans(config component.Config, logger *zap.Logger, td ptrace
spans.At(k), resource,
config.(*Config).IndexedAttributes,
config.(*Config).IndexAllAttributes,
config.(*Config).LogGroupNames)
config.(*Config).LogGroupNames,
config.(*Config).skipTimestampValidation)
if localErr != nil {
logger.Debug("Error translating span.", zap.Error(localErr))
continue
Expand Down
6 changes: 2 additions & 4 deletions exporter/awsxrayexporter/awsxray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ func TestXrayAndW3CSpanTraceExport(t *testing.T) {
func TestXrayAndW3CSpanTraceResourceExtraction(t *testing.T) {
td := constructXrayAndW3CSpanData()
logger, _ := zap.NewProduction()
assert.Len(t, extractResourceSpans(generateConfig(t), logger, td), 2, "2 spans have xay trace id")
assert.Len(t, extractResourceSpans(generateConfig(t), logger, td), 4, "4 spans have xray/w3c trace id")
}

func TestW3CSpanTraceResourceExtraction(t *testing.T) {
t.Skip("Flaky test, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/9255")
td := constructW3CSpanData()
logger, _ := zap.NewProduction()
assert.Len(t, extractResourceSpans(generateConfig(t), logger, td), 0, "0 spans have xray trace id")
assert.Len(t, extractResourceSpans(generateConfig(t), logger, td), 2, "2 spans have w3c trace id")
}

func TestTelemetryEnabled(t *testing.T) {
Expand Down Expand Up @@ -148,7 +147,6 @@ func constructSpanData() ptrace.Traces {
return traces
}

// nolint:unused
func constructW3CSpanData() ptrace.Traces {
resource := constructResource()
traces := ptrace.NewTraces()
Expand Down
3 changes: 3 additions & 0 deletions exporter/awsxrayexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ type Config struct {
LogGroupNames []string `mapstructure:"aws_log_groups"`
// TelemetryConfig contains the options for telemetry collection.
TelemetryConfig telemetry.Config `mapstructure:"telemetry,omitempty"`

// skipTimestampValidation if enabled, will skip timestamp validation logic on the trace ID
skipTimestampValidation bool
}
7 changes: 4 additions & 3 deletions exporter/awsxrayexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ func TestLoadConfig(t *testing.T) {
ResourceARN: "arn:aws:ec2:us-east1:123456789:instance/i-293hiuhe0u",
RoleARN: "arn:aws:iam::123456789:role/monitoring-EKS-NodeInstanceRole",
},
IndexedAttributes: []string{"indexed_attr_0", "indexed_attr_1"},
IndexAllAttributes: false,
LogGroupNames: []string{"group1", "group2"},
IndexedAttributes: []string{"indexed_attr_0", "indexed_attr_1"},
IndexAllAttributes: false,
LogGroupNames: []string{"group1", "group2"},
skipTimestampValidation: false,
},
},
}
Expand Down
9 changes: 8 additions & 1 deletion exporter/awsxrayexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/featuregate"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray/telemetry"
Expand All @@ -31,6 +32,11 @@ const (
stability = component.StabilityLevelBeta
)

var skipTimestampValidationFeatureGate = featuregate.GlobalRegistry().MustRegister(
"exporter.awsxray.skiptimestampvalidation",
featuregate.StageBeta,
featuregate.WithRegisterDescription("Remove XRay's timestamp validation on first 32 bits of trace ID"))

// NewFactory creates a factory for AWS-Xray exporter.
func NewFactory() exporter.Factory {
return exporter.NewFactory(
Expand All @@ -41,7 +47,8 @@ func NewFactory() exporter.Factory {

func createDefaultConfig() component.Config {
return &Config{
AWSSessionSettings: awsutil.CreateDefaultSessionConfig(),
AWSSessionSettings: awsutil.CreateDefaultSessionConfig(),
skipTimestampValidation: skipTimestampValidationFeatureGate.IsEnabled(),
}
}

Expand Down
30 changes: 30 additions & 0 deletions exporter/awsxrayexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/featuregate"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
)
Expand All @@ -45,10 +46,39 @@ func TestCreateDefaultConfig(t *testing.T) {
ResourceARN: "",
RoleARN: "",
},
skipTimestampValidation: true,
}, "failed to create default config")
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
}

func TestCreateDefaultConfigWithSkipTimestampValidation(t *testing.T) {
factory := NewFactory()

err := featuregate.GlobalRegistry().Set("exporter.awsxray.skiptimestampvalidation", true)
assert.NoError(t, err)

cfg := factory.CreateDefaultConfig()
assert.Equal(t, cfg, &Config{
AWSSessionSettings: awsutil.AWSSessionSettings{
NumberOfWorkers: 8,
Endpoint: "",
RequestTimeoutSeconds: 30,
MaxRetries: 2,
NoVerifySSL: false,
ProxyAddress: "",
Region: "",
LocalMode: false,
ResourceARN: "",
RoleARN: "",
},
skipTimestampValidation: true,
}, "failed to create default config")
assert.NoError(t, componenttest.CheckConfigStruct(cfg))

err = featuregate.GlobalRegistry().Set("exporter.awsxray.skiptimestampvalidation", false)
assert.NoError(t, err)
}

func TestCreateTracesExporter(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion exporter/awsxrayexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
go.opentelemetry.io/collector/confmap v0.77.0
go.opentelemetry.io/collector/consumer v0.77.0
go.opentelemetry.io/collector/exporter v0.77.0
go.opentelemetry.io/collector/featuregate v0.77.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0011
go.opentelemetry.io/collector/semconv v0.77.0
go.uber.org/zap v1.24.0
Expand All @@ -34,7 +35,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.77.0 // indirect
go.opentelemetry.io/collector/featuregate v0.77.0 // indirect
go.opentelemetry.io/collector/receiver v0.77.0 // indirect
go.opentelemetry.io/otel v1.15.1 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
Expand Down
29 changes: 16 additions & 13 deletions exporter/awsxrayexporter/internal/translator/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ var (
)

// MakeSegmentDocumentString converts an OpenTelemetry Span to an X-Ray Segment and then serialzies to JSON
func MakeSegmentDocumentString(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool, logGroupNames []string) (string, error) {
segment, err := MakeSegment(span, resource, indexedAttrs, indexAllAttrs, logGroupNames)
func MakeSegmentDocumentString(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool, logGroupNames []string, skipTimestampValidation bool) (string, error) {
segment, err := MakeSegment(span, resource, indexedAttrs, indexAllAttrs, logGroupNames, skipTimestampValidation)
if err != nil {
return "", err
}
Expand All @@ -91,7 +91,7 @@ func MakeSegmentDocumentString(span ptrace.Span, resource pcommon.Resource, inde
}

// MakeSegment converts an OpenTelemetry Span to an X-Ray Segment
func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool, logGroupNames []string) (*awsxray.Segment, error) {
func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool, logGroupNames []string, skipTimestampValidation bool) (*awsxray.Segment, error) {
var segmentType string

storeResource := true
Expand All @@ -103,7 +103,7 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str
}

// convert trace id
traceID, err := convertToAmazonTraceID(span.TraceID())
traceID, err := convertToAmazonTraceID(span.TraceID(), skipTimestampValidation)
if err != nil {
return nil, err
}
Expand All @@ -121,7 +121,7 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str
sqlfiltered, sql = makeSQL(span, awsfiltered)
additionalAttrs = addSpecialAttributes(sqlfiltered, indexedAttrs, attributes)
user, annotations, metadata = makeXRayAttributes(additionalAttrs, resource, storeResource, indexedAttrs, indexAllAttrs)
spanLinks, makeSpanLinkErr = makeSpanLinks(span.Links())
spanLinks, makeSpanLinkErr = makeSpanLinks(span.Links(), skipTimestampValidation)
name string
namespace string
)
Expand Down Expand Up @@ -309,7 +309,7 @@ func determineAwsOrigin(resource pcommon.Resource) string {
// - For example, 10:00AM December 2nd, 2016 PST in epoch time is 1480615200 seconds,
// or 58406520 in hexadecimal.
// - A 96-bit identifier for the trace, globally unique, in 24 hexadecimal digits.
func convertToAmazonTraceID(traceID pcommon.TraceID) (string, error) {
func convertToAmazonTraceID(traceID pcommon.TraceID, skipTimestampValidation bool) (string, error) {
const (
// maxAge of 28 days. AWS has a 30 day limit, let's be conservative rather than
// hit the limit
Expand All @@ -327,13 +327,16 @@ func convertToAmazonTraceID(traceID pcommon.TraceID) (string, error) {
b = [4]byte{}
)

// If AWS traceID originally came from AWS, no problem. However, if oc generated
// the traceID, then the epoch may be outside the accepted AWS range of within the
// past 30 days.
//
// In that case, we return invalid traceid error
if delta := epochNow - epoch; delta > maxAge || delta < -maxSkew {
return "", fmt.Errorf("invalid xray traceid: %s", traceID)
// If feature gate is enabled, skip the timestamp validation logic
if !skipTimestampValidation {
// If AWS traceID originally came from AWS, no problem. However, if oc generated
// the traceID, then the epoch may be outside the accepted AWS range of within the
// past 30 days.
//
// In that case, we return invalid traceid error
if delta := epochNow - epoch; delta > maxAge || delta < -maxSkew {
return "", fmt.Errorf("invalid xray traceid: %s", traceID)
}
}

binary.BigEndian.PutUint32(b[0:4], uint32(epoch))
Expand Down
Loading