|
| 1 | +# Instructions for Building a Singer Tap/Target |
| 2 | + |
| 3 | +This document provides guidance for implementing a high-quality Singer Tap (or Target) in compliance with the Singer specification and community best practices. Use it in conjunction with GitHub Copilot or your preferred IDE. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Rate Limiting |
| 8 | + |
| 9 | +- Respect API rate limits (e.g., daily quotas or per-second limits). |
| 10 | +- For short-term rate limits, detect HTTP 429 or similar errors and implement retries with sleep/delay. |
| 11 | +- Use Singer’s built-in rate-limiting utilities where available. |
| 12 | + |
| 13 | + |
| 14 | +## 2. Memory Efficiency |
| 15 | + |
| 16 | +- Minimize RAM usage by streaming data. |
| 17 | +Example: Use generators or iterators instead of loading entire datasets into memory. |
| 18 | + |
| 19 | + |
| 20 | +## 3. Consistent Date Handling |
| 21 | + |
| 22 | +- Use RFC 3339 format (including time zone offset). UTC (Z) is preferred. |
| 23 | +Examples: |
| 24 | +Good: 2017-01-01T00:00:00Z, 2017-01-01T00:00:00-05:00 |
| 25 | +Bad: 2017-01-01 00:00:00 |
| 26 | +Use pytz for timezone-aware conversions. |
| 27 | + |
| 28 | + |
| 29 | +## 4. Logging & Exception Handling |
| 30 | + |
| 31 | +- Log every API request (URL + parameters), omitting sensitive info (e.g., API keys). |
| 32 | +- Log progress updates (e.g., “Starting stream X”). |
| 33 | +- On API errors, log status code and response body. |
| 34 | + |
| 35 | +For fatal errors: |
| 36 | +- Log at CRITICAL or FATAL level. |
| 37 | +- Exit with non-zero status. |
| 38 | +- Omit stack trace for known, user-triggered conditions. |
| 39 | +- Include full trace for unexpected exceptions. |
| 40 | +- For recoverable errors, implement retries with exponential backoff (e.g., using the backoff library). |
| 41 | + |
| 42 | + |
| 43 | +## 5. Module Structure |
| 44 | + |
| 45 | +- Organize code into a proper Python module (directory with __init__.py), not a single script file. |
| 46 | + |
| 47 | + |
| 48 | +## 6. Schema Management |
| 49 | + |
| 50 | +- For static schemas, store them as .json files in a schemas/ directory—not as inline Python dicts. |
| 51 | +Prefer explicit schemas: |
| 52 | +- Avoid additionalProperties: true or vague typing. |
| 53 | +- Use clear field names and types. |
| 54 | +- Set additionalProperties: false when schemas must be strict. |
| 55 | +- Be cautious when tightening schemas in new versions—it may require a major version bump per semantic versioning. |
| 56 | + |
| 57 | + |
| 58 | +## 7. JSON Schema Guidelines |
| 59 | + |
| 60 | +- All files under schemas/*.json must follow the JSON Schema standard. |
| 61 | +- Any fields named created_time, modified_time, ending in _time or ending in _date must use the date-time format. |
| 62 | +- Avoid using additionalProperties at the root level. It's allowed in nested fields only. |
| 63 | + |
| 64 | +Example: |
| 65 | +{ |
| 66 | + "type": "object", |
| 67 | + "properties": { |
| 68 | + "created_time": { |
| 69 | + "type": ["null", "string"], |
| 70 | + "format": "date-time" |
| 71 | + }, |
| 72 | + "last_access_time": { |
| 73 | + "type": ["null", "string"], |
| 74 | + "format": "date-time" |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +## 8. Validating Bookmarking |
| 81 | + |
| 82 | +We use the singer.bookmarks module to read from and write to the bookmark state file. |
| 83 | +To ensure correctness, always validate the structure of the bookmark state before processing or committing any changes. |
| 84 | +- In abstract.py, we use get_bookmark() and write_bookmark() to manage bookmarks for streams. |
| 85 | +- The write_bookmark() function overrides the one from the singer module to apply custom behavior. |
| 86 | +- Always confirm that the state structure matches the expected format before writing. |
| 87 | + |
| 88 | +Format Example: |
| 89 | +{ |
| 90 | + "bookmarks": { |
| 91 | + "stream_name": { |
| 92 | + "replication_key": "2024-01-01T00:00:00Z" |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +Optional validation function: |
| 99 | +def is_valid_bookmark_state(state): |
| 100 | + return isinstance(state, dict) and \ |
| 101 | + "bookmarks" in state and \ |
| 102 | + isinstance(state["bookmarks"], dict) |
| 103 | + |
| 104 | + |
| 105 | +## 9. Code Quality |
| 106 | + |
| 107 | +- Use pylint and aim for zero error-level messages. |
| 108 | +- CI pipelines (e.g., CircleCI) should enforce linting. |
| 109 | +- Fix or explicitly disable warnings when appropriate. |
0 commit comments