|
| 1 | +package evaluator |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestGuessUnitFromMetricName(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + metricName string |
| 9 | + want string |
| 10 | + }{ |
| 11 | + {name: "seconds suffix", metricName: "http_request_duration_seconds", want: "seconds"}, |
| 12 | + {name: "bytes suffix", metricName: "container_memory_usage_bytes", want: "bytes"}, |
| 13 | + {name: "seconds total suffix", metricName: "process_cpu_seconds_total", want: "seconds"}, |
| 14 | + {name: "timestamp seconds suffix", metricName: "last_seen_timestamp_seconds", want: "seconds"}, |
| 15 | + {name: "real metric histogram bytes", metricName: "http_incoming_request_size_histogram_bytes", want: "bytes"}, |
| 16 | + {name: "real metric bytes total", metricName: "go_memstats_alloc_bytes_total", want: "bytes"}, |
| 17 | + {name: "real metric duration milliseconds", metricName: "rox_sensor_scan_call_duration_milliseconds", want: "milliseconds"}, |
| 18 | + {name: "real metric purger duration seconds", metricName: "rox_sensor_network_flow_manager_purger_duration_seconds", want: "seconds"}, |
| 19 | + {name: "real metric vm report duration milliseconds", metricName: "rox_sensor_virtual_machine_index_report_processing_duration_milliseconds", want: "milliseconds"}, |
| 20 | + {name: "sec alias suffix", metricName: "request_duration_sec", want: "seconds"}, |
| 21 | + {name: "msec alias suffix", metricName: "request_duration_msec", want: "milliseconds"}, |
| 22 | + {name: "no known suffix", metricName: "rox_sensor_events", want: ""}, |
| 23 | + {name: "empty metric name", metricName: "", want: ""}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, tt := range tests { |
| 27 | + t.Run(tt.name, func(t *testing.T) { |
| 28 | + got := guessUnitFromMetricName(tt.metricName) |
| 29 | + if got != tt.want { |
| 30 | + t.Fatalf("guessUnitFromMetricName(%q) = %q, want %q", tt.metricName, got, tt.want) |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestGuessUnitFromHelpText(t *testing.T) { |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + helpText string |
| 40 | + want string |
| 41 | + }{ |
| 42 | + {name: "milliseconds mention", helpText: "Time taken in milliseconds to process events", want: "milliseconds"}, |
| 43 | + {name: "seconds mention", helpText: "Duration in seconds", want: "seconds"}, |
| 44 | + {name: "seconds short sec mention", helpText: "Duration in sec", want: "seconds"}, |
| 45 | + {name: "bytes mention", helpText: "Payload size in bytes", want: "bytes"}, |
| 46 | + {name: "milliseconds alias msec", helpText: "Call took 32 msec", want: "milliseconds"}, |
| 47 | + {name: "milliseconds alias millis", helpText: "Observed latency in millis", want: "milliseconds"}, |
| 48 | + {name: "byte singular mention", helpText: "Equals to /memory/classes/total:byte.", want: "bytes"}, |
| 49 | + {name: "ambiguous mentions", helpText: "Latency in milliseconds and seconds", want: ""}, |
| 50 | + {name: "ambiguous time and bytes mentions", helpText: "Duration in seconds with payload bytes", want: ""}, |
| 51 | + {name: "no known units", helpText: "Number of retries", want: ""}, |
| 52 | + {name: "empty help text", helpText: "", want: ""}, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + got := guessUnitFromHelpText(tt.helpText) |
| 58 | + if got != tt.want { |
| 59 | + t.Fatalf("guessUnitFromHelpText(%q) = %q, want %q", tt.helpText, got, tt.want) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestGuessMetricUnit(t *testing.T) { |
| 66 | + tests := []struct { |
| 67 | + name string |
| 68 | + metricName string |
| 69 | + helpText string |
| 70 | + want string |
| 71 | + }{ |
| 72 | + { |
| 73 | + name: "metric name wins over help text", |
| 74 | + metricName: "process_cpu_seconds_total", |
| 75 | + helpText: "CPU usage in milliseconds", |
| 76 | + want: "seconds", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "falls back to help text when name has no unit", |
| 80 | + metricName: "rox_central_event_processing", |
| 81 | + helpText: "Time taken in milliseconds", |
| 82 | + want: "milliseconds", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "real cluster duration without unit in name uses help", |
| 86 | + metricName: "rox_sensor_k8s_event_processing_duration", |
| 87 | + helpText: "Time spent fully processing an event from Kubernetes in milliseconds", |
| 88 | + want: "milliseconds", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "returns empty when both sources fail", |
| 92 | + metricName: "rox_sensor_events", |
| 93 | + helpText: "Count of events", |
| 94 | + want: "", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "real metric without unit in name or help", |
| 98 | + metricName: "rox_sensor_k8s_event_processing_duration", |
| 99 | + helpText: "Time taken to fully process an event from Kubernetes", |
| 100 | + want: "", |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tt := range tests { |
| 105 | + t.Run(tt.name, func(t *testing.T) { |
| 106 | + got := guessMetricUnit(tt.metricName, tt.helpText) |
| 107 | + if got != tt.want { |
| 108 | + t.Fatalf("guessMetricUnit(%q, %q) = %q, want %q", tt.metricName, tt.helpText, got, tt.want) |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments