1 Star 0 Fork 0

Clannad/webkit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
otelmetric_option.go 3.16 KB
一键复制 编辑 原始数据 按行查看 历史
yokaqa 提交于 2024-10-18 10:10 . add
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package otelmetric
import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
)
// newProviderConfigByOptions returns a config configured with options.
func newProviderConfigByOptions(options []Option) providerConfig {
conf := providerConfig{}
for _, o := range options {
conf = o.apply(conf)
}
return conf
}
// Option applies a configuration option value to a MeterProvider.
type Option interface {
apply(providerConfig) providerConfig
}
// optionFunc applies a set of options to a config.
type optionFunc func(providerConfig) providerConfig
// apply returns a config with option(s) applied.
func (o optionFunc) apply(conf providerConfig) providerConfig {
return o(conf)
}
// providerConfig is the configuration for Provider.
type providerConfig struct {
viewOption metric.Option
readerOption metric.Option
resourceOption metric.Option
enabledBuiltInMetrics bool
}
// IsBuiltInMetricsEnabled returns whether the builtin metrics is enabled.
func (cfg providerConfig) IsBuiltInMetricsEnabled() bool {
return cfg.enabledBuiltInMetrics
}
// MetricOptions converts and returns the providerConfig as metrics options.
func (cfg providerConfig) MetricOptions() []metric.Option {
var metricOptions = make([]metric.Option, 0)
if cfg.viewOption != nil {
metricOptions = append(metricOptions, cfg.viewOption)
}
if cfg.readerOption != nil {
metricOptions = append(metricOptions, cfg.readerOption)
}
if cfg.resourceOption != nil {
metricOptions = append(metricOptions, cfg.resourceOption)
}
return metricOptions
}
// WithBuiltInMetrics enables builtin metrics.
func WithBuiltInMetrics() Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.enabledBuiltInMetrics = true
return cfg
})
}
// WithResource associates a Resource with a MeterProvider. This Resource
// represents the entity producing telemetry and is associated with all Meters
// the MeterProvider will create.
func WithResource(res *resource.Resource) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.resourceOption = metric.WithResource(res)
return cfg
})
}
// WithReader associates Reader r with a MeterProvider.
//
// By default, if this option is not used, the MeterProvider will perform no
// operations; no data will be exported without a Reader.
func WithReader(reader metric.Reader) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
if reader == nil {
return cfg
}
cfg.readerOption = metric.WithReader(reader)
return cfg
})
}
// WithView associates views a MeterProvider.
//
// Views are appended to existing ones in a MeterProvider if this option is
// used multiple times.
//
// By default, if this option is not used, the MeterProvider will use the
// default view.
func WithView(views ...metric.View) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.viewOption = metric.WithView(views...)
return cfg
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/clannad_sk/webkit.git
git@gitee.com:clannad_sk/webkit.git
clannad_sk
webkit
webkit
997199055094

搜索帮助

0d507c66 1850385 C8b1a773 1850385