Skip to content

Commit 83b98f9

Browse files
authored
Merge pull request #50 from newrelic/feat/add-monitoring-type
feat: add support to set monitoringType
2 parents 0cb0aaa + 0ba1e92 commit 83b98f9

7 files changed

Lines changed: 220 additions & 14 deletions

File tree

action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ inputs:
4343
description: 'JSON array with artifact definitions. Each artifact must specify name, path, os, arch, and format. Example: [{"name": "linux-tar", "path": "./dist/agent.tar.gz", "os": "linux", "arch": "amd64", "format": "tar+gzip"}]'
4444
required: false
4545
default: ''
46+
monitoring-type:
47+
description: 'Monitoring type indicating which fleet this agent belongs to (APM or INFRA). Leave empty to use the service default mapping.'
48+
required: false
49+
default: ''
50+
display-name:
51+
description: 'Human-readable display name for this agent.'
52+
required: false
53+
default: ''
4654
cache:
4755
description: 'Enable Go build cache'
4856
required: false
@@ -196,6 +204,8 @@ runs:
196204
INPUT_AGENT_TYPE: ${{ inputs.agent-type }}
197205
INPUT_VERSION: ${{ inputs.version }}
198206
INPUT_CONFIG_DIRECTORY: ${{ inputs.config-directory }}
207+
INPUT_MONITORING_TYPE: ${{ inputs.monitoring-type }}
208+
INPUT_DISPLAY_NAME: ${{ inputs.display-name }}
199209
NEWRELIC_TOKEN: ${{ steps.newrelic-auth.outputs.token }}
200210
INPUT_OCI_REGISTRY: ${{ inputs.oci-registry }}
201211
INPUT_OCI_USERNAME: ${{ inputs.oci-username }}

