Skip to content

Feature/api region configuration#22

Open
mueller-thomas-vse wants to merge 2 commits intocloudbring:masterfrom
mueller-thomas-vse:feature/api-region-configuration
Open

Feature/api region configuration#22
mueller-thomas-vse wants to merge 2 commits intocloudbring:masterfrom
mueller-thomas-vse:feature/api-region-configuration

Conversation

@mueller-thomas-vse
Copy link
Copy Markdown

@mueller-thomas-vse mueller-thomas-vse commented Oct 30, 2025

Pull Request: Add Configurable New Relic API Region Support

Branch: feature/api-region-configuration

Description

This PR adds support for configurable New Relic API regions via the NEW_RELIC_API_REGION environment variable. Users can now select between US and EU API endpoints.

Changes

Features:

  • Add NEW_RELIC_API_REGION environment variable support
  • Support for US region (default): https://api.newrelic.com/graphql
  • Support for EU region: https://api.eu.newrelic.com/graphql
  • Case-insensitive region handling
  • Default fallback to US region for invalid values

📝 Documentation:

  • Updated .env.example with new configuration option
  • Updated README.md with region configuration details
  • Added comprehensive inline comments

🧪 Testing:

  • Added comprehensive test suite (api-region.test.ts) with 8 test cases:
    • Default behavior (no region specified)
    • Explicit US region selection
    • Explicit EU region selection
    • Case-insensitive region values
    • Invalid region fallback
    • Region-specific NRQL queries
    • Region-specific APM listing
    • Region persistence across multiple calls

Test Results

All Tests Passing:

  • Test Files: 34 passed | 2 skipped (36 total)
  • Tests: 158 passed | 4 skipped (162 total)
  • Coverage: >90%
  • Duration: ~8.5s

Code Quality

Code Style Checks:

  • Follows TypeScript strict mode
  • Uses Biome for linting and formatting
  • Conventional commit messages
  • No breaking changes

Files Changed

  1. src/client/newrelic-client.ts

    • Replaced hardcoded NERDGRAPH_URL with configurable endpoint selection
    • Added getNerdGraphUrl() function for dynamic URL resolution
    • Added nerdGraphUrl property to NewRelicClient class
    • Updated constructor to initialize URL based on environment variable
  2. .env.example

    • Added NEW_RELIC_API_REGION documentation
    • Example values: us, eu
    • Clear endpoint descriptions
  3. README.md

    • Added new "Optional Environment Variables" section
    • Documented NEW_RELIC_API_REGION with region-to-endpoint mapping
  4. test/client/newrelic-client/api-region.test.ts (NEW)

    • Comprehensive test suite for region functionality
    • Tests default behavior, explicit selections, edge cases

How to Use

Set US Region (default):

export NEW_RELIC_API_REGION=us
# or omit for default

Set EU Region:

export NEW_RELIC_API_REGION=eu

In Configuration File:

{
  "env": {
    "NEW_RELIC_API_KEY": "your-api-key",
    "NEW_RELIC_ACCOUNT_ID": "your-account-id",
    "NEW_RELIC_API_REGION": "eu"
  }
}

Backward Compatibility

✅ Fully backward compatible:

  • Default behavior unchanged (US region)
  • Existing configurations continue to work
  • No breaking changes to public API

Follows Contribution Guidelines

✅ TDD approach with comprehensive tests
✅ >90% test coverage maintained
✅ Code formatted and linted
✅ Documentation updated
✅ Conventional commits
✅ All automated checks passing

Summary by CodeRabbit

  • New Features

    • Added configurable region selection (US or EU) for New Relic API access via the NEW_RELIC_API_REGION environment variable, defaulting to US region.
  • Documentation

    • Updated configuration documentation to describe the new optional region variable and corresponding GraphQL endpoints for each region.
  • Tests

    • Added comprehensive test coverage for region-specific API endpoint configurations.

- Add NEW_RELIC_API_REGION environment variable (us/eu)
- Support both US (default) and EU API endpoints
- US endpoint: https://api.newrelic.com/graphql
- EU endpoint: https://api.eu.newrelic.com/graphql
- Updated .env.example with new configuration option
- Added comprehensive test suite with 8 test cases
- All 162 tests passing with >90% coverage
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 30, 2025

Walkthrough

This pull request introduces New Relic API region configuration support. A new environment variable NEW_RELIC_API_REGION allows switching between US and EU endpoints. Changes include configuration updates, client-side endpoint selection logic, comprehensive documentation, and new test coverage validating region-specific behavior.

Changes

