github.com/kaydxh/golang@v0.0.131/pkg/monitor/opentelemetry/metric/prometheus/prometheus.metric.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package prometheus
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"net/http"
    28  	url_ "net/url"
    29  
    30  	"github.com/prometheus/client_golang/prometheus"
    31  	prometheusmetric "go.opentelemetry.io/otel/exporters/prometheus"
    32  	controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
    33  	"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
    34  )
    35  
    36  const (
    37  	defaultMetricsUrl = "noop://localhost/metrics"
    38  )
    39  
    40  type PrometheusExporterBuilderOptions struct {
    41  	Url string
    42  }
    43  
    44  type PrometheusExporterBuilder struct {
    45  	opts PrometheusExporterBuilderOptions
    46  }
    47  
    48  func defaultBuilderOptions() PrometheusExporterBuilderOptions {
    49  	return PrometheusExporterBuilderOptions{
    50  		Url: defaultMetricsUrl,
    51  	}
    52  }
    53  
    54  func NewPrometheusExporterBuilder(opts ...PrometheusExporterBuilderOption) *PrometheusExporterBuilder {
    55  
    56  	builder := &PrometheusExporterBuilder{
    57  		opts: defaultBuilderOptions(),
    58  	}
    59  	builder.ApplyOptions(opts...)
    60  
    61  	return builder
    62  }
    63  
    64  func (p *PrometheusExporterBuilder) Build(
    65  	ctx context.Context,
    66  	c *controller.Controller,
    67  ) (aggregation.TemporalitySelector, error) {
    68  
    69  	exporter, err := prometheusmetric.New(prometheusmetric.Config{
    70  		Registerer: prometheus.DefaultRegisterer,
    71  		Gatherer:   prometheus.DefaultGatherer,
    72  	}, c)
    73  	if err != nil {
    74  		return nil, fmt.Errorf("new prometheusmetric err: %v", err)
    75  	}
    76  
    77  	u, err := url_.Parse(p.opts.Url)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("parse url: %v, err: %v", p.opts.Url, err)
    80  	}
    81  
    82  	http.HandleFunc(u.Path, exporter.ServeHTTP)
    83  	return exporter, nil
    84  }