()
| 1768 | const DEFAULT_OTEL_HEADERS_DEBOUNCE_MS = 29 * 60 * 1000 // 29 minutes |
| 1769 | |
| 1770 | export function getOtelHeadersFromHelper(): Record<string, string> { |
| 1771 | const otelHeadersHelper = getConfiguredOtelHeadersHelper() |
| 1772 | |
| 1773 | if (!otelHeadersHelper) { |
| 1774 | return {} |
| 1775 | } |
| 1776 | |
| 1777 | // Return cached headers if still valid (debounce) |
| 1778 | const debounceMs = parseInt( |
| 1779 | process.env.CLAUDE_CODE_OTEL_HEADERS_HELPER_DEBOUNCE_MS || |
| 1780 | DEFAULT_OTEL_HEADERS_DEBOUNCE_MS.toString(), |
| 1781 | ) |
| 1782 | if ( |
| 1783 | cachedOtelHeaders && |
| 1784 | Date.now() - cachedOtelHeadersTimestamp < debounceMs |
| 1785 | ) { |
| 1786 | return cachedOtelHeaders |
| 1787 | } |
| 1788 | |
| 1789 | if (isOtelHeadersHelperFromProjectOrLocalSettings()) { |
| 1790 | // Check if trust has been established for this project |
| 1791 | const hasTrust = checkHasTrustDialogAccepted() |
| 1792 | if (!hasTrust) { |
| 1793 | return {} |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | try { |
| 1798 | const result = execSyncWithDefaults_DEPRECATED(otelHeadersHelper, { |
| 1799 | timeout: 30000, // 30 seconds - allows for auth service latency |
| 1800 | }) |
| 1801 | ?.toString() |
| 1802 | .trim() |
| 1803 | if (!result) { |
| 1804 | throw new Error('otelHeadersHelper did not return a valid value') |
| 1805 | } |
| 1806 | |
| 1807 | const headers = jsonParse(result) |
| 1808 | if ( |
| 1809 | typeof headers !== 'object' || |
| 1810 | headers === null || |
| 1811 | Array.isArray(headers) |
| 1812 | ) { |
| 1813 | throw new Error( |
| 1814 | 'otelHeadersHelper must return a JSON object with string key-value pairs', |
| 1815 | ) |
| 1816 | } |
| 1817 | |
| 1818 | // Validate all values are strings |
| 1819 | for (const [key, value] of Object.entries(headers)) { |
| 1820 | if (typeof value !== 'string') { |
| 1821 | throw new Error( |
| 1822 | `otelHeadersHelper returned non-string value for key "${key}": ${typeof value}`, |
| 1823 | ) |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | // Cache the result |
no test coverage detected