Skip to content

Commit 846b360

Browse files
committed
feat(workloads): add intelligent workload support with dynamic flows and alert policy
1 parent 2e519bd commit 846b360

5 files changed

Lines changed: 321 additions & 0 deletions

File tree

pkg/testhelpers/helpers.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func GetNonExistentIDs() (string, string) {
5252
return "00000FF000F0FFFF000F0F0000FF0FF0000000F000F00F0FF000FFFFF0FF00F0", "00000HH000H0HHHH000H0H0000HH0HH0000000H000H00H0HH000HHHHH0HH00H0"
5353
}
5454

55+
// GetTestEntityGUID returns the entity GUID for intelligent workload integration tests from the environment.
56+
func GetTestEntityGUID() (string, error) {
57+
return getEnvString("NEW_RELIC_TEST_ENTITY_GUID")
58+
}
59+
60+
// GetTestTransactionName returns the transaction name for intelligent workload integration tests from the environment.
61+
func GetTestTransactionName() (string, error) {
62+
return getEnvString("NEW_RELIC_TEST_TRANSACTION_NAME")
63+
}
64+
5565
// getEnvInt helper to DRY up the other env get calls for integers
5666
func getEnvInt(name string) (int, error) {
5767
if name == "" {
@@ -72,6 +82,20 @@ func getEnvInt(name string) (int, error) {
7282
return n, nil
7383
}
7484

85+
// getEnvString helper to DRY up the other env get calls for strings
86+
func getEnvString(name string) (string, error) {
87+
if name == "" {
88+
return "", fmt.Errorf("failed to get environment value, no name specified")
89+
}
90+
91+
value := os.Getenv(name)
92+
if value == "" {
93+
return "", fmt.Errorf("failed to get environment value due to undefined environment variable %s", name)
94+
}
95+
96+
return value, nil
97+
}
98+
7599
// Use this method to generate a random name to be used in integration tests
76100
// for whatever resource you might be trying to create and/or test.
77101
// If no random character count is provided, the default behavior is to append 5 random

pkg/workloads/example_basic_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,68 @@ func Example_basic() {
6565
log.Fatal("error deleting workload: ", err)
6666
}
6767
}
68+
69+
func Example_intelligentWorkload() {
70+
// Initialize the client configuration. A Personal API key is required to
71+
// communicate with the backend API.
72+
cfg := config.New()
73+
cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")
74+
75+
// Initialize the client.
76+
client := New(cfg)
77+
78+
accountID := 12345678
79+
entityGUID := common.EntityGUID("MjUwODy1OXxOUjF8V097S0xPQ3R8ODcz")
80+
81+
// Create a new intelligent workload using dynamic flows.
82+
// New Relic auto-discovers related entities via Transaction 360 distributed tracing.
83+
// If set alongside entityGuids or entitySearchQueries, dynamicFlows takes precedence
84+
// and others will be ignored.
85+
createInput := WorkloadCreateInput{
86+
Name: "Example intelligent workload",
87+
ScopeAccounts: &WorkloadScopeAccountsInput{
88+
AccountIDs: []int{accountID},
89+
},
90+
DynamicFlows: []WorkloadDynamicFlowInput{
91+
{
92+
EntityGUID: entityGUID,
93+
TransactionName: "WebTransaction/Action/index",
94+
},
95+
},
96+
StatusConfig: &WorkloadStatusConfigInput{
97+
AlertPolicy: &WorkloadAlertPolicyInput{
98+
Enabled: true,
99+
},
100+
},
101+
}
102+
103+
workload, err := client.WorkloadCreate(accountID, createInput)
104+
if err != nil {
105+
log.Fatal("error creating intelligent workload: ", err)
106+
}
107+
108+
// Update an existing intelligent workload.
109+
updated, err := client.WorkloadUpdate(workload.GUID, WorkloadUpdateInput{
110+
Name: "Example intelligent workload - updated",
111+
DynamicFlows: []WorkloadUpdateDynamicFlowInput{
112+
{
113+
EntityGUID: entityGUID,
114+
TransactionName: "WebTransaction/Action/index",
115+
},
116+
},
117+
StatusConfig: &WorkloadUpdateStatusConfigInput{
118+
AlertPolicy: &WorkloadUpdateAlertPolicyInput{
119+
Enabled: false,
120+
},
121+
},
122+
})
123+
if err != nil {
124+
log.Fatal("error updating intelligent workload: ", err)
125+
}
126+
127+
// Delete an existing intelligent workload.
128+
_, err = client.WorkloadDelete(updated.GUID)
129+
if err != nil {
130+
log.Fatal("error deleting intelligent workload: ", err)
131+
}
132+
}

pkg/workloads/types.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ type WorkloadAutomaticStatus struct {
191191
Rules []WorkloadRegularRule `json:"rules"`
192192
}
193193

194+
// WorkloadAlertPolicy - The alert policy status configuration (response type).
195+
type WorkloadAlertPolicy struct {
196+
// Whether the alert policy status configuration is enabled or not.
197+
Enabled bool `json:"enabled"`
198+
}
199+
200+
// WorkloadAlertPolicyInput - The input object used to represent the alert policy status configuration for create.
201+
type WorkloadAlertPolicyInput struct {
202+
// Whether the alert policy status configuration is enabled or not.
203+
Enabled bool `json:"enabled"`
204+
}
205+
194206
// WorkloadAutomaticStatusInput - An input object used to represent an automatic status configuration. If not provided, a status configuration will be created by default.
195207
type WorkloadAutomaticStatusInput struct {
196208
// Whether the automatic status configuration is enabled or not.
@@ -231,6 +243,8 @@ type WorkloadCollection struct {
231243
Status WorkloadWorkloadStatus `json:"status"`
232244
// The configuration that defines how the status of the workload is calculated.
233245
StatusConfig WorkloadStatusConfig `json:"statusConfig,omitempty"`
246+
// A list of dynamic flow entries for an intelligent workload.
247+
DynamicFlows []WorkloadDynamicFlow `json:"dynamicFlows,omitempty"`
234248
// The moment when the object was last updated, represented in milliseconds since the Unix epoch.
235249
UpdatedAt *nrtime.EpochMilliseconds `json:"updatedAt,omitempty"`
236250
// The user who last updated the workload.
@@ -241,6 +255,8 @@ type WorkloadCollection struct {
241255
type WorkloadCreateInput struct {
242256
// Relevant information about the workload.
243257
Description string `json:"description,omitempty"`
258+
// A list of dynamic flow entries for an intelligent workload. When provided alongside entityGuids or entitySearchQueries, dynamicFlows takes precedence and others will be ignored.
259+
DynamicFlows []WorkloadDynamicFlowInput `json:"dynamicFlows,omitempty"`
244260
// A list of entity GUIDs composing the workload.
245261
EntityGUIDs []common.EntityGUID `json:"entityGuids"`
246262
// A list of entity search queries used to retrieve the entities that compose the workload.
@@ -259,6 +275,24 @@ type WorkloadDuplicateInput struct {
259275
Name string `json:"name,omitempty"`
260276
}
261277

278+
// WorkloadDynamicFlow - A dynamic flow entry in an intelligent workload (response type).
279+
type WorkloadDynamicFlow struct {
280+
// The unique entity identifier of the dynamic flow entry.
281+
EntityGUID common.EntityGUID `json:"entityGuid"`
282+
// The unique identifier of the dynamic flow entry.
283+
ID int `json:"id"`
284+
// The transaction name associated with the dynamic flow entry.
285+
TransactionName string `json:"transactionName"`
286+
}
287+
288+
// WorkloadDynamicFlowInput - The input object used to represent a dynamic flow entry for create.
289+
type WorkloadDynamicFlowInput struct {
290+
// The unique entity identifier of the dynamic flow entry.
291+
EntityGUID common.EntityGUID `json:"entityGuid"`
292+
// The transaction name associated with the dynamic flow entry.
293+
TransactionName string `json:"transactionName"`
294+
}
295+
262296
// WorkloadEntityRef - A reference to a New Relic entity.
263297
type WorkloadEntityRef struct {
264298
// The unique entity identifier in New Relic.
@@ -459,6 +493,8 @@ type WorkloadStatus struct {
459493

460494
// WorkloadStatusConfig - The configuration that defines how the status of the workload is calculated.
461495
type WorkloadStatusConfig struct {
496+
// An alert policy status configuration for intelligent workloads.
497+
AlertPolicy WorkloadAlertPolicy `json:"alertPolicy,omitempty"`
462498
// An automatic status configuration.
463499
Automatic WorkloadAutomaticStatus `json:"automatic,omitempty"`
464500
// A list of static status configurations.
@@ -467,6 +503,8 @@ type WorkloadStatusConfig struct {
467503

468504
// WorkloadStatusConfigInput - The input object used to provide the configuration that defines how the status of the workload is calculated.
469505
type WorkloadStatusConfigInput struct {
506+
// An alert policy status configuration for intelligent workloads.
507+
AlertPolicy *WorkloadAlertPolicyInput `json:"alertPolicy,omitempty"`
470508
// An input object used to represent an automatic status configuration.
471509
Automatic *WorkloadAutomaticStatusInput `json:"automatic,omitempty"`
472510
// A list of static status configurations. You can only configure one static status for a workload.
@@ -483,6 +521,12 @@ type WorkloadStatusResult struct {
483521

484522
func (x *WorkloadStatusResult) ImplementsWorkloadStatusResult() {}
485523

524+
// WorkloadUpdateAlertPolicyInput - The input object used to represent the alert policy status configuration for update.
525+
type WorkloadUpdateAlertPolicyInput struct {
526+
// Whether the alert policy status configuration is enabled or not.
527+
Enabled bool `json:"enabled"`
528+
}
529+
486530
// WorkloadUpdateAutomaticStatusInput - An input object used to represent an automatic status configuration.
487531
type WorkloadUpdateAutomaticStatusInput struct {
488532
// Whether the automatic status configuration is enabled or not.
@@ -493,6 +537,16 @@ type WorkloadUpdateAutomaticStatusInput struct {
493537
Rules []WorkloadUpdateRegularRuleInput `json:"rules,omitempty"`
494538
}
495539

540+
// WorkloadUpdateDynamicFlowInput - The input object used to represent a dynamic flow entry for update.
541+
type WorkloadUpdateDynamicFlowInput struct {
542+
// The unique entity identifier of the dynamic flow entry.
543+
EntityGUID common.EntityGUID `json:"entityGuid"`
544+
// The unique identifier of the dynamic flow entry to be updated. If not provided, a new entry is created.
545+
ID int `json:"id,omitempty"`
546+
// The transaction name associated with the dynamic flow entry.
547+
TransactionName string `json:"transactionName"`
548+
}
549+
496550
// WorkloadUpdateCollectionEntitySearchQueryInput - The input object used to represent the entity search query to be updated.
497551
type WorkloadUpdateCollectionEntitySearchQueryInput struct {
498552
// The unique identifier of the entity search query to be updated. If not provided, a new entity search query is created.
@@ -505,6 +559,8 @@ type WorkloadUpdateCollectionEntitySearchQueryInput struct {
505559
type WorkloadUpdateInput struct {
506560
// Relevant information about the workload.
507561
Description string `json:"description,omitempty"`
562+
// A list of dynamic flow entries for an intelligent workload. When provided alongside entityGuids or entitySearchQueries, dynamicFlows takes precedence and others will be ignored.
563+
DynamicFlows []WorkloadUpdateDynamicFlowInput `json:"dynamicFlows,omitempty"`
508564
// A list of entity GUIDs composing the workload.
509565
EntityGUIDs []common.EntityGUID `json:"entityGuids"`
510566
// A list of entity search queries used to retrieve the groups of entities that compose the workload.
@@ -545,6 +601,8 @@ type WorkloadUpdateStaticStatusInput struct {
545601

546602
// WorkloadUpdateStatusConfigInput - The input object used to provide the configuration that defines how the status of the workload is calculated.
547603
type WorkloadUpdateStatusConfigInput struct {
604+
// An alert policy status configuration for intelligent workloads.
605+
AlertPolicy *WorkloadUpdateAlertPolicyInput `json:"alertPolicy,omitempty"`
548606
// An input object used to represent an automatic status configuration.
549607
Automatic *WorkloadUpdateAutomaticStatusInput `json:"automatic,omitempty"`
550608
// A list of static status configurations. You can only configure one static status for a workload.

pkg/workloads/workloads_api.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)