Skip to content

Commit 0949d05

Browse files
committed
cleanup
1 parent 4969e6e commit 0949d05

File tree

1 file changed

+24
-90
lines changed

1 file changed

+24
-90
lines changed

libraries/src/AWS.Lambda.Powertools.Tracing/Internal/XRayRecorder.cs

Lines changed: 24 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private static bool IsSerializationError(Exception e)
170170
{
171171
if (e == null) return false;
172172

173-
var message = e.Message ?? string.Empty;
173+
var message = e.Message;
174174
var stackTrace = e.StackTrace ?? string.Empty;
175175
var typeName = e.GetType().Name;
176176

@@ -344,7 +344,7 @@ private static object SanitizeValueForMetadata(object value)
344344
{
345345
// If sanitization fails, return a safe string representation
346346
// This ensures we don't break the tracing functionality
347-
return $"[Sanitization failed: {ex.Message}] {value.ToString()}";
347+
return $"[Sanitization failed: {ex.Message}] {value}";
348348
}
349349
}
350350

@@ -493,23 +493,6 @@ private static bool IsKnownSafeArrayType(Type type)
493493
type == typeof(decimal[]);
494494
}
495495

496-
/// <summary>
497-
/// Checks if all elements in an array are safe types.
498-
/// </summary>
499-
/// <param name="array">The array to check</param>
500-
/// <returns>True if all elements are safe</returns>
501-
private static bool IsArrayElementsSafe(Array array)
502-
{
503-
for (int i = 0; i < array.Length; i++)
504-
{
505-
var element = array.GetValue(i);
506-
if (element != null && NeedsTypeSanitization(element.GetType()))
507-
return false;
508-
}
509-
510-
return true;
511-
}
512-
513496
/// <summary>
514497
/// Sanitizes dictionary values.
515498
/// </summary>
@@ -521,7 +504,7 @@ private static object SanitizeDictionary(System.Collections.IDictionary dict, in
521504
var sanitizedDict = new System.Collections.Generic.Dictionary<string, object>();
522505
foreach (System.Collections.DictionaryEntry entry in dict)
523506
{
524-
var key = entry.Key?.ToString() ?? "null";
507+
var key = entry.Key.ToString() ?? "null";
525508
sanitizedDict[key] = SanitizeValueRecursive(entry.Value, depth + 1);
526509
}
527510

@@ -556,7 +539,7 @@ private static object SanitizeComplexObject(object value, Type type)
556539
try
557540
{
558541
// This ensures the object can be serialized without issues
559-
return $"[{type.Name}] {value.ToString()}";
542+
return $"[{type.Name}] {value}";
560543
}
561544
catch (Exception ex)
562545
{
@@ -566,64 +549,12 @@ private static object SanitizeComplexObject(object value, Type type)
566549
}
567550

568551

569-
570-
/// <summary>
571-
/// Determines if a type is safe for X-Ray without sanitization
572-
/// </summary>
573-
/// <param name="type">The type to check</param>
574-
/// <returns>True if the type is safe</returns>
575-
private static bool IsSafeType(Type type)
576-
{
577-
return type == typeof(string) ||
578-
type == typeof(int) ||
579-
type == typeof(long) ||
580-
type == typeof(double) ||
581-
type == typeof(float) ||
582-
type == typeof(bool) ||
583-
type == typeof(decimal);
584-
}
585-
586-
/// <summary>
587-
/// Checks if a type needs sanitization due to potential JSON serialization issues.
588-
/// </summary>
589-
/// <param name="type">The type to check</param>
590-
/// <returns>True if the type needs sanitization</returns>
591-
private static bool NeedsTypeSanitization(Type type)
592-
{
593-
// Problematic primitive types that cause LitJson issues
594-
if (type == typeof(IntPtr) || type == typeof(UIntPtr) ||
595-
type == typeof(uint) || type == typeof(ulong) ||
596-
type == typeof(ushort) || type == typeof(byte) || type == typeof(sbyte))
597-
return true;
598-
599-
// Other problematic types
600-
if (type == typeof(DateTime) || type == typeof(TimeSpan) ||
601-
type == typeof(Guid) || type.IsEnum)
602-
return true;
603-
604-
// Check for specific nullable types without reflection
605-
if (IsKnownNullableProblematicType(type))
606-
return true;
607-
608-
return false;
609-
}
610-
611-
/// <summary>
612-
/// Checks for known nullable problematic types without using reflection
613-
/// </summary>
614-
private static bool IsKnownNullableProblematicType(Type type)
615-
{
616-
return type == typeof(IntPtr?) || type == typeof(UIntPtr?) ||
617-
type == typeof(uint?) || type == typeof(ulong?) ||
618-
type == typeof(ushort?) || type == typeof(byte?) || type == typeof(sbyte?) ||
619-
type == typeof(DateTime?) || type == typeof(TimeSpan?) ||
620-
type == typeof(Guid?);
621-
}
622-
623552
/// <summary>
624553
/// Safely sanitizes the current entity to prevent JSON serialization errors.
625554
/// </summary>
626-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Entity properties are preserved by X-Ray SDK")]
555+
[UnconditionalSuppressMessage("Trimming",
556+
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
557+
Justification = "Entity properties are preserved by X-Ray SDK")]
627558
private void SanitizeCurrentEntitySafely()
628559
{
629560
try
@@ -636,17 +567,18 @@ private void SanitizeCurrentEntitySafely()
636567
SanitizeEntityAnnotations(entity);
637568
SanitizeEntityHttpInformation(entity);
638569
}
639-
catch (Exception ex)
570+
catch
640571
{
641-
// Log the error but don't break tracing
642-
Console.WriteLine($"Warning: Entity sanitization failed: {ex.Message}");
572+
// ignored
643573
}
644574
}
645575

