Skip to content

fix(logs): parse compute string for aggregate metrics#40

Merged
platinummonkey merged 3 commits into
mainfrom
fix/logs-aggregate-compute-parsing
Feb 11, 2026
Merged

fix(logs): parse compute string for aggregate metrics#40
platinummonkey merged 3 commits into
mainfrom
fix/logs-aggregate-compute-parsing

Conversation

@platinummonkey
Copy link
Copy Markdown
Collaborator

Summary

Fixes #35

The pup logs aggregate --compute flag now properly parses metric-based aggregation functions like avg(@duration), sum(@bytes), etc. Previously, the entire compute string was passed as-is to the aggregation function field, causing 400 errors from the Datadog API.

Changes

Core Fix (cmd/logs_simple.go:641-690)

  • Added parseComputeString() helper to extract aggregation function and metric from compute strings
  • Supports formats: "count", "avg(@duration)", "percentile(@duration, 99)"
  • Validates aggregation functions against API-supported list
  • Handles case-insensitive input and whitespace

Updated runLogsAggregate (cmd/logs_simple.go:954-1035)

  • Parse compute string before building API request
  • Properly set aggregation function and metric fields
  • Enhanced error messages with detailed request info and troubleshooting tips

Test Coverage (cmd/logs_simple_test.go:84-228)

  • Added 16 test cases covering all compute string formats
  • Tests for valid functions: count, avg, sum, min, max, cardinality, median, percentile
  • Tests for error cases: invalid functions, malformed strings, empty input
  • Tests for edge cases: whitespace, case sensitivity, complex metric names

Examples

Now working:

# Average duration by service
pup logs aggregate --query="*" --from="1h" --compute="avg(@duration)" --group-by="service"

# Unique user count
pup logs aggregate --query="service:web" --from="4h" --compute="cardinality(@user.id)"

# 99th percentile latency
pup logs aggregate --query="*" --from="2h" --compute="percentile(@latency, 99)"

# Sum of bytes by host
pup logs aggregate --query="*" --from="1d" --compute="sum(@bytes)" --group-by="host"

Testing

All tests pass with race detection:

go test -race ./cmd/...

Specific test for this fix:

go test -v -run TestParseComputeString ./cmd/

Error Message Improvements

Enhanced error messages now include:

  • Parsed compute details (aggregation + metric)
  • Full request parameters
  • Formatted timestamps
  • Troubleshooting tips
  • Supported function list

🤖 Generated with Claude Code

Fixes #35

The `pup logs aggregate --compute` flag now properly parses metric-based
aggregation functions like `avg(@duration)`, `sum(@bytes)`, etc.

Previously, the entire compute string was passed as-is to the aggregation
function field, causing 400 errors from the API. The metric field was
also hardcoded to "*" instead of being extracted from the compute string.

Changes:
- Add parseComputeString() to extract aggregation function and metric
- Support formats: "count", "avg(@duration)", "percentile(@duration, 99)"
- Validate aggregation functions against supported list
- Improve error messages with detailed request info and troubleshooting
- Add comprehensive test coverage for all compute string formats

The fix enables proper usage:
  pup logs aggregate --query="*" --from="1h" --compute="avg(@duration)" --group-by="service"
  pup logs aggregate --query="*" --from="1h" --compute="cardinality(@user.id)"
  pup logs aggregate --query="*" --from="1h" --compute="percentile(@Latency, 99)"

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@platinummonkey platinummonkey requested a review from a team as a code owner February 11, 2026 15:00
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 11, 2026

📊 Test Coverage Report

Overall Coverage: 80.4% Coverage

Threshold: 80% ✅

Coverage by Package
## Coverage by Package

- github.com/DataDog/pup/pkg/auth/callback/server.go:40: 81.2%
- github.com/DataDog/pup/pkg/auth/dcr/client.go:28: 100.0%
- github.com/DataDog/pup/pkg/auth/dcr/types.go:24: 100.0%
- github.com/DataDog/pup/pkg/auth/oauth/client.go:22: 100.0%
- github.com/DataDog/pup/pkg/auth/oauth/pkce.go:24: 85.7%
- github.com/DataDog/pup/pkg/auth/storage/factory.go:53: 94.7%
- github.com/DataDog/pup/pkg/auth/storage/keychain.go:44: 42.9%
- github.com/DataDog/pup/pkg/auth/storage/storage.go:58: 71.4%
- github.com/DataDog/pup/pkg/auth/types/types.go:23: 100.0%
- github.com/DataDog/pup/pkg/client/client.go:32: 94.4%
- github.com/DataDog/pup/pkg/config/config.go:22: 100.0%
- github.com/DataDog/pup/pkg/formatter/formatter.go:31: 100.0%
- github.com/DataDog/pup/pkg/useragent/useragent.go:32: 100.0%
- github.com/DataDog/pup/pkg/util/time.go:20: 95.8%

## Summary

total:								(statements)		80.4%

📈 Coverage Status: ✅ PASSED - Coverage meets minimum threshold

Updated for commit b96c9bb

platinummonkey and others added 2 commits February 11, 2026 09:17
The Datadog API expects percentile aggregations in "pcNN" format (e.g., pc99,
pc95, pc50) not "percentile". Updated parseComputeString to automatically
convert user-friendly "percentile(@field, NN)" syntax to the API's "pcNN"
format.

Changes:
- Convert percentile(@Latency, 99) -> aggregation="pc99", metric="@Latency"
- Add percentileValue extraction from regex
- Validate that percentile has a value parameter
- Update error messages to show supported percentile formats
- Add tests for pc99, pc95, pc50 conversions
- Add test for missing percentile value error

Examples now working:
  pup logs aggregate --compute="percentile(@Latency, 99)"  # -> pc99
  pup logs aggregate --compute="percentile(@duration, 95)" # -> pc95
  pup logs aggregate --compute="percentile(@response_time, 50)" # -> pc50

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Changed timestamp handling to use UTC consistently throughout logs commands.
This ensures timestamps are displayed and processed in UTC regardless of
the local system timezone.

Changes:
- parseTimeString now uses time.Now().UTC() for relative time calculations
- Error messages display timestamps in UTC with "UTC" label
- Both runLogsSearch and runLogsAggregate use UTC formatting

Before: From: 2026-02-11T08:20:07-06:00 (CST)
After:  From: 2026-02-11T14:20:07Z UTC

This makes timestamps consistent and timezone-independent for API operations.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@platinummonkey platinummonkey merged commit b975955 into main Feb 11, 2026
4 checks passed
@platinummonkey platinummonkey deleted the fix/logs-aggregate-compute-parsing branch February 11, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] logs aggregate: --compute with metric fails (e.g. avg(@duration))

1 participant