github.com/go-graphite/carbonapi@v0.17.0/zipper/protocols/prometheus/types/types.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math"
     7  	"strconv"
     8  )
     9  
    10  // Value contains timestamp/value after parsing
    11  type Value struct {
    12  	Timestamp float64
    13  	Value     float64
    14  }
    15  
    16  // Result contains result as returned by Prometheus
    17  type Result struct {
    18  	Metric map[string]string `json:"metric"`
    19  	Values []Value           `json:"values"`
    20  }
    21  
    22  // Data - all useful data (except status and errors) that's returned by prometheus
    23  type Data struct {
    24  	Result     []Result `json:"result"`
    25  	ResultType string   `json:"resultType"`
    26  }
    27  
    28  // HTTPResponse - full HTTP response from prometheus
    29  type HTTPResponse struct {
    30  	Status    string `json:"status"`
    31  	ErrorType string `json:"errorType"`
    32  	Error     string `json:"error"`
    33  	Data      Data   `json:"data"`
    34  }
    35  
    36  func (p *Value) UnmarshalJSON(data []byte) error {
    37  	arr := make([]interface{}, 0)
    38  	err := json.Unmarshal(data, &arr)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	if len(arr) != 2 {
    44  		return fmt.Errorf("length mismatch, got %v, expected 2", len(arr))
    45  	}
    46  
    47  	var ok bool
    48  	p.Timestamp, ok = arr[0].(float64)
    49  	if !ok {
    50  		return fmt.Errorf("type mismatch for element[0/1], expected 'float64', got '%T', str=%v", arr[0], string(data))
    51  	}
    52  
    53  	str, ok := arr[1].(string)
    54  	if !ok {
    55  		return fmt.Errorf("type mismatch for element[1/1], expected 'string', got '%T', str=%v", arr[1], string(data))
    56  	}
    57  
    58  	switch str {
    59  	case "NaN":
    60  		p.Value = math.NaN()
    61  		return nil
    62  	case "+Inf":
    63  		p.Value = math.Inf(1)
    64  		return nil
    65  	case "-Inf":
    66  		p.Value = math.Inf(-1)
    67  		return nil
    68  	default:
    69  		p.Value, err = strconv.ParseFloat(str, 64)
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  type PrometheusTagResponse struct {
    79  	Status    string   `json:"status"`
    80  	ErrorType string   `json:"errorType"`
    81  	Error     string   `json:"error"`
    82  	Data      []string `json:"data"`
    83  }
    84  
    85  type PrometheusFindResponse struct {
    86  	Status    string              `json:"status"`
    87  	ErrorType string              `json:"errorType"`
    88  	Error     string              `json:"error"`
    89  	Data      []map[string]string `json:"data"`
    90  }
    91  
    92  // Tag handles prometheus-specific tags
    93  type Tag struct {
    94  	TagValue string
    95  	OP       string
    96  }