-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from AH-dark/feat/options
Tracer configuration using options
- Loading branch information
Showing
2 changed files
with
154 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2021 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package prometheus | ||
|
||
import ( | ||
"net/http" | ||
|
||
prom "github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
) | ||
|
||
var defaultBuckets = []float64{5000, 10000, 25000, 50000, 100000, 250000, 500000, 1000000} | ||
|
||
// Option opts for monitor prometheus | ||
type Option interface { | ||
apply(cfg *config) | ||
} | ||
|
||
type option func(cfg *config) | ||
|
||
func (fn option) apply(cfg *config) { | ||
fn(cfg) | ||
} | ||
|
||
type config struct { | ||
buckets []float64 | ||
enableGoCollector bool | ||
runtimeMetricRules []collectors.GoRuntimeMetricsRule | ||
registry *prom.Registry | ||
serveMux *http.ServeMux | ||
disableServer bool | ||
} | ||
|
||
func defaultConfig() *config { | ||
return &config{ | ||
buckets: defaultBuckets, | ||
enableGoCollector: false, | ||
runtimeMetricRules: []collectors.GoRuntimeMetricsRule{}, | ||
registry: prom.NewRegistry(), | ||
serveMux: http.DefaultServeMux, | ||
disableServer: false, | ||
} | ||
} | ||
|
||
// WithEnableGoCollector enable go collector | ||
func WithEnableGoCollector(enable bool) Option { | ||
return option(func(cfg *config) { | ||
cfg.enableGoCollector = enable | ||
}) | ||
} | ||
|
||
// WithGoCollectorRule define your custom go collector rule | ||
func WithGoCollectorRule(rules ...collectors.GoRuntimeMetricsRule) Option { | ||
return option(func(cfg *config) { | ||
cfg.runtimeMetricRules = rules | ||
}) | ||
} | ||
|
||
// WithHistogramBuckets define your custom histogram buckets base on your biz | ||
func WithHistogramBuckets(buckets []float64) Option { | ||
return option(func(cfg *config) { | ||
if len(buckets) > 0 { | ||
cfg.buckets = buckets | ||
} | ||
}) | ||
} | ||
|
||
// WithRegistry define your custom registry | ||
func WithRegistry(registry *prom.Registry) Option { | ||
return option(func(cfg *config) { | ||
if registry != nil { | ||
cfg.registry = registry | ||
} | ||
}) | ||
} | ||
|
||
// WithServeMux define your custom serve mux | ||
func WithServeMux(serveMux *http.ServeMux) Option { | ||
return option(func(cfg *config) { | ||
if serveMux != nil { | ||
cfg.serveMux = serveMux | ||
} | ||
}) | ||
} | ||
|
||
// WithDisableServer disable prometheus server | ||
func WithDisableServer(disable bool) Option { | ||
return option(func(cfg *config) { | ||
cfg.disableServer = disable | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters