Sentry Phil Niedertscheider Feb 9, 2026

Building Type-Safe Metrics API in Swift Part II

Article Summary

Phil Niedertscheider from Sentry reveals how Swift's type system can catch metric API bugs before they compile. No more sending garbage data like memory addresses to production.

This is part II of Sentry's deep dive into building a type-safe Metrics API for their Swift SDK. The team tackled a critical problem: their logging API accepted Any types for attributes, allowing developers to accidentally pass invalid data that serialized into useless strings with memory addresses.

Key Takeaways

Critical Insight

Sentry's new Swift Metrics API uses protocols and enums to guarantee type safety at compile time, eliminating an entire class of runtime data quality bugs.

The article reveals a surprising compiler limitation with array conformance that forced the team to completely rethink their generic constraints approach.

About This Article

Problem

Sentry's initial Metrics API used type-erased Any for attribute values. This let developers pass unsupported types like custom classes, which would serialize to useless memory addresses such as $103d12130. The resulting data was non-deterministic and couldn't be queried.

Solution

Phil Niedertscheider's team built the SentryAttributeContent enum with associated values and the SentryAttributeValue protocol. This allows extensions on standard library types like String, Bool, Int, Double, and Float to automatically convert to supported attribute types.

Impact

The new API in sentry-cocoa v9.4.0 validates attribute values at compile time. This stops an entire class of runtime serialization bugs where invalid data used to ship to production without anyone noticing.