When exporting observability data (metrics, traces, logs) to external systems, properly sanitize all data fields to ensure compatibility and consistency. Key requirements:
When exporting observability data (metrics, traces, logs) to external systems, properly sanitize all data fields to ensure compatibility and consistency. Key requirements:
Example:
def sanitize_metric_name(name: str) -> str:
# Handle leading digit
if name and name[0].isdigit():
name = "_" + name
# Replace invalid chars with underscore
return re.sub(r"[^a-zA-Z0-9_]", "_", name)
def sanitize_label_key(key: str) -> str:
# Remove special chars
return re.sub(r"[^a-zA-Z0-9]", "_", key)
def format_label_value(value: Any) -> str:
# Convert to string and escape
return str(value).replace("\\", "\\\\").replace("\n", "\\n")
# Usage
metric_name = sanitize_metric_name("2xx.success.rate") # "_2xx_success_rate"
label_key = sanitize_label_key("status.code") # "status_code"
label_value = format_label_value(200) # "200"
This ensures reliable data export and prevents issues with downstream observability systems while maintaining semantic meaning of the original data.
Enter the URL of a public GitHub repository