alibaba/spring-ai-alibaba · warning

failed to parse Long type: {}

Error message

failed to parse Long type: {}

What it means

TracingRepositoryImpl.getLong converts raw stored values (Number or String) into Long. When a String value cannot be parsed as a long, it logs 'failed to parse Long type' with the offending value and returns null so the caller (startTimeUs/endTimeUs/durationUs/convertSpanEvents) skips the field.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/repository/impl/TracingRepositoryImpl.java:404

            .build();
    }

    // 辅助方法
    private String getString(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value != null ? value.toString() : null;
    }

    private Long getLong(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value instanceof Number) {
            return ((Number) value).longValue();
        }
        if (value instanceof String) {
            try {
                return Long.parseLong((String) value);
            } catch (NumberFormatException e) {
                log.warn("failed to parse Long type: {}", value, e);
                return null;
            }
        }
        log.warn("failed to parse Long type: {}", value);
        return null;
    }

    @SuppressWarnings("unchecked")
    private List<SpanLinkDTO> convertSpanLinks(List<Map<String, Object>> links) {
        if (links == null) return new ArrayList<>();
        
        return links.stream()
            .map(link -> SpanLinkDTO.builder()
                .traceId(getString(link, "traceID"))
                .spanId(getString(link, "spanID"))
                .attributes((Map<String, Object>) link.get("attribute"))
                .build())
            .collect(Collectors.toList());

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Inspect the logged value in the warning to find the malformed span attribute
  2. Fix or clean the data at the source so numeric fields are stored as numbers or integer strings
  3. Extend getLong to handle Float/Double inputs (((Number)value).longValue()) and trim strings before parsing

Example fix

// before
if (value instanceof String) {
    try { return Long.parseLong((String) value); }
    catch (NumberFormatException e) { log.warn(...); return null; }
}
// after
if (value instanceof Number n) return n.longValue();
if (value instanceof String s) {
    try { return Long.parseLong(s.trim()); }
    catch (NumberFormatException e) {
        try { return (long) Double.parseDouble(s.trim()); }
        catch (NumberFormatException ex) { log.warn(...); return null; }
    }
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (value instanceof Number || (value instanceof String s && s.matches("-?\\d+"))) { /* safe to convert */ }

Type guard

Long asLong(Object v) {
    if (v instanceof Number n) return n.longValue();
    if (v instanceof String s) { try { return Long.parseLong(s.trim()); } catch (NumberFormatException e) { return null; } }
    return null;
}

Try / catch

Long ts = getLong(attr, "start_time");
if (ts == null) { log.warn("skipping span with unparseable start_time: {}", attr.get("start_time")); return; }

Prevention

When it happens

Trigger: A span attribute or field stored as a String that is not a numeric long (e.g. '12.5', 'abc', '1e9', empty string, or a value with units) is passed to Long.parseLong.

Common situations: Tracing backends storing duration as '12.5ms' or floats as strings; span events containing non-numeric attribute values that the converter assumes are timestamps/durations.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/bbfa2692ee91bc19. Report an issue: GitHub.