Cohort / File(s) Change Summary
Configuration
\\.env.example
Added NEW_RELIC_API_REGION environment variable with default value "us" and documentation for valid values ("us" or "eu") with corresponding GraphQL endpoint URLs.
Documentation
README.md
Added "Optional Environment Variables" subsection documenting NEW_RELIC_API_REGION with region options: US (https://api.newrelic.com/graphql) and EU (https://api.eu.newrelic.com/graphql).
Client Implementation
src/client/newrelic-client.ts
Replaced hard-coded NerdGraph URL with region-aware configuration. Introduced NERDGRAPH_URLS mapping, getNerdGraphUrl() helper function, and nerdGraphUrl instance field initialized based on NEW_RELIC_API_REGION environment variable (defaults to US). Updated fetch calls to use dynamic endpoint.
Test Coverage
test/client/newrelic-client/api-region.test.ts
Added comprehensive test suite validating region-based endpoint selection, case-insensitive region handling, fallback to US for unknown regions, NRQL queries and APM application listing across both regions, and consistent region usage across multiple API calls.

Sequence Diagram

sequenceDiagram
    participant User
    participant NewRelicClient as NewRelicClient
    participant Env as Environment
    participant NerdGraph as NerdGraph API

    User->>NewRelicClient: new NewRelicClient()
    NewRelicClient->>Env: read NEW_RELIC_API_REGION
    
    alt Region = "eu"
        Env-->>NewRelicClient: "eu"
        NewRelicClient->>NewRelicClient: set nerdGraphUrl = eu endpoint
    else Region = "us" or default
        Env-->>NewRelicClient: "us" or undefined
        NewRelicClient->>NewRelicClient: set nerdGraphUrl = us endpoint
    end

    User->>NewRelicClient: executeQuery()
    NewRelicClient->>NerdGraph: fetch(this.nerdGraphUrl, ...)
    NerdGraph-->>NewRelicClient: response
    NewRelicClient-->>User: result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Logic changes distributed across client initialization and fetch calls require careful verification of endpoint selection and fallback behavior
  • New test suite with multiple region-specific test cases adds confidence but needs thorough validation of mock setup and assertions
  • Environment variable integration and case-insensitivity handling should be verified against actual runtime behavior

Poem

🐰 A whisker-twitch of configuration,
Two endpoints dance across the nation—
US and EU, side by side,
One region chosen as our guide!
New Relic routes now region-aware,
Our API hops with regional flair! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "Feature/api region configuration" directly and clearly relates to the main change in the changeset. The PR's primary objective is to add support for configurable New Relic API regions via the NEW_RELIC_API_REGION environment variable, allowing selection between US and EU endpoints. The title accurately captures this feature addition and would allow a developer scanning the repository history to quickly understand that this PR introduces regional configuration capabilities. The title is concise, avoids vague terminology, and is specific enough to convey the core purpose of the changes.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/client/newrelic-client.ts (1)

9-15: Consider logging a warning for invalid region values.

The implementation correctly defaults to the US endpoint for invalid region values, which provides a safe fallback. However, this silent fallback could make debugging configuration issues difficult.

Consider adding a warning log when an invalid region is provided:

 function getNerdGraphUrl(): string {
   const region = (process.env.NEW_RELIC_API_REGION || 'us').toLowerCase();
   if (region === 'eu') {
     return NERDGRAPH_URLS.eu;
   }
+  if (region !== 'us' && process.env.NEW_RELIC_API_REGION) {
+    console.warn(`Invalid NEW_RELIC_API_REGION "${process.env.NEW_RELIC_API_REGION}". Defaulting to US region.`);
+  }
   return NERDGRAPH_URLS.us;
 }

Note: This is optional and depends on your logging strategy. The current behavior is acceptable.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59e067b and 8a56cf5.

📒 Files selected for processing (4)
  • .env.example (1 hunks)
  • README.md (1 hunks)
  • src/client/newrelic-client.ts (3 hunks)
  • test/client/newrelic-client/api-region.test.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/client/newrelic-client/api-region.test.ts (1)
src/client/newrelic-client.ts (1)
  • NewRelicClient (45-288)
🔇 Additional comments (9)
README.md (1)

212-217: LGTM! Clear and comprehensive documentation.

The documentation clearly explains the new optional environment variable, its default value, and the mapping between region codes and GraphQL endpoints. This aligns perfectly with the implementation.

.env.example (1)

13-17: LGTM! Helpful configuration example.

The environment variable example is well-documented with clear comments explaining the valid values and their corresponding endpoints. The default value is appropriately shown.

src/client/newrelic-client.ts (3)

1-4: LGTM! Clean constant definition.

The NERDGRAPH_URLS constant is well-structured with proper TypeScript typing using as const for immutability and type narrowing.


48-48: LGTM! Appropriate use of instance property.

Storing the resolved NerdGraph URL as an instance property is the right approach—it's determined once during construction and remains constant for the lifetime of the client instance.

Also applies to: 53-53


270-270: LGTM! Clean refactor to use configurable endpoint.

The fetch call now correctly uses the instance property instead of a hardcoded URL, enabling region-aware API calls while maintaining backward compatibility.

test/client/newrelic-client/api-region.test.ts (4)

1-21: LGTM! Proper test setup and teardown.

The test configuration correctly:

  • Preserves and restores original fetch and process.env
  • Clears mocks between tests to prevent interference
  • Uses proper lifecycle hooks for isolation

23-114: LGTM! Comprehensive coverage of URL selection logic.

This test suite thoroughly validates the getNerdGraphUrl behavior:

  • Default US endpoint when no region is specified
  • Explicit US and EU region selection
  • Case-insensitive handling
  • Safe fallback to US for invalid values

The assertions correctly verify that fetch is called with the expected endpoint URL.


116-193: LGTM! Validates region-specific API operations.

These tests confirm that the region configuration correctly flows through to actual API operations:

  • NRQL queries use the EU endpoint when configured
  • APM application listing uses the US endpoint when configured
  • Mock responses are properly structured
  • Results are validated for correctness

195-216: LGTM! Confirms region persistence.

This test validates that the region is determined once during client construction and consistently used across multiple API calls, which is the correct behavior for this design.

@cloudbring
Copy link
Copy Markdown
Owner

#23 Was merged that has EU region functionality. Let me know if this fixes your issue

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.

2 participants