-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metricbeat: Add configuration for RabbitMQ management prefix path #7074
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
The RabbitMQ module uses http://www.rabbitmq.com/management.html[HTTP API] created by the management plugin to collect metrics. | ||
|
||
The default metricsets are `connection`, `node` and `queue`. | ||
The default metricsets are `connection`, `node`, `queue` and `exchange`. | ||
|
||
If `management.path_prefix` is set in RabbitMQ configuration, `management_path_prefix` has to be set to the same value in this module configuration. |
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 |
---|---|---|
|
@@ -3,57 +3,31 @@ package exchange | |
import ( | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/common/cfgwarn" | ||
"github.com/elastic/beats/metricbeat/helper" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/mb/parse" | ||
"github.com/elastic/beats/metricbeat/module/rabbitmq" | ||
) | ||
|
||
const ( | ||
defaultScheme = "http" | ||
defaultPath = "/api/exchanges" | ||
) | ||
|
||
var ( | ||
hostParser = parse.URLHostParserBuilder{ | ||
DefaultScheme: defaultScheme, | ||
DefaultPath: defaultPath, | ||
}.Build() | ||
) | ||
|
||
// init registers the MetricSet with the central registry as soon as the program | ||
// starts. The New function will be called later to instantiate an instance of | ||
// the MetricSet for each host defined in the module's configuration. After the | ||
// MetricSet has been created then Fetch will begin to be called periodically. | ||
func init() { | ||
if err := mb.Registry.AddMetricSet("rabbitmq", "exchange", New, hostParser); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we miss that in our PR bonanza or this came in later? |
||
panic(err) | ||
} | ||
mb.Registry.MustAddMetricSet("rabbitmq", "exchange", New, | ||
mb.WithHostParser(rabbitmq.HostParser), | ||
mb.DefaultMetricSet(), | ||
) | ||
} | ||
|
||
// MetricSet holds any configuration or state information. It must implement | ||
// the mb.MetricSet interface. And this is best achieved by embedding | ||
// mb.BaseMetricSet because it implements all of the required mb.MetricSet | ||
// interface methods except for Fetch. | ||
// MetricSet for fetching RabbitMQ exchanges metrics. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
*helper.HTTP | ||
*rabbitmq.MetricSet | ||
} | ||
|
||
// New creates a new instance of the MetricSet. New is responsible for unpacking | ||
// any MetricSet specific configuration options if there are any. | ||
// New creates new instance of MetricSet | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
cfgwarn.Beta("The rabbitmq exchange metricset is beta") | ||
|
||
http, err := helper.NewHTTP(base) | ||
ms, err := rabbitmq.NewMetricSet(base, rabbitmq.ExchangesPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
http.SetHeader("Accept", "application/json") | ||
|
||
return &MetricSet{ | ||
base, | ||
http, | ||
}, nil | ||
return &MetricSet{ms}, nil | ||
} | ||
|
||
// Fetch methods implements the data gathering and data conversion to the right | ||
|
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 |
---|---|---|
|
@@ -14,13 +14,21 @@ import ( | |
) | ||
|
||
func TestFetchEventContents(t *testing.T) { | ||
absPath, err := filepath.Abs("../_meta/testdata/") | ||
absPath, _ := filepath.Abs("../_meta/testdata/") | ||
|
||
response, err := ioutil.ReadFile(absPath + "/exchange_sample_response.json") | ||
response, _ := ioutil.ReadFile(absPath + "/exchange_sample_response.json") | ||
notFound, _ := ioutil.ReadFile(absPath + "/notfound_response.json") | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json;") | ||
w.Write([]byte(response)) | ||
switch r.URL.Path { | ||
case "/api/exchanges": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json;") | ||
w.Write([]byte(response)) | ||
default: | ||
w.WriteHeader(404) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, I should steal this for my elasticsearch http tests. |
||
w.Header().Set("Content-Type", "application/json;") | ||
w.Write([]byte(notFound)) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
|
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,29 @@ | ||
package rabbitmq | ||
|
||
import ( | ||
"github.com/elastic/beats/metricbeat/helper" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
) | ||
|
||
// MetricSet can be used to build other metric sets that query RabbitMQ | ||
// management plugin | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
*helper.HTTP | ||
} | ||
|
||
// NewMetricSet creates an metric set that can be used to build other metric | ||
// sets that query RabbitMQ management plugin | ||
func NewMetricSet(base mb.BaseMetricSet, subPath string) (*MetricSet, error) { | ||
http, err := helper.NewHTTP(base) | ||
if err != nil { | ||
return nil, err | ||
} | ||
http.SetURI(http.GetURI() + subPath) | ||
http.SetHeader("Accept", "application/json") | ||
|
||
return &MetricSet{ | ||
base, | ||
http, | ||
}, nil | ||
} |
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,63 @@ | ||
package rabbitmq | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
mbtest "github.com/elastic/beats/metricbeat/mb/testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
mb.Registry.MustAddMetricSet("rabbitmq", "test", newTestMetricSet, | ||
mb.WithHostParser(HostParser), | ||
) | ||
} | ||
|
||
type testMetricSet struct { | ||
*MetricSet | ||
} | ||
|
||
func newTestMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
ms, err := NewMetricSet(base, "/api/test") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &testMetricSet{ms}, nil | ||
} | ||
|
||
// Fetch makes an HTTP request to fetch connections metrics from the connections endpoint. | ||
func (m *testMetricSet) Fetch() ([]common.MapStr, error) { | ||
_, err := m.HTTP.FetchContent() | ||
return nil, err | ||
} | ||
|
||
func TestManagementPathPrefix(t *testing.T) { | ||
visited := false | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/management_prefix/api/test": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json;") | ||
visited = true | ||
default: | ||
w.WriteHeader(404) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
config := map[string]interface{}{ | ||
"module": "rabbitmq", | ||
"metricsets": []string{"test"}, | ||
"hosts": []string{server.URL}, | ||
pathConfigKey: "/management_prefix", | ||
} | ||
|
||
f := mbtest.NewEventsFetcher(t, config) | ||
f.Fetch() | ||
assert.True(t, visited) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a assert.NoError( for these erors. This will make debugging test much easier.