Skip to content

Commit 93407d3

Browse files
fix(entities): change EntityAlertViolationInt from int to int64 (#1405)
1 parent 2e519bd commit 93407d3

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

.tutone.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ packages:
865865
- name: WorkloadEntityOutline
866866
generate_struct_getters: true
867867
- name: EntityAlertViolationInt
868-
create_as: int
868+
create_as: int64
869869

870870
# TODO: These should be generated and referenced from the dashboards package
871871
- name: DashboardVariable
@@ -2045,4 +2045,5 @@ packages:
20452045
skip_type_create: true
20462046
- name: EntityGuid
20472047
field_type_override: common.EntityGUID
2048-
skip_type_create: true
2048+
skip_type_create: true
2049+

pkg/entities/entity_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
package entities
55

66
import (
7+
"context"
8+
"encoding/json"
9+
"net/http"
710
"testing"
811

912
"github.com/stretchr/testify/require"
13+
14+
mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers"
1015
)
1116

1217
func TestFindTagByKey(t *testing.T) {
@@ -190,3 +195,74 @@ func TestBuildEntitySearchNrqlQuery(t *testing.T) {
190195
require.Contains(t, result, "type = 'APPLICATION'")
191196
require.Contains(t, result, " AND tags.`tagKey` = 'tagValue' AND tags.`tagKey2` = 'tagValue2'")
192197
}
198+
199+
// TestEntityAlertViolationInt_LargeID verifies that violationIds exceeding the int32 range
200+
// (as returned by the Alerts backend for 16-digit IDs) unmarshal without overflow or error.
201+
func TestEntityAlertViolationInt_LargeID(t *testing.T) {
202+
t.Parallel()
203+
204+
// These are real violation IDs from the bug report (NR-561784) that exceed int32 max (2,147,483,647).
205+
cases := []struct {
206+
json string
207+
want EntityAlertViolationInt
208+
}{
209+
{`{"violationId": 1773950568354017}`, EntityAlertViolationInt(1773950568354017)},
210+
{`{"violationId": 1777567939793015}`, EntityAlertViolationInt(1777567939793015)},
211+
{`{"violationId": 2147483647}`, EntityAlertViolationInt(2147483647)}, // int32 max — still works
212+
{`{"violationId": 2147483648}`, EntityAlertViolationInt(2147483648)}, // one above int32 max
213+
}
214+
215+
for _, tc := range cases {
216+
var v EntityAlertViolation
217+
err := json.Unmarshal([]byte(tc.json), &v)
218+
require.NoError(t, err, "unmarshal failed for input %s", tc.json)
219+
require.Equal(t, tc.want, v.ViolationId)
220+
}
221+
}
222+
223+
// TestGetEntityWithContext_LargeViolationId simulates the full Terraform read path for
224+
// newrelic_synthetics_monitor: a mock NerdGraph server returns a SyntheticMonitorEntity
225+
// with a 16-digit violationId, and GetEntityWithContext must parse it without error.
226+
// This reproduces the crash reported in NR-561784 on 32-bit platforms where
227+
// EntityAlertViolationInt was int (32-bit) instead of int64.
228+
func TestGetEntityWithContext_LargeViolationId(t *testing.T) {
229+
t.Parallel()
230+
231+
// Real violation IDs from the bug report that overflow int32.
232+
const violationID int64 = 1773950568354017
233+
234+
mockResponse := `{
235+
"data": {
236+
"actor": {
237+
"entity": {
238+
"__typename": "SyntheticMonitorEntity",
239+
"guid": "MTAwMjMzMjUwOHxTWU5USHxNT05JVE9SfDEyMzQ1Njc4",
240+
"name": "test-monitor",
241+
"recentAlertViolations": [
242+
{
243+
"violationId": 1773950568354017,
244+
"label": "Synthetics monitor test-monitor",
245+
"level": "CRITICAL",
246+
"alertSeverity": "CRITICAL"
247+
}
248+
]
249+
}
250+
}
251+
}
252+
}`
253+
254+
ts := mock.NewMockServer(t, mockResponse, http.StatusOK)
255+
defer ts.Close()
256+
257+
cfg := mock.NewTestConfig(t, ts)
258+
client := New(cfg)
259+
260+
entity, err := client.GetEntityWithContext(context.Background(), "MTAwMjMzMjUwOHxTWU5USHxNT05JVE9SfDEyMzQ1Njc4")
261+
require.NoError(t, err)
262+
require.NotNil(t, entity)
263+
264+
synthEntity, ok := (*entity).(*SyntheticMonitorEntity)
265+
require.True(t, ok, "expected *SyntheticMonitorEntity, got %T", *entity)
266+
require.Len(t, synthEntity.RecentAlertViolations, 1)
267+
require.Equal(t, EntityAlertViolationInt(violationID), synthEntity.RecentAlertViolations[0].ViolationId)
268+
}

pkg/entities/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11971,7 +11971,7 @@ type AttributeMap map[string]interface{}
1197111971
type DashboardWidgetRawConfiguration []byte
1197211972

1197311973
// EntityAlertViolationInt - The `ViolationInt` scalar type represents 52-bit signed integers
11974-
type EntityAlertViolationInt int
11974+
type EntityAlertViolationInt int64
1197511975

1197611976
// Float - The `Float` scalar type represents signed double-precision fractional
1197711977
// values as specified by

0 commit comments

Comments
 (0)