-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Prometheus exporter (#3239)
This change will automatically register a created exporter with a prometheus registerer, if none are provided it will use prometheus' DefaultRegisterer. Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
- Loading branch information
1 parent
1e72af4
commit b6a22ab
Showing
8 changed files
with
170 additions
and
56 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
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
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
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
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,60 @@ | ||
// Copyright The OpenTelemetry 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 "go.opentelemetry.io/otel/exporters/prometheus" | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewConfig(t *testing.T) { | ||
registry := prometheus.NewRegistry() | ||
|
||
testCases := []struct { | ||
name string | ||
options []Option | ||
wantRegisterer prometheus.Registerer | ||
}{ | ||
{ | ||
name: "Default", | ||
options: nil, | ||
wantRegisterer: prometheus.DefaultRegisterer, | ||
}, | ||
|
||
{ | ||
name: "WithRegisterer", | ||
options: []Option{ | ||
WithRegisterer(registry), | ||
}, | ||
wantRegisterer: registry, | ||
}, | ||
{ | ||
name: "nil options do nothing", | ||
options: []Option{ | ||
WithRegisterer(nil), | ||
}, | ||
wantRegisterer: prometheus.DefaultRegisterer, | ||
}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cfg := newConfig(tt.options...) | ||
|
||
assert.Equal(t, tt.wantRegisterer, cfg.registerer) | ||
}) | ||
} | ||
} |
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,59 @@ | ||
// Copyright The OpenTelemetry 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 "go.opentelemetry.io/otel/exporters/prometheus" | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// config contains options for the exporter. | ||
type config struct { | ||
registerer prometheus.Registerer | ||
} | ||
|
||
// newConfig creates a validated config configured with options. | ||
func newConfig(opts ...Option) config { | ||
cfg := config{} | ||
for _, opt := range opts { | ||
cfg = opt.apply(cfg) | ||
} | ||
|
||
if cfg.registerer == nil { | ||
cfg.registerer = prometheus.DefaultRegisterer | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
// Option sets exporter option values. | ||
type Option interface { | ||
apply(config) config | ||
} | ||
|
||
type optionFunc func(config) config | ||
|
||
func (fn optionFunc) apply(cfg config) config { | ||
return fn(cfg) | ||
} | ||
|
||
// WithRegisterer configures which prometheus Registerer the Exporter will | ||
// register with. If no registerer is used the prometheus DefaultRegisterer is | ||
// used. | ||
func WithRegisterer(reg prometheus.Registerer) Option { | ||
return optionFunc(func(cfg config) config { | ||
cfg.registerer = reg | ||
return cfg | ||
}) | ||
} |
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
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