github.com/minio/madmin-go/v3@v3.0.51/prometheus_metrics_test.go (about)

     1  //
     2  // Copyright (c) 2015-2023 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/prometheus/prom2json"
    27  )
    28  
    29  func TestParsePrometheusResultsReturnsPrometheusObjectsFromStringReader(t *testing.T) {
    30  	prometheusResults := `# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
    31  		# TYPE go_gc_duration_seconds summary
    32  		go_gc_duration_seconds_sum 0.248349766
    33  		go_gc_duration_seconds_count 397
    34  	`
    35  	myReader := strings.NewReader(prometheusResults)
    36  	results, err := ParsePrometheusResults(myReader)
    37  	if err != nil {
    38  		t.Errorf("error not expected, got: %v", err)
    39  	}
    40  
    41  	expectedResults := []*prom2json.Family{
    42  		{
    43  			Name: "go_gc_duration_seconds",
    44  			Type: "SUMMARY",
    45  			Help: "A summary of the pause duration of garbage collection cycles.",
    46  			Metrics: []interface{}{
    47  				prom2json.Summary{}, // We just verify length, not content
    48  			},
    49  		},
    50  	}
    51  
    52  	if len(results) != len(expectedResults) {
    53  		t.Errorf("len(results): %d  not equal to len(expectedResults): %d", len(results), len(expectedResults))
    54  	}
    55  
    56  	for i, result := range results {
    57  		if result.Name != expectedResults[i].Name {
    58  			t.Errorf("result.Name: %v  not equal to expectedResults[i].Name: %v", result.Name, expectedResults[i].Name)
    59  		}
    60  		if len(result.Metrics) != len(expectedResults[i].Metrics) {
    61  			t.Errorf("len(result.Metrics): %d  not equal to len(expectedResults[i].Metrics): %d", len(result.Metrics), len(expectedResults[i].Metrics))
    62  		}
    63  	}
    64  }