github.com/newrelic/newrelic-client-go@v1.1.0/pkg/apm/applications_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package apm 5 6 import ( 7 "fmt" 8 "net/http" 9 "testing" 10 11 "time" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var ( 17 testApplicationSummary = ApplicationSummary{ 18 ResponseTime: 5.91, 19 Throughput: 1, 20 ErrorRate: 0, 21 ApdexTarget: 0.5, 22 ApdexScore: 1, 23 HostCount: 1, 24 InstanceCount: 15, 25 ConcurrentInstanceCount: 1, 26 } 27 28 testApplicationEndUserSummary = ApplicationEndUserSummary{ 29 ResponseTime: 3.8, 30 Throughput: 1660, 31 ApdexTarget: 2.5, 32 ApdexScore: 0.78, 33 } 34 35 testApplicationSettings = ApplicationSettings{ 36 AppApdexThreshold: 0.5, 37 EndUserApdexThreshold: 7, 38 EnableRealUserMonitoring: true, 39 UseServerSideConfig: false, 40 } 41 42 testApplicationLinks = ApplicationLinks{ 43 ServerIDs: []int{}, 44 HostIDs: []int{204260579}, 45 InstanceIDs: []int{204261411}, 46 AlertPolicyID: 1234, 47 } 48 49 testApplication = Application{ 50 ID: 204261410, 51 Name: "Billing Service", 52 Language: "python", 53 HealthStatus: "unknown", 54 Reporting: true, 55 LastReportedAt: "2019-12-11T19:09:10+00:00", 56 Summary: testApplicationSummary, 57 EndUserSummary: testApplicationEndUserSummary, 58 Settings: testApplicationSettings, 59 Links: testApplicationLinks, 60 } 61 62 testMetricNames = []*MetricName{ 63 {"GC/System/Pauses", []string{ 64 "as_percentage", 65 "average_time", 66 "calls_per_minute", 67 "max_value", 68 "total_call_time_per_minute", 69 "utilization", 70 }}, 71 {"Memory/Heap/Free", []string{ 72 "used_bytes_by_host", 73 "used_mb_by_host", 74 "total_used_mb", 75 }}, 76 } 77 78 testApplicationJson = `{ 79 "id": 204261410, 80 "name": "Billing Service", 81 "language": "python", 82 "health_status": "unknown", 83 "reporting": true, 84 "last_reported_at": "2019-12-11T19:09:10+00:00", 85 "application_summary": { 86 "response_time": 5.91, 87 "throughput": 1, 88 "error_rate": 0, 89 "apdex_target": 0.5, 90 "apdex_score": 1, 91 "host_count": 1, 92 "instance_count": 15, 93 "concurrent_instance_count": 1 94 }, 95 "end_user_summary": { 96 "response_time": 3.8, 97 "throughput": 1660, 98 "apdex_target": 2.5, 99 "apdex_score": 0.78 100 }, 101 "settings": { 102 "app_apdex_threshold": 0.5, 103 "end_user_apdex_threshold": 7, 104 "enable_real_user_monitoring": true, 105 "use_server_side_config": false 106 }, 107 "links": { 108 "application_instances": [ 109 204261411 110 ], 111 "servers": [], 112 "application_hosts": [ 113 204260579 114 ], 115 "alert_policy": 1234 116 } 117 }` 118 119 testMetricNamesJson = `{ 120 "metrics": [ 121 { 122 "name": "GC/System/Pauses", 123 "values": [ 124 "as_percentage", 125 "average_time", 126 "calls_per_minute", 127 "max_value", 128 "total_call_time_per_minute", 129 "utilization" 130 ] 131 }, 132 { 133 "name": "Memory/Heap/Free", 134 "values": [ 135 "used_bytes_by_host", 136 "used_mb_by_host", 137 "total_used_mb" 138 ] 139 } 140 ] 141 }` 142 143 testMetricDataJson = `{ 144 "metric_data": { 145 "from": "2020-01-27T23:25:45+00:00", 146 "to": "2020-01-27T23:55:45+00:00", 147 "metrics_not_found": [], 148 "metrics_found": [ 149 "GC/System/Pauses" 150 ], 151 "metrics": [ 152 { 153 "name": "GC/System/Pauses", 154 "timeslices": [ 155 { 156 "from": "2020-01-27T23:22:00+00:00", 157 "to": "2020-01-27T23:23:00+00:00", 158 "values": { 159 "as_percentage": 0.0298, 160 "average_time": 0.298, 161 "calls_per_minute": 65.9, 162 "max_value": 0.0006, 163 "total_call_time_per_minute": 0.0196, 164 "utilization": 0.0327 165 } 166 }, 167 { 168 "from": "2020-01-27T23:23:00+00:00", 169 "to": "2020-01-27T23:24:00+00:00", 170 "values": { 171 "as_percentage": 0.0294, 172 "average_time": 0.294, 173 "calls_per_minute": 67, 174 "max_value": 0.0005, 175 "total_call_time_per_minute": 0.0197, 176 "utilization": 0.0328 177 } 178 } 179 ] 180 } 181 ] 182 } 183 }` 184 ) 185 186 func TestListApplications(t *testing.T) { 187 t.Parallel() 188 responseJSON := fmt.Sprintf(`{ "applications": [%s] }`, testApplicationJson) 189 apm := newMockResponse(t, responseJSON, http.StatusOK) 190 191 actual, err := apm.ListApplications(nil) 192 193 expected := []*Application{&testApplication} 194 195 assert.NoError(t, err) 196 assert.NotNil(t, actual) 197 assert.Equal(t, expected, actual) 198 } 199 200 func TestListApplicationsWithParams(t *testing.T) { 201 t.Parallel() 202 expectedName := "appName" 203 expectedHost := "appHost" 204 expectedLanguage := "appLanguage" 205 expectedIDs := "123,456" 206 207 apm := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 208 values := r.URL.Query() 209 210 name := values.Get("filter[name]") 211 host := values.Get("filter[host]") 212 ids := values.Get("filter[ids]") 213 language := values.Get("filter[language]") 214 215 assert.Equal(t, expectedName, name) 216 assert.Equal(t, expectedHost, host) 217 assert.Equal(t, expectedIDs, ids) 218 assert.Equal(t, expectedLanguage, language) 219 220 w.Header().Set("Content-Type", "application/json") 221 _, err := w.Write([]byte(`{"applications":[]}`)) 222 223 assert.NoError(t, err) 224 })) 225 226 params := ListApplicationsParams{ 227 Name: expectedName, 228 Host: expectedHost, 229 IDs: []int{123, 456}, 230 Language: expectedLanguage, 231 } 232 233 _, err := apm.ListApplications(¶ms) 234 235 assert.NoError(t, err) 236 } 237 238 func TestGetApplication(t *testing.T) { 239 t.Parallel() 240 responseJSON := fmt.Sprintf(`{ "application": %s}`, testApplicationJson) 241 apm := newMockResponse(t, responseJSON, http.StatusOK) 242 243 actual, err := apm.GetApplication(testApplication.ID) 244 245 assert.NoError(t, err) 246 assert.NotNil(t, actual) 247 assert.Equal(t, &testApplication, actual) 248 } 249 250 func TestUpdateApplication(t *testing.T) { 251 t.Parallel() 252 responseJSON := fmt.Sprintf(`{ "application": %s}`, testApplicationJson) 253 apm := newMockResponse(t, responseJSON, http.StatusOK) 254 255 params := UpdateApplicationParams{ 256 Name: testApplication.Name, 257 Settings: testApplication.Settings, 258 } 259 260 actual, err := apm.UpdateApplication(testApplication.ID, params) 261 262 assert.NoError(t, err) 263 assert.NotNil(t, actual) 264 assert.Equal(t, &testApplication, actual) 265 } 266 267 func TestDeleteApplication(t *testing.T) { 268 t.Parallel() 269 responseJSON := fmt.Sprintf(`{ "application": %s}`, testApplicationJson) 270 apm := newMockResponse(t, responseJSON, http.StatusOK) 271 272 actual, err := apm.DeleteApplication(testApplication.ID) 273 274 assert.NoError(t, err) 275 assert.NotNil(t, actual) 276 assert.Equal(t, &testApplication, actual) 277 } 278 279 func TestGetMetricNames(t *testing.T) { 280 t.Parallel() 281 apm := newMockResponse(t, testMetricNamesJson, http.StatusOK) 282 283 actual, err := apm.GetMetricNames(testApplication.ID, MetricNamesParams{}) 284 expected := testMetricNames 285 286 assert.NoError(t, err) 287 assert.NotNil(t, actual) 288 assert.Equal(t, len(expected), len(actual)) 289 290 if len(expected) == len(actual) { 291 for i := range expected { 292 assert.Equal(t, expected[i], actual[i]) 293 } 294 } 295 } 296 297 func TestMetricData(t *testing.T) { 298 t.Parallel() 299 apm := newMockResponse(t, testMetricDataJson, http.StatusOK) 300 301 actual, err := apm.GetMetricData(testApplication.ID, MetricDataParams{}) 302 expectedTimeSlices := []struct { 303 From string 304 To string 305 Values MetricTimesliceValues 306 }{ 307 { 308 "2020-01-27T23:22:00+00:00", 309 "2020-01-27T23:23:00+00:00", 310 MetricTimesliceValues{ 311 AsPercentage: 0.0298, 312 AverageTime: 0.298, 313 CallsPerMinute: 65.9, 314 MaxValue: 0.0006, 315 TotalCallTimePerMinute: 0.0196, 316 Utilization: 0.0327, 317 Values: map[string]float64{}, 318 }, 319 }, 320 { 321 "2020-01-27T23:23:00+00:00", 322 "2020-01-27T23:24:00+00:00", 323 MetricTimesliceValues{ 324 AsPercentage: 0.0294, 325 AverageTime: 0.294, 326 CallsPerMinute: 67, 327 MaxValue: 0.0005, 328 TotalCallTimePerMinute: 0.0197, 329 Utilization: 0.0328, 330 Values: map[string]float64{}, 331 }, 332 }, 333 } 334 335 assert.NoError(t, err) 336 assert.NotNil(t, actual) 337 338 for i, e := range expectedTimeSlices { 339 from, err := time.Parse(time.RFC3339, e.From) 340 assert.NoError(t, err) 341 342 to, err := time.Parse(time.RFC3339, e.To) 343 assert.NoError(t, err) 344 345 assert.Equal(t, &from, actual[0].Timeslices[i].From) 346 assert.Equal(t, &to, actual[0].Timeslices[i].To) 347 348 assert.Equal(t, e.Values.AsPercentage, actual[0].Timeslices[i].Values.AsPercentage) 349 assert.Equal(t, e.Values.AverageTime, actual[0].Timeslices[i].Values.AverageTime) 350 assert.Equal(t, e.Values.CallsPerMinute, actual[0].Timeslices[i].Values.CallsPerMinute) 351 assert.Equal(t, e.Values.MaxValue, actual[0].Timeslices[i].Values.MaxValue) 352 assert.Equal(t, e.Values.TotalCallTimePerMinute, actual[0].Timeslices[i].Values.TotalCallTimePerMinute) 353 assert.Equal(t, e.Values.Utilization, actual[0].Timeslices[i].Values.Utilization) 354 } 355 }