github.com/thanos-io/thanos@v0.32.5/pkg/metadata/prometheus.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package metadata
     5  
     6  import (
     7  	"context"
     8  	"net/url"
     9  
    10  	"github.com/thanos-io/thanos/pkg/metadata/metadatapb"
    11  	"github.com/thanos-io/thanos/pkg/promclient"
    12  	"github.com/thanos-io/thanos/pkg/tracing"
    13  )
    14  
    15  // Prometheus implements metadatapb.Metadata gRPC service that allows to fetch metric metadata from Prometheus HTTP /api/v1/metadata endpoint.
    16  type Prometheus struct {
    17  	base   *url.URL
    18  	client *promclient.Client
    19  }
    20  
    21  // NewPrometheus creates a new metadata.Prometheus.
    22  func NewPrometheus(base *url.URL, client *promclient.Client) *Prometheus {
    23  	return &Prometheus{
    24  		base:   base,
    25  		client: client,
    26  	}
    27  }
    28  
    29  // MetricMetadata returns all specified metric metadata from Prometheus.
    30  func (p *Prometheus) MetricMetadata(r *metadatapb.MetricMetadataRequest, s metadatapb.Metadata_MetricMetadataServer) error {
    31  	md, err := p.client.MetricMetadataInGRPC(s.Context(), p.base, r.Metric, int(r.Limit))
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	tracing.DoInSpan(s.Context(), "send_metadata_response", func(_ context.Context) {
    37  		err = s.Send(&metadatapb.MetricMetadataResponse{Result: &metadatapb.MetricMetadataResponse_Metadata{
    38  			Metadata: metadatapb.FromMetadataMap(md)}})
    39  	})
    40  	return err
    41  }