github.com/kaydxh/golang@v0.0.131/pkg/monitor/opentelemetry/metric/meter.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 metric
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"time"
    28  
    29  	"go.opentelemetry.io/otel/metric/global"
    30  	controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
    31  	"go.opentelemetry.io/otel/sdk/metric/export"
    32  	"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
    33  	processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
    34  	"go.opentelemetry.io/otel/sdk/metric/selector/simple"
    35  )
    36  
    37  type MeterOptinos struct {
    38  	PushExporterBuilder PushExporterBuilder
    39  	PullExporterBuilder PullExporterBuilder
    40  	collectPeriod       time.Duration
    41  }
    42  
    43  type Meter struct {
    44  	Controller controller.Controller
    45  	opts       MeterOptinos
    46  }
    47  
    48  func defaultMeterOptions() MeterOptinos {
    49  	return MeterOptinos{
    50  		collectPeriod: time.Minute,
    51  	}
    52  }
    53  
    54  func NewMeter(opts ...MeterOption) *Meter {
    55  	m := &Meter{
    56  		opts: defaultMeterOptions(),
    57  	}
    58  	m.ApplyOptions(opts...)
    59  	return m
    60  }
    61  
    62  //https://github.com/open-telemetry/opentelemetry-go/blob/example/prometheus/v0.30.0/example/prometheus/main.go
    63  //https://github.com/kaydxh/newrelic-opentelemetry-examples/blob/main/go/metrics.go
    64  func (m *Meter) Install(ctx context.Context) (err error) {
    65  
    66  	var metricControllerOptions []controller.Option
    67  	if m.opts.collectPeriod > 0 {
    68  		metricControllerOptions = append(
    69  			metricControllerOptions,
    70  			controller.WithCollectPeriod(m.opts.collectPeriod),
    71  		)
    72  	}
    73  
    74  	if m.opts.PushExporterBuilder != nil {
    75  		exporter, err := m.createPushExporter(ctx)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		metricControllerOptions = append(metricControllerOptions, controller.WithExporter(exporter))
    80  
    81  	}
    82  
    83  	c := controller.New(
    84  		processor.NewFactory(
    85  			simple.NewWithHistogramDistribution(),
    86  			aggregation.CumulativeTemporalitySelector(),
    87  			processor.WithMemory(true),
    88  		),
    89  		metricControllerOptions...,
    90  	)
    91  	if m.opts.PullExporterBuilder != nil {
    92  		_, err = m.createPullExporter(ctx, c)
    93  		if err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	err = c.Start(ctx)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	global.SetMeterProvider(c)
   104  
   105  	return nil
   106  }
   107  
   108  func (m *Meter) createPushExporter(ctx context.Context) (export.Exporter, error) {
   109  	if m.opts.PushExporterBuilder == nil {
   110  		return nil, fmt.Errorf("push metric exporter builder is nil")
   111  	}
   112  
   113  	return m.opts.PushExporterBuilder.Build(ctx)
   114  }
   115  
   116  // Pull Exporter supports Prometheus pulls.  It does not implement the
   117  // sdk/export/metric.Exporter interface--instead it creates a pull
   118  // controller and reads the latest checkpointed data on-scrape.
   119  func (m *Meter) createPullExporter(ctx context.Context, c *controller.Controller,
   120  ) (aggregation.TemporalitySelector, error) {
   121  	if m.opts.PullExporterBuilder == nil {
   122  		return nil, fmt.Errorf("pull metric exporter builder is nil")
   123  	}
   124  
   125  	return m.opts.PullExporterBuilder.Build(ctx, c)
   126  }