Skip to content

Commit

Permalink
Use rewritten import path for Prometheus to avoid dep conflicts (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
robskillington authored May 17, 2018
1 parent 6f12159 commit 51e5c45
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 21 deletions.
26 changes: 12 additions & 14 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import:
version: ~3.1.0
subpackages:
- statsd
- package: github.com/prometheus/client_golang
version: ^0.8.0
- package: github.com/m3db/prometheus_client_golang
version: 8ae269d24972b8695572fa6b2e3718b5ea82d6b4
subpackages:
- prometheus
- package: github.com/m3db/prometheus_client_model
version: fa8ad6fec33561be4280a8f0514318c79d7f6cb6
- package: github.com/m3db/prometheus_common
version: 25aaa3dff79bb48116615ebe1dea6a494b74ce77
- package: github.com/m3db/prometheus_procfs
version: 1878d9fbb537119d24b21ca07effd591627cd160
- package: github.com/apache/thrift
version: 9549b25c77587b29be4e0b5c258221a4ed85d37a
subpackages:
Expand Down
2 changes: 1 addition & 1 deletion m3/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

var (
// DefaultSanitizerOpts are the options for the default M3 Sanitizer
// DefaultSanitizerOpts are the options for the default M3 sanitizer.
DefaultSanitizerOpts = tally.SanitizeOptions{
NameCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Expand Down
121 changes: 121 additions & 0 deletions prometheus/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package prometheus

import (
"net/http"
"strings"
)

// Configuration is a configuration for a Prometheus reporter.
type Configuration struct {
// HandlerPath if specified will be used instead of using the default
// HTTP handler path "/metrics".
HandlerPath string `yaml:"handlerPath"`

// ListenAddress if specified will be used instead of just registering the
// handler on the default HTTP serve mux without listening.
ListenAddress string `yaml:"listenAddress"`

// TimerType is the default Prometheus type to use for Tally timers.
TimerType string `yaml:"timerType"`

// DefaultHistogramBuckets if specified will set the default histogram
// buckets to be used by the reporter.
DefaultHistogramBuckets []HistogramObjective `yaml:"defaultHistogramBuckets"`

// DefaultSummaryObjectives if specified will set the default summary
// objectives to be used by the reporter.
DefaultSummaryObjectives []SummaryObjective `yaml:"defaultSummaryObjectives"`
}

// HistogramObjective is a Prometheus histogram bucket.
// See: https://godoc.org/github.com/prometheus/client_golang/prometheus#HistogramOpts
type HistogramObjective struct {
Upper float64 `yaml:"upper"`
}

// SummaryObjective is a Prometheus summary objective.
// See: https://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts
type SummaryObjective struct {
Percentile float64 `yaml:"percentile"`
AllowedError float64 `yaml:"allowedError"`
}

// ConfigurationOptions allows some error callbacks to be registered.
type ConfigurationOptions struct {
OnError func(e error)
onRegisterError func(e error)
}

// NewReporter creates a new M3 reporter from this configuration.
func (c Configuration) NewReporter(
configOpts ConfigurationOptions,
) (Reporter, error) {
var opts Options
opts.OnRegisterError = configOpts.onRegisterError

switch c.TimerType {
case "summary":
opts.DefaultTimerType = SummaryTimerType
case "histogram":
opts.DefaultTimerType = HistogramTimerType
}

if len(c.DefaultHistogramBuckets) > 0 {
var values []float64
for _, value := range c.DefaultHistogramBuckets {
values = append(values, value.Upper)
}
opts.DefaultHistogramBuckets = values
}

if len(c.DefaultSummaryObjectives) > 0 {
values := make(map[float64]float64)
for _, value := range c.DefaultSummaryObjectives {
values[value.Percentile] = value.AllowedError
}
opts.DefaultSummaryObjectives = values
}

reporter := NewReporter(opts)

path := "/metrics"
if handlerPath := strings.TrimSpace(c.HandlerPath); handlerPath != "" {
path = handlerPath
}

if addr := strings.TrimSpace(c.ListenAddress); addr == "" {
http.Handle(path, reporter.HTTPHandler())
} else {
mux := http.NewServeMux()
mux.Handle(path, reporter.HTTPHandler())
go func() {
if err := http.ListenAndServe(addr, mux); err != nil {
if configOpts.OnError != nil {
configOpts.OnError(err)
}
}
}()
}

return reporter, nil
}
4 changes: 2 additions & 2 deletions prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (

"github.com/uber-go/tally"

prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
prom "github.com/m3db/prometheus_client_golang/prometheus"
"github.com/m3db/prometheus_client_golang/prometheus/promhttp"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions prometheus/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"testing"
"time"

prom "github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
prom "github.com/m3db/prometheus_client_golang/prometheus"
dto "github.com/m3db/prometheus_client_model/go"
"github.com/uber-go/tally"

"github.com/stretchr/testify/assert"
Expand Down
44 changes: 44 additions & 0 deletions prometheus/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package prometheus

import (
"github.com/uber-go/tally"
)

var (
// DefaultSanitizerOpts are the options for the default Prometheus sanitizer.
DefaultSanitizerOpts = tally.SanitizeOptions{
NameCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: tally.UnderscoreDashCharacters,
},
KeyCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: tally.UnderscoreDashCharacters,
},
ValueCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: tally.UnderscoreDashCharacters,
},
ReplacementCharacter: tally.DefaultReplacementCharacter,
}
)

0 comments on commit 51e5c45

Please sign in to comment.