Skip to content

Commit

Permalink
Adding query APIs for metricsets and modules from metricbeat registry
Browse files Browse the repository at this point in the history
  • Loading branch information
vjsamuel committed Apr 25, 2017
1 parent fafe088 commit d53a0aa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
- Add new MetricSet interfaces for developers (`Closer`, `ReportingFetcher`, and `PushMetricSet`). {pull}3908[3908]
- Add kubelet module {pull}3916[3916]
- Add dropwizard module {pull}4022[4022]
- Adding query APIs for metricsets and modules from metricbeat registry {pull}4102[4102]

*Packetbeat*
- Add `fields` and `fields_under_root` to packetbeat protocols configurations. {pull}3518[3518]
Expand Down
38 changes: 37 additions & 1 deletion metricbeat/mb/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"strings"
"sync"

"github.com/elastic/beats/libbeat/logp"
)
Expand Down Expand Up @@ -46,6 +47,8 @@ type metricSetFactoryInfo struct {
// Register contains the factory functions for creating new Modules and new
// MetricSets.
type Register struct {
//Lock to control concurrent read/writes
sync.RWMutex
// A map of module name to ModuleFactory.
modules map[string]ModuleFactory
// A map of module name to nested map of MetricSet name to metricSetFactoryInfo.
Expand Down Expand Up @@ -144,9 +147,42 @@ func (r *Register) metricSetFactory(module, name string) (MetricSetFactory, Host
return info.factory, info.hostParser, nil
}

//Modules returns the list of module names that are registered
func (r *Register) Modules() []string {
r.RLock()
defer r.RUnlock()

modules := []string{}
for module := range r.modules {
modules = append(modules, module)
}

return modules
}

//MetricSets returns the list of metricsets registered for a given module
func (r *Register) MetricSets(module string) []string {
r.RLock()
defer r.RUnlock()

metricsets := []string{}

sets, ok := r.metricSets[module]
if ok {
for name := range sets {
metricsets = append(metricsets, name)
}
}

return metricsets
}

// String return a string representation of the registered ModuleFactory's and
// MetricSetFactory's.
func (r Register) String() string {
func (r *Register) String() string {
r.RLock()
defer r.RUnlock()

var modules []string
for module := range r.modules {
modules = append(modules, module)
Expand Down
24 changes: 24 additions & 0 deletions metricbeat/mb/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,27 @@ func TestMetricSetFactory(t *testing.T) {
assert.NotNil(t, hp) // Can't compare functions in Go so just check for non-nil.
})
}

func TestMetricSetQuery(t *testing.T) {
registry := NewRegister()
err := registry.AddMetricSet(moduleName, metricSetName, fakeMetricSetFactory)
if err != nil {
t.Fatal(err)
}

metricsets := registry.MetricSets(moduleName)
assert.Equal(t, len(metricsets), 1)
assert.Equal(t, metricsets[0], metricSetName)

metricsets = registry.MetricSets("foo")
assert.Equal(t, len(metricsets), 0)
}

func TestModuleQuery(t *testing.T) {
registry := NewRegister()
registry.modules[moduleName] = fakeModuleFactory

modules := registry.Modules()
assert.Equal(t, len(modules), 1)
assert.Equal(t, modules[0], moduleName)
}

0 comments on commit d53a0aa

Please sign in to comment.