646576
/// <summary>
647577
/// Sanitizes the metadata in an entity
648578
/// </summary>
649-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Entity.Metadata property is preserved by X-Ray SDK")]
579+
[UnconditionalSuppressMessage("Trimming",
580+
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
581+
Justification = "Entity.Metadata property is preserved by X-Ray SDK")]
650582
private static void SanitizeEntityMetadata(Entity entity)
651583
{
652584
try
@@ -682,16 +614,18 @@ private static void SanitizeEntityMetadata(Entity entity)
682614
}
683615
}
684616
}
685-
catch (Exception ex)
617+
catch
686618
{
687-
Console.WriteLine($"Warning: Metadata sanitization failed: {ex.Message}");
619+
// ignored
688620
}
689621
}
690622

691623
/// <summary>
692624
/// Sanitizes the annotations in an entity
693625
/// </summary>
694-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Entity.Annotations property is preserved by X-Ray SDK")]
626+
[UnconditionalSuppressMessage("Trimming",
627+
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
628+
Justification = "Entity.Annotations property is preserved by X-Ray SDK")]
695629
private static void SanitizeEntityAnnotations(Entity entity)
696630
{
697631
try
@@ -712,16 +646,18 @@ private static void SanitizeEntityAnnotations(Entity entity)
712646
annotations[key] = sanitizedValue;
713647
}
714648
}
715-
catch (Exception ex)
649+
catch
716650
{
717-
Console.WriteLine($"Warning: Annotations sanitization failed: {ex.Message}");
651+
// ignored
718652
}
719653
}
720654

721655
/// <summary>
722656
/// Sanitizes HTTP information in an entity
723657
/// </summary>
724-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Entity.Http property is preserved by X-Ray SDK")]
658+
[UnconditionalSuppressMessage("Trimming",
659+
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
660+
Justification = "Entity.Http property is preserved by X-Ray SDK")]
725661
private static void SanitizeEntityHttpInformation(Entity entity)
726662
{
727663
try
@@ -742,11 +678,9 @@ private static void SanitizeEntityHttpInformation(Entity entity)
742678
http[key] = sanitizedValue;
743679
}
744680
}
745-
catch (Exception ex)
681+
catch
746682
{
747-
Console.WriteLine($"Warning: HTTP information sanitization failed: {ex.Message}");
683+
// ignored
748684
}
749685
}
750-
751-
752686
}

0 commit comments

Comments
 (0)