Skip to content

feat(entities): add cursor pagination for entity search - #1429

Draft
pranav-new-relic wants to merge 1 commit into
mainfrom
feat/entity-search-cursor
Draft

feat(entities): add cursor pagination for entity search#1429
pranav-new-relic wants to merge 1 commit into
mainfrom
feat/entity-search-cursor

Conversation

@pranav-new-relic

Copy link
Copy Markdown
Member

EntitySearch responses include a nextCursor but the existing methods had no way to feed it back in, so callers could only ever see the first page. Adds GetEntitySearchByQueryWithCursor and GetEntitySearchWithCursor (with WithContext variants) that accept a cursor argument alongside the existing parameters; the derived query strings inject $cursor: String and forward it into results(cursor: $cursor).

EntitySearch responses include a nextCursor but the existing methods
had no way to feed it back in, so callers could only ever see the
first page. Adds GetEntitySearchByQueryWithCursor and
GetEntitySearchWithCursor (with WithContext variants) that accept a
cursor argument alongside the existing parameters; the derived query
strings inject $cursor: String and forward it into
results(cursor: $cursor).

Reported by Scott Dewitt in #help-obs-as-code.
@codecov-commenter

codecov-commenter commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.38%. Comparing base (55e6cd2) to head (c8a6dfc).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
pkg/entities/entities_api_.go 85.71% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1429       +/-   ##
===========================================
+ Coverage   32.39%   65.38%   +32.98%     
===========================================
  Files         148      148               
  Lines        6923     6979       +56     
===========================================
+ Hits         2243     4563     +2320     
+ Misses       4467     2004     -2463     
- Partials      213      412      +199     
Flag Coverage Δ
integration 51.71% <85.71%> (?)
unit 32.88% <85.71%> (+0.48%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pranav-new-relic

Copy link
Copy Markdown
Member Author

Trying this out before merge

If you'd like to consume this branch to test the cursor-pagination fix, you can point your go.mod at this commit directly. Two ways:

Option 1: go get at the branch commit (simplest)

From your project root:

go get github.com/newrelic/newrelic-client-go/v2@c8a6dfcc

That pins to the exact commit on this branch and updates go.mod / go.sum with a pseudo-version like v2.90.1-0.20260708...c8a6dfcc. go get github.com/newrelic/newrelic-client-go/v2@feat/entity-search-cursor also works and will re-resolve to whatever the latest commit on the branch is.

Option 2: replace directive against a local checkout

Useful if you want to iterate against a local clone:

git clone --branch feat/entity-search-cursor https://github.com/newrelic/newrelic-client-go.git ../newrelic-client-go

Then in your project's go.mod:

replace github.com/newrelic/newrelic-client-go/v2 => ../newrelic-client-go

Run go mod tidy and you're pointed at the checkout.

Using the new functions

Two new entry points, mirroring the existing pair — pass an empty string for the first page, then feed Results.NextCursor back in until it's empty:

package main

import (
	"context"
	"fmt"

	"github.com/newrelic/newrelic-client-go/v2/newrelic"
	"github.com/newrelic/newrelic-client-go/v2/pkg/config"
	"github.com/newrelic/newrelic-client-go/v2/pkg/entities"
)

func main() {
	client, err := newrelic.New(newrelic.ConfigPersonalAPIKey("NRAK-..."))
	if err != nil {
		panic(err)
	}

	cursor := ""
	page := 0
	for {
		page++
		res, err := client.Entities.GetEntitySearchByQueryWithCursorWithContext(
			context.Background(),
			entities.EntitySearchOptions{},
			"domain = 'APM' AND reporting = 'true'",
			nil,     // sortBy
			cursor,  // "" on the first call
		)
		if err != nil {
			panic(err)
		}

		fmt.Printf("page %d: %d entities\n", page, len(res.Results.Entities))
		for _, e := range res.Results.Entities {
			fmt.Printf("  %s  %s\n", e.GetGUID(), e.GetName())
		}

		if res.Results.NextCursor == "" {
			break // no more pages
		}
		cursor = res.Results.NextCursor
	}
}

The queryBuilder variant is identical in shape — call GetEntitySearchWithCursor(options, "", queryBuilder, sortBy, sortByWithDirection, cursor) and drive the same loop.

Config knobs (unchanged from the existing methods):

  • config.Region / newrelic.ConfigRegion if you're on EU
  • EntitySearchOptions.TagFilter if you want extra tag columns in the response

Happy to answer any questions if something doesn't compile on your side.

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