Skip to content

Commit

Permalink
Truncate derived transaction names
Browse files Browse the repository at this point in the history
  • Loading branch information
swi-jared committed Jun 17, 2024
1 parent c7b7d7e commit 78316fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
56 changes: 28 additions & 28 deletions internal/txn/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,37 @@ func GetTransactionName(span sdktrace.ReadOnlySpan) string {
}

// deriveTransactionName returns transaction name from given span name and attributes, falling back to "unknown"
func deriveTransactionName(name string, attrs []attribute.KeyValue) (txnName string) {
if txnName = config.GetTransactionName(); txnName != "" {
return
}

var httpRoute, httpUrl = "", ""
for _, attr := range attrs {
if attr.Key == semconv.HTTPRouteKey {
httpRoute = attr.Value.AsString()
} else if attr.Key == semconv.HTTPURLKey {
httpUrl = attr.Value.AsString()
func deriveTransactionName(name string, attrs []attribute.KeyValue) string {
txnName := config.GetTransactionName()
if txnName == "" {
var httpRoute, httpUrl = "", ""
for _, attr := range attrs {
if attr.Key == semconv.HTTPRouteKey {
httpRoute = attr.Value.AsString()
} else if attr.Key == semconv.HTTPURLKey {
httpUrl = attr.Value.AsString()
}
}
}

if httpRoute != "" {
txnName = httpRoute
} else if name != "" {
txnName = name
}
if httpUrl != "" && strings.TrimSpace(txnName) == "" {
parsed, err := url.Parse(httpUrl)
if err != nil {
// We can't import internal logger in the util package, so we default to "log". However, this should be
// infrequent.
log.Println("could not parse URL from span", httpUrl)
} else {
// Clear user/password
parsed.User = nil
txnName = parsed.String()
if httpRoute != "" {
txnName = httpRoute
} else if name != "" {
txnName = name
}
if httpUrl != "" && strings.TrimSpace(txnName) == "" {
parsed, err := url.Parse(httpUrl)
if err != nil {
// We can't import internal logger in the util package, so we default to "log". However, this should be
// infrequent.
log.Println("could not parse URL from span", httpUrl)
} else {
// Clear user/password
parsed.User = nil
txnName = parsed.String()
}
}
}

txnName = strings.TrimSpace(txnName)
if txnName == "" {
txnName = "unknown"
Expand All @@ -74,5 +74,5 @@ func deriveTransactionName(name string, attrs []attribute.KeyValue) (txnName str
if len(txnName) > 255 {
txnName = txnName[:255]
}
return
return txnName
}
17 changes: 17 additions & 0 deletions internal/txn/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,20 @@ func TestDeriveTxnFromEnv(t *testing.T) {
require.Equal(t, envTxn, config.GetTransactionName())
require.Equal(t, envTxn, deriveTransactionName(name, attrs))
}
func TestDeriveTxnFromEnvTruncated(t *testing.T) {
envTxn := strings.Repeat("a", 1024)
expected := strings.Repeat("a", 255)
name := "span name"
var attrs []attribute.KeyValue
require.NoError(t, os.Setenv("SW_APM_TRANSACTION_NAME", envTxn))
require.NoError(t, os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "foo"))
require.NoError(t, os.Setenv("LAMBDA_TASK_ROOT", "bar"))
defer func() {
_ = os.Unsetenv("SW_APM_TRANSACTION_NAME")
_ = os.Unsetenv("AWS_LAMBDA_FUNCTION_NAME")
_ = os.Unsetenv("LAMBDA_TASK_ROOT")
}()
config.Load()
require.Equal(t, envTxn, config.GetTransactionName())
require.Equal(t, expected, deriveTransactionName(name, attrs))
}

0 comments on commit 78316fa

Please sign in to comment.