### Initialize Tally Scope with Options Source: https://github.com/uber-go/tally/blob/master/README.md Demonstrates how to create a root scope for tally metrics collection. It requires a custom reporter, tags for metadata, and a reporting interval. The returned scope and closer are essential for managing the lifecycle of the metrics collection. ```go reporter := NewMyStatsReporter() // Implement as you will tags := map[string]string{ "dc": "east-1", "type": "master", } reportEvery := time.Second scope := tally.NewRootScope(tally.ScopeOptions{ Tags: tags, Reporter: reporter, }, reportEvery) ``` -------------------------------- ### Create and Use Tally Metrics (Counter, Gauge) Source: https://github.com/uber-go/tally/blob/master/README.md Illustrates how to obtain and interact with different metric types provided by tally. This includes incrementing a counter and updating a gauge. It's recommended to cache metric objects for efficiency. ```go // Get a counter, increment a counter reqCounter := scope.Counter("requests") // cache me reqCounter.Inc(1) queueGauge := scope.Gauge("queue_length") // cache me queueGauge.Update(42) ``` -------------------------------- ### Tally Prometheus Reporter Options Configuration (Go) Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Defines the configurable options for the Prometheus reporter, including the Prometheus registry, default timer types, histogram buckets, summary objectives, and error handling for metric registration. ```go type Options struct { // Registerer is the prometheus registerer to register // metrics with. Use nil to specify the default registerer. Registerer prom.Registerer // DefaultTimerType is the default type timer type to create // when using timers. It's default value is a histogram timer type. DefaultTimerType TimerType // DefaultHistogramBuckets is the default histogram buckets // to use. Use nil to specify the default histogram buckets. DefaultHistogramBuckets []float64 // DefaultSummaryObjectives is the default summary objectives // to use. Use nil to specify the default summary objectives. DefaultSummaryObjectives map[float64]float64 // OnRegisterError defines a method to call to when registering // a metric with the registerer fails. Use nil to specify // to panic by default when registering a metric fails. OnRegisterError func(err error) } ``` -------------------------------- ### Tally Performance Benchmarks Source: https://github.com/uber-go/tally/blob/master/README.md This section presents performance benchmarks for various Tally operations, including Counter increments, reporting metrics with and without data, Gauge sets, and Timer operations. The benchmarks highlight the efficiency of Tally's design, showing low nanoseconds per operation for many core functions, which is crucial for high-throughput metric collection. ```text BenchmarkCounterInc-8 200000000 7.68 ns/op BenchmarkReportCounterNoData-8 300000000 4.88 ns/op BenchmarkReportCounterWithData-8 100000000 21.6 ns/op BenchmarkGaugeSet-8 100000000 16.0 ns/op BenchmarkReportGaugeNoData-8 100000000 10.4 ns/op BenchmarkReportGaugeWithData-8 50000000 27.6 ns/op BenchmarkTimerInterval-8 50000000 37.7 ns/op BenchmarkTimerReport-8 300000000 5.69 ns/op ``` -------------------------------- ### Configure Statsd Reporter with Tally Scope Source: https://github.com/uber-go/tally/blob/master/README.md This Go code snippet demonstrates how to configure and initialize a Tally root scope with a statsd reporter. It utilizes `go-statsd-client` and `tally/statsd` to send metrics to a statsd endpoint. Ensure the statsd client is properly configured with host, port, and buffer settings. The scope is initialized with a service prefix and tags. ```go import ( io "github.com/cactus/go-statsd-client/v5/statsd" "github.com/uber-go/tally" tallystatsd "github.com/uber-go/tally/statsd" // ... ) func newScope() (tally.Scope, io.Closer) { statter, _ := statsd.NewBufferedClient("127.0.0.1:8125", "stats", 100*time.Millisecond, 1440) reporter := tallystatsd.NewReporter(statter, tallystatsd.Options{ SampleRate: 1.0, }) sphere, closer := tally.NewRootScope(tally.ScopeOptions{ Prefix: "my-service", Tags: map[string]string{}, Reporter: reporter, }, time.Second) return scope, closer } ``` -------------------------------- ### Tally Prometheus Register Timer Options (Go) Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Specifies options for registering timers on demand within the Prometheus reporter. Allows customization of timer type, histogram buckets, and summary objectives. Passing nil for options uses reporter defaults. ```go type RegisterTimerOptions struct { TimerType TimerType HistogramBuckets []float64 SummaryObjectives map[float64]float64 } ``` -------------------------------- ### Register Timer Options Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Options specifically for registering timers, allowing customization of timer type, histogram buckets, and summary objectives. ```APIDOC ## Register Timer Options ### Description Provides options to customize the behavior when registering a timer on demand using `RegisterTimer`. Allows specifying the `TimerType`, histogram buckets, or summary objectives. ### Fields - **TimerType TimerType**: The type of timer to register (SummaryTimerType or HistogramTimerType). - **HistogramBuckets []float64**: Specific buckets to use if `TimerType` is `HistogramTimerType`. If nil, default buckets are used. - **SummaryObjectives map[float64]float64**: Specific objectives to use if `TimerType` is `SummaryTimerType`. If nil, default objectives are used. ### Usage Pass an instance of this struct to the `RegisterTimer` method to override default timer settings for a specific metric. ### Example ```go import ( "github.com/uber-go/tally/prometheus" ) customTimerOpts := &prometheus.RegisterTimerOptions{ TimerType: prometheus.HistogramTimerType, HistogramBuckets: []float64{0.001, 0.01, 0.1, 1.0, 5.0, 10.0}, } // When calling RegisterTimer: // reporter.RegisterTimer("my_custom_timer", []string{}, "A timer with custom histogram buckets.", customTimerOpts) ``` ``` -------------------------------- ### Reporter Options Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Configuration options for the Prometheus reporter, allowing customization of the registry, default timer types, and error handling. ```APIDOC ## Reporter Options ### Description Options for configuring the `tally` Prometheus reporter. Allows specifying a custom Prometheus registry, default timer types (summary or histogram), default histogram buckets or summary objectives, and a callback for registration errors. ### Fields - **Registerer prom.Registerer**: The Prometheus registerer to use. If nil, the default registerer is used. - **DefaultTimerType TimerType**: The default timer type to use when creating timers (SummaryTimerType or HistogramTimerType). Defaults to HistogramTimerType. - **DefaultHistogramBuckets []float64**: Default buckets to use for histogram timers. If nil, default buckets are used. - **DefaultSummaryObjectives map[float64]float64**: Default objectives to use for summary timers. If nil, default objectives are used. - **OnRegisterError func(err error)**: A function to call when registering a metric fails. If nil, the reporter will panic on registration errors. ### Request Example ```go import ( "github.com/prometheus/client_golang/prometheus" "github.com/uber-go/tally/prometheus" ) // Create a custom registry myRegistry := prometheus.NewRegistry() // Configure reporter options reporterOpts := prometheus.Options{ Registerer: myRegistry, DefaultTimerType: prometheus.SummaryTimerType, } // Create reporter with options reporter := prometheus.NewReporter(reporterOpts) ``` ### Response Example ```go // The Options struct itself is used for configuration, no direct response. ``` ``` -------------------------------- ### Tally StatsReporter Interface Definition Source: https://github.com/uber-go/tally/blob/master/README.md This Go code defines the `StatsReporter` interface, which serves as the backend for Tally scopes to report various types of metrics. It extends `BaseStatsReporter` and includes methods for reporting counters, gauges, timers, and histogram samples. Implementations of this interface are responsible for forwarding metric data to a storage or aggregation system. The `Capabilities` method should describe the reporter's features like tagging and active reporting. ```go // BaseStatsReporter implements the shared reporter methods. type BaseStatsReporter interface { Capabilities() Capabilities Flush() } // StatsReporter is a backend for Scopes to report metrics to. type StatsReporter interface { BaseStatsReporter // ReportCounter reports a counter value ReportCounter( name string, tags map[string]string, value int64, ) // ReportGauge reports a gauge value ReportGauge( name string, tags map[string]string, value float64, ) // ReportTimer reports a timer value ReportTimer( name string, tags map[string]string, interval time.Duration, ) // ReportHistogramValueSamples reports histogram samples for a bucket ReportHistogramValueSamples( name string, tags map[string]string, buckets Buckets, bucketLowerBound, bucketUpperBound float64, samples int64, ) // ReportHistogramDurationSamples reports histogram samples for a bucket ReportHistogramDurationSamples( name string, tags map[string]string, buckets Buckets, bucketLowerBound, bucketUpperBound time.Duration, samples int64, ) } ``` -------------------------------- ### StatsD Reporter Options in Go Source: https://github.com/uber-go/tally/blob/master/statsd/README.md Defines the Options struct for configuring the tally reporter. It includes a SampleRate field to control the metrics emission rate, defaulting to 1 if not specified. ```go package reporter // Options is a set of options for the tally reporter. type Options struct { // SampleRate is the metrics emission sample rate. If you // do not set this value it will be set to 1. SampleRate float32 } ``` -------------------------------- ### Tally Scope Interface Definition Source: https://github.com/uber-go/tally/blob/master/README.md This Go code defines the `Scope` interface, which is central to the Tally metrics library. It provides methods for creating and accessing different metric types (Counter, Gauge, Timer, Histogram) and for creating child scopes with additional tags or name prefixes. The `Histogram` method offers flexibility in defining value and duration buckets using various predefined or custom bucket configurations. The `Capabilities` method returns information about the reporter's features. ```go type Scope interface { // Counter returns the Counter object corresponding to the name. Counter(name string) Counter // Gauge returns the Gauge object corresponding to the name. Gauge(name string) Gauge // Timer returns the Timer object corresponding to the name. Timer(name string) Timer // Histogram returns the Histogram object corresponding to the name. // To use default value and duration buckets configured for the scope // simply pass tally.DefaultBuckets or nil. // You can use tally.ValueBuckets{x, y, ...} for value buckets. // You can use tally.DurationBuckets{x, y, ...} for duration buckets. // You can use tally.MustMakeLinearValueBuckets(start, width, count) for linear values. // You can use tally.MustMakeLinearDurationBuckets(start, width, count) for linear durations. // You can use tally.MustMakeExponentialValueBuckets(start, factor, count) for exponential values. // You can use tally.MustMakeExponentialDurationBuckets(start, factor, count) for exponential durations. Histogram(name string, buckets Buckets) Histogram // Tagged returns a new child scope with the given tags and current tags. Tagged(tags map[string]string) Scope // SubScope returns a new child scope appending a further name prefix. SubScope(name string) Scope // Capabilities returns a description of metrics reporting capabilities. Capabilities() Capabilities } // Capabilities is a description of metrics reporting capabilities. type Capabilities interface { // Reporting returns whether the reporter has the ability to actively report. Reporting() bool // Tagging returns whether the reporter has the capability for tagged metrics. Tagging() bool } ``` -------------------------------- ### Tally Prometheus Reporter Interface Definition (Go) Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Defines the interface for the Prometheus-backed Tally reporter. It includes methods for accessing the HTTP handler, registering metrics (counters, gauges, timers) with descriptions, and extends the tally.CachedStatsReporter interface. ```go type Reporter interface { tally.CachedStatsReporter // HTTPHandler provides the Prometheus HTTP scrape handler. HTTPHandler() http.Handler // RegisterCounter is a helper method to initialize a counter // in the Prometheus backend with a given help text. // If not called explicitly, the Reporter will create one for // you on first use, with a not super helpful HELP string. RegisterCounter( name string, tagKeys []string, desc string, ) (*prom.CounterVec, error) // RegisterGauge is a helper method to initialize a gauge // in the prometheus backend with a given help text. // If not called explicitly, the Reporter will create one for // you on first use, with a not super helpful HELP string. RegisterGauge( name string, tagKeys []string, desc string, ) (*prom.GaugeVec, error) // RegisterTimer is a helper method to initialize a timer // summary or histogram vector in the prometheus backend // with a given help text. // If not called explicitly, the Reporter will create one for // you on first use, with a not super helpful HELP string. // You may pass opts as nil to get the default timer type // and objectives/buckets. // You may also pass objectives/buckets as nil in opts to // get the default objectives/buckets for the specified // timer type. RegisterTimer( name string, tagKeys []string, desc string, opts *RegisterTimerOptions, ) (TimerUnion, error) } ``` -------------------------------- ### Reporter Interface Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md The Reporter interface for the Prometheus backed tally reporter, including methods for accessing the HTTP handler and pre-registering metrics. ```APIDOC ## Reporter Interface ### Description The Reporter interface provides Prometheus-backed stats reporting for tally, including an HTTP handler for Prometheus scraping and methods to pre-register metrics with custom help text. ### Methods - **HTTPHandler() http.Handler**: Returns the Prometheus HTTP scrape handler. - **RegisterCounter(name string, tagKeys []string, desc string) (*prom.CounterVec, error)**: Registers a Prometheus CounterVec with a given name, tag keys, and help description. If not called explicitly, a default one is created on first use. - **RegisterGauge(name string, tagKeys []string, desc string) (*prom.GaugeVec, error)**: Registers a Prometheus GaugeVec with a given name, tag keys, and help description. If not called explicitly, a default one is created on first use. - **RegisterTimer(name string, tagKeys []string, desc string, opts *RegisterTimerOptions) (TimerUnion, error)**: Registers a Prometheus Timer (summary or histogram) with a given name, tag keys, and help description. Allows specifying timer type and objectives/buckets via `RegisterTimerOptions`. If not called explicitly, a default one is created on first use. ### Request Example ```go // Example of using RegisterCounter reporter.RegisterCounter("http_requests_total", []string{"method", "path"}, "Total number of HTTP requests received.") // Example of using RegisterTimer reporter.RegisterTimer("request_duration_seconds", []string{"endpoint"}, "Latency of HTTP requests.", nil) ``` ### Response Example ```go // Success Response for RegisterCounter // Returns a *prom.CounterVec // Success Response for RegisterTimer // Returns a TimerUnion (which can be a *prom.SummaryVec or *prom.HistogramVec) ``` ``` -------------------------------- ### Tally Prometheus Timer Types Definition (Go) Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Enumerates the different types of timers supported by the Prometheus reporter: SummaryTimerType and HistogramTimerType. These types determine how timer metrics are reported. ```go type TimerType int const ( // SummaryTimerType is a timer type that reports into a summary SummaryTimerType TimerType = iota // HistogramTimerType is a timer type that reports into a histogram HistogramTimerType ) ``` -------------------------------- ### Timer Type Constants Source: https://github.com/uber-go/tally/blob/master/prometheus/README.md Defines the available types for timers in the Prometheus reporter: SummaryTimerType and HistogramTimerType. ```APIDOC ## Timer Type Constants ### Description Constants that define the type of timer to be used for reporting metrics to Prometheus. Timers can be configured as either summaries or histograms. ### Constants - **SummaryTimerType TimerType**: Represents a timer that reports metrics using Prometheus summaries. - **HistogramTimerType TimerType**: Represents a timer that reports metrics using Prometheus histograms. ### Usage These constants are used within the `Options` struct and `RegisterTimerOptions` struct to specify the desired timer behavior. ### Example ```go // Setting the default timer type to SummaryTimerType options := prometheus.Options{ DefaultTimerType: prometheus.SummaryTimerType, } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.