mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-11-04 03:02:26 -06:00 
			
		
		
		
	
		
			
	
	
		
			100 lines
		
	
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			100 lines
		
	
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								// Copyright The OpenTelemetry Authors
							 | 
						||
| 
								 | 
							
								// SPDX-License-Identifier: Apache-2.0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"time"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									"go.opentelemetry.io/otel"
							 | 
						||
| 
								 | 
							
									"go.opentelemetry.io/otel/metric"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// config contains optional settings for reporting runtime metrics.
							 | 
						||
| 
								 | 
							
								type config struct {
							 | 
						||
| 
								 | 
							
									// MinimumReadMemStatsInterval sets the minimum interval
							 | 
						||
| 
								 | 
							
									// between calls to runtime.ReadMemStats().  Negative values
							 | 
						||
| 
								 | 
							
									// are ignored.
							 | 
						||
| 
								 | 
							
									MinimumReadMemStatsInterval time.Duration
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// MeterProvider sets the metric.MeterProvider.  If nil, the global
							 | 
						||
| 
								 | 
							
									// Provider will be used.
							 | 
						||
| 
								 | 
							
									MeterProvider metric.MeterProvider
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// Option supports configuring optional settings for runtime metrics.
							 | 
						||
| 
								 | 
							
								type Option interface {
							 | 
						||
| 
								 | 
							
									apply(*config)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// ProducerOption supports configuring optional settings for runtime metrics using a
							 | 
						||
| 
								 | 
							
								// metric producer in addition to standard instrumentation.
							 | 
						||
| 
								 | 
							
								type ProducerOption interface {
							 | 
						||
| 
								 | 
							
									Option
							 | 
						||
| 
								 | 
							
									applyProducer(*config)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// DefaultMinimumReadMemStatsInterval is the default minimum interval
							 | 
						||
| 
								 | 
							
								// between calls to runtime.ReadMemStats().  Use the
							 | 
						||
| 
								 | 
							
								// WithMinimumReadMemStatsInterval() option to modify this setting in
							 | 
						||
| 
								 | 
							
								// Start().
							 | 
						||
| 
								 | 
							
								const DefaultMinimumReadMemStatsInterval time.Duration = 15 * time.Second
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// WithMinimumReadMemStatsInterval sets a minimum interval between calls to
							 | 
						||
| 
								 | 
							
								// runtime.ReadMemStats(), which is a relatively expensive call to make
							 | 
						||
| 
								 | 
							
								// frequently.  This setting is ignored when `d` is negative.
							 | 
						||
| 
								 | 
							
								func WithMinimumReadMemStatsInterval(d time.Duration) Option {
							 | 
						||
| 
								 | 
							
									return minimumReadMemStatsIntervalOption(d)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								type minimumReadMemStatsIntervalOption time.Duration
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (o minimumReadMemStatsIntervalOption) apply(c *config) {
							 | 
						||
| 
								 | 
							
									if o >= 0 {
							 | 
						||
| 
								 | 
							
										c.MinimumReadMemStatsInterval = time.Duration(o)
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (o minimumReadMemStatsIntervalOption) applyProducer(c *config) { o.apply(c) }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// WithMeterProvider sets the Metric implementation to use for
							 | 
						||
| 
								 | 
							
								// reporting.  If this option is not used, the global metric.MeterProvider
							 | 
						||
| 
								 | 
							
								// will be used.  `provider` must be non-nil.
							 | 
						||
| 
								 | 
							
								func WithMeterProvider(provider metric.MeterProvider) Option {
							 | 
						||
| 
								 | 
							
									return metricProviderOption{provider}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								type metricProviderOption struct{ metric.MeterProvider }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (o metricProviderOption) apply(c *config) {
							 | 
						||
| 
								 | 
							
									if o.MeterProvider != nil {
							 | 
						||
| 
								 | 
							
										c.MeterProvider = o.MeterProvider
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// newConfig computes a config from the supplied Options.
							 | 
						||
| 
								 | 
							
								func newConfig(opts ...Option) config {
							 | 
						||
| 
								 | 
							
									c := config{
							 | 
						||
| 
								 | 
							
										MeterProvider: otel.GetMeterProvider(),
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
									for _, opt := range opts {
							 | 
						||
| 
								 | 
							
										opt.apply(&c)
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
									if c.MinimumReadMemStatsInterval <= 0 {
							 | 
						||
| 
								 | 
							
										c.MinimumReadMemStatsInterval = DefaultMinimumReadMemStatsInterval
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
									return c
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// newConfig computes a config from the supplied ProducerOptions.
							 | 
						||
| 
								 | 
							
								func newProducerConfig(opts ...ProducerOption) config {
							 | 
						||
| 
								 | 
							
									c := config{}
							 | 
						||
| 
								 | 
							
									for _, opt := range opts {
							 | 
						||
| 
								 | 
							
										opt.applyProducer(&c)
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
									if c.MinimumReadMemStatsInterval <= 0 {
							 | 
						||
| 
								 | 
							
										c.MinimumReadMemStatsInterval = DefaultMinimumReadMemStatsInterval
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
									return c
							 | 
						||
| 
								 | 
							
								}
							 |