cmd/agent-metadata-action/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ func run(nrApp *newrelic.Application) error {
134134
// Determine which flow to execute
135135
agentType := config.GetAgentType()
136136
agentVersion := config.GetVersion()
137+
monitoringType := config.GetMonitoringType()
138+
if monitoringType != "" && monitoringType != "APM" && monitoringType != "INFRA" {
139+
return fmt.Errorf("invalid monitoring-type %q: must be APM or INFRA", monitoringType)
140+
}
137141

138142
if agentType != "" && agentVersion != "" {
139143
return runAgentFlow(ctx, metadataClient, workspace, agentType, agentVersion)

cmd/agent-metadata-action/main_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,63 @@ func TestRun_InvalidEnvironment(t *testing.T) {
213213
assert.Contains(t, err.Error(), "workspace directory does not exist")
214214
}
215215

216+
func TestRun_InvalidMonitoringType(t *testing.T) {
217+
originalCreateClient := createMetadataClientFunc
218+
createMetadataClientFunc = func(baseURL, token string) metadataClient {
219+
return &mockMetadataClient{}
220+
}
221+
defer func() { createMetadataClientFunc = originalCreateClient }()
222+
223+
workspace := t.TempDir()
224+
t.Setenv("GITHUB_WORKSPACE", workspace)
225+
t.Setenv("NEWRELIC_TOKEN", "mock-token")
226+
t.Setenv("INPUT_AGENT_TYPE", "java")
227+
t.Setenv("INPUT_VERSION", "1.0.0")
228+
t.Setenv("INPUT_MONITORING_TYPE", "METRICS")
229+
230+
err := run(nil)
231+
232+
assert.Error(t, err)
233+
assert.Contains(t, err.Error(), "invalid monitoring-type")
234+
assert.Contains(t, err.Error(), "must be APM or INFRA")
235+
}
236+
237+
func TestRun_ValidMonitoringTypes(t *testing.T) {
238+
tests := []struct {
239+
name string
240+
monitoringType string
241+
}{
242+
{name: "empty - relies on service default", monitoringType: ""},
243+
{name: "APM", monitoringType: "APM"},
244+
{name: "INFRA", monitoringType: "INFRA"},
245+
}
246+
247+
projectRoot, err := filepath.Abs("../..")
248+
require.NoError(t, err)
249+
workspace := filepath.Join(projectRoot, "integration-test", "agent-flow")
250+
251+
for _, tt := range tests {
252+
t.Run(tt.name, func(t *testing.T) {
253+
originalCreateClient := createMetadataClientFunc
254+
createMetadataClientFunc = func(baseURL, token string) metadataClient {
255+
return &mockMetadataClient{}
256+
}
257+
defer func() { createMetadataClientFunc = originalCreateClient }()
258+
259+
t.Setenv("GITHUB_WORKSPACE", workspace)
260+
t.Setenv("NEWRELIC_TOKEN", "mock-token")
261+
t.Setenv("INPUT_AGENT_TYPE", "java")
262+
t.Setenv("INPUT_VERSION", "1.0.0")
263+
t.Setenv("INPUT_OCI_REGISTRY", "")
264+
t.Setenv("INPUT_MONITORING_TYPE", tt.monitoringType)
265+
266+
err := run(nil)
267+
268+
assert.NoError(t, err)
269+
})
270+
}
271+
}
272+
216273
func TestValidateEnvironment(t *testing.T) {
217274
tests := []struct {
218275
name string

internal/config/dirs_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestGetRootFolderForAgentRepo(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
setupFunc func(t *testing.T)
12+
expected string
13+
}{
14+
{
15+
name: "returns explicit override when INPUT_CONFIG_DIRECTORY is set",
16+
setupFunc: func(t *testing.T) {
17+
if err := os.Setenv("INPUT_CONFIG_DIRECTORY", ".custom"); err != nil {
18+
t.Fatalf("failed to set env: %v", err)
19+
}
20+
t.Cleanup(func() {
21+
os.Unsetenv("INPUT_CONFIG_DIRECTORY")
22+
})
23+
},
24+
expected: ".custom",
25+
},
26+
{
27+
name: "returns .fleetControl as default when no override",
28+
setupFunc: func(t *testing.T) {
29+
os.Unsetenv("INPUT_CONFIG_DIRECTORY")
30+
},
31+
expected: ".fleetControl",
32+
},
33+
{
34+
name: "trims whitespace from override values",
35+
setupFunc: func(t *testing.T) {
36+
if err := os.Setenv("INPUT_CONFIG_DIRECTORY", " .custom "); err != nil {
37+
t.Fatalf("failed to set env: %v", err)
38+
}
39+
t.Cleanup(func() {
40+
os.Unsetenv("INPUT_CONFIG_DIRECTORY")
41+
})
42+
},
43+
expected: ".custom",
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
tt.setupFunc(t)
50+
got := GetRootFolderForAgentRepo()
51+
if got != tt.expected {
52+
t.Errorf("expected %q, got %q", tt.expected, got)
53+
}
54+
})
55+
}
56+
}

internal/config/env.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ func GetConfigDirectory() string {
6565
return os.Getenv("INPUT_CONFIG_DIRECTORY")
6666
}
6767

68+
// GetMonitoringType loads the monitoring type from environment variables
69+
func GetMonitoringType() string {
70+
return os.Getenv("INPUT_MONITORING_TYPE")
71+
}
72+
73+
// GetDisplayName loads the display name from environment variables
74+
func GetDisplayName() string {
75+
return os.Getenv("INPUT_DISPLAY_NAME")
76+
}
77+
6878
// SetNRAgentHost sets the host to use for the go agent that will be used to monitor this app
6979
func SetNRAgentHost() error {
7080
err := os.Setenv("NEW_RELIC_HOST", "staging-collector.newrelic.com")

internal/loader/metadata.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package loader
22

33
import (
4+
"agent-metadata-action/internal/config"
45
"agent-metadata-action/internal/github"
56
"agent-metadata-action/internal/logging"
67
"agent-metadata-action/internal/models"
@@ -9,11 +10,16 @@ import (
910
"fmt"
1011
)
1112

12-
// LoadMetadataForAgents loads metadata with only version populated
13+
// LoadMetadataForAgents loads metadata with version and optional monitoringType
1314
func LoadMetadataForAgents(version string) models.Metadata {
14-
return models.Metadata{
15-
"version": version,
15+
m := models.Metadata{"version": version}
16+
if monitoringType := config.GetMonitoringType(); monitoringType != "" {
17+
m["monitoringType"] = monitoringType
1618
}
19+
if displayName := config.GetDisplayName(); displayName != "" {
20+
m["displayName"] = displayName
21+
}
22+
return m
1723
}
1824

1925
type MetadataForDocs struct {

internal/loader/metadata_test.go

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,80 @@ import (
1515
)
1616

1717
func TestLoadMetadataForAgents(t *testing.T) {
18-
t.Setenv("INPUT_AGENT_TYPE", "myagenttype")
19-
t.Setenv("INPUT_VERSION", "1.2.3")
20-
21-
metadata := LoadMetadataForAgents("1.2.3")
22-
assert.Equal(t, "1.2.3", metadata["version"])
23-
assert.Empty(t, metadata["features"])
24-
assert.Empty(t, metadata["bugs"])
25-
assert.Empty(t, metadata["security"])
26-
assert.Empty(t, metadata["deprecations"])
27-
assert.Empty(t, metadata["supportedOperatingSystems"])
28-
assert.Empty(t, metadata["eol"])
18+
tests := []struct {
19+
name string
20+
version string
21+
monitoringType string
22+
expectMonitoringType bool
23+
expectedMonitoringVal string
24+
}{
25+
{
26+
name: "no monitoringType - only version key present",
27+
version: "1.2.3",
28+
monitoringType: "",
29+
expectMonitoringType: false,
30+
},
31+
{
32+
name: "APM monitoringType",
33+
version: "1.2.3",
34+
monitoringType: "APM",
35+
expectMonitoringType: true,
36+
expectedMonitoringVal: "APM",
37+
},
38+
{
39+
name: "INFRA monitoringType",
40+
version: "1.2.3",
41+
monitoringType: "INFRA",
42+
expectMonitoringType: true,
43+
expectedMonitoringVal: "INFRA",
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
t.Setenv("INPUT_MONITORING_TYPE", tt.monitoringType)
50+
metadata := LoadMetadataForAgents(tt.version)
51+
assert.Equal(t, tt.version, metadata["version"])
52+
if tt.expectMonitoringType {
53+
assert.Equal(t, tt.expectedMonitoringVal, metadata["monitoringType"])
54+
} else {
55+
assert.NotContains(t, metadata, "monitoringType")
56+
}
57+
})
58+
}
59+
}
60+
61+
func TestLoadMetadataForAgents_DisplayName(t *testing.T) {
62+
tests := []struct {
63+
name string
64+
displayName string
65+
expectKey bool
66+
expectedVal string
67+
}{
68+
{
69+
name: "no displayName - key absent",
70+
displayName: "",
71+
expectKey: false,
72+
},
73+
{
74+
name: "displayName set",
75+
displayName: "Java Agent",
76+
expectKey: true,
77+
expectedVal: "Java Agent",
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
t.Setenv("INPUT_DISPLAY_NAME", tt.displayName)
84+
metadata := LoadMetadataForAgents("1.2.3")
85+
if tt.expectKey {
86+
assert.Equal(t, tt.expectedVal, metadata["displayName"])
87+
} else {
88+
assert.NotContains(t, metadata, "displayName")
89+
}
90+
})
91+
}
2992
}
3093

3194
func TestLoadMetadata_WithMDXFiles_Success(t *testing.T) {

0 commit comments

Comments
 (0)