Skip to content
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

Added 'query' parameter to metricbeats global configuration #8292

Conversation

sayden
Copy link
Contributor

@sayden sayden commented Sep 12, 2018

Initial PR on mb package which fixes issue #8286 but I guess it will be better to implement in the HTTP helper. The thing is that the URI parsing of a metricbeat occurs in the mb package so to implement it outside of it could lead to ugly-and-not-so-predictable code

I've tested in local successfully but I haven't found a test that already covers Build() method.

Ideas welcome :)

@sayden sayden requested review from jsoriano and exekias September 12, 2018 16:07
@sayden sayden added Metricbeat Metricbeat in progress Pull request is currently in progress. labels Sep 12, 2018
@sayden sayden force-pushed the feature/add-query-configuration-field-to-metricfields-configuration branch from 67a9524 to 272c74b Compare September 12, 2018 16:52
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for identifying and fixing this issue, it mostly LGTM, could you add a test case to TestURLHostParserBuilder test in url_test.go that would have failed without this change?

@jsoriano
Copy link
Member

Oh, and add a reference to this setting in metricbeat/docs/metricbeat-options.asciidoc

@exekias
Copy link
Contributor

exekias commented Sep 13, 2018

Nice! this looks like the right solution 🎉 Please add also a changelog entry. I'm adding the needs_backport label as this will need to be backported to 6.x at least

@exekias exekias added the needs_backport PR is waiting to be backported to other branches. label Sep 13, 2018
@sayden
Copy link
Contributor Author

sayden commented Sep 13, 2018

I have been digging into the code and I found a way to implement it within the HTTP helper instead that on the mb package. I don't know what do you think but I think it's a bit less intrusive than to put it directly into the mb (all in all, it's very HTTP specific and maybe it wouldn't make sense for a module that uses UDP or RPC).

What do you think?

@jsoriano
Copy link
Member

jsoriano commented Sep 13, 2018

@sayden but is the HTTPHelper affected by this issue? I mean, if HTTPHelper receives a correctly built HostData, does it have any issue with query params?
If not, I think that we should solve it in the parts that build an incorrect HostData, as seems to be in URLHostParserBuilder.

@sayden
Copy link
Contributor Author

sayden commented Sep 13, 2018

HTTPHelper is indirectly affected because it's the URLHostParserBuilder the one which always receives an empty query. As soon as no query params are present in the host, this line https://github.com/elastic/beats/blob/master/metricbeat/helper/http.go#L94 won't receive an escaped uri.

In this function, https://github.com/elastic/beats/blob/master/metricbeat/helper/http.go#L102 the http.Request can be modified to include query params too.

I really don't have a strong preference so... as you wish.

@sayden
Copy link
Contributor Author

sayden commented Sep 13, 2018

Ok, after a quick chat with @jsoriano I'm going with the base Metricset option

Copy link
Contributor

@exekias exekias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revisited this code now, I'm wondering if this could get fixed from NewHostDataFromURL, as there you get access to the url.URL object, which contains the RawQuery

func (q QueryParams) String() (s string) {
for k, v := range q {
if v == nil {
v = "null"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why setting null?, I think URL params support empty values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure I have to admit but Go was setting literally <nil> so I thought that null was a bit more universal.

I have just checked it and it seems that empty values are supported. Thanks for the thumbs up!

// String returns the values in common query params format (key=value&key2=value2) which is the way that the url
// package expects this params (without the initial '?')
func (q QueryParams) String() (s string) {
for k, v := range q {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use url.Values for this

@@ -38,3 +38,7 @@
example: elasticsearch
description: >
Name of the service metricbeat fetches the data from.

- name: query
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fields.yml spec is used to define the index template for the data we push: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

I believe we are not reporting the query as for this change, so we don't need to configure a mapping for it

@sayden
Copy link
Contributor Author

sayden commented Sep 14, 2018

Ok, as requested by @exekias I'm gonna improve the code with the suggestions.

I have also checked that the contents of a url query param can also be an array (with syntax array[]=value1&array[]=value2 or array=value1&array=value2. I have never seen it but it is actually possible.

I have also checked that the implementation can't be done in NewHostDataFromURL because the RawQuery field is set only if you have set previously the QueryParams field in URLHostParserBuilder. It will arrive empty if not.

Edit: Added a one more common way to set array in the query. It seems it's not standarized at all

@sayden
Copy link
Contributor Author

sayden commented Sep 14, 2018

I was actually wondering if it is better to proceed with a string value for the query configuration field or use the YAML format to build a query like in the following example:

metricbeat.modules:
- module: http
  metricsets:
   - json
   - server
  period: 1s
  hosts: ["localhost:8080"]
  namespace: "json_namespace"
  path: "/home"
  query:
    key: value
    key2: 11
    key3: 12.3
    key4: true
    key5: -4
    ar:
    - ar1
    - ar2
    - ar3
    ar2:
    - 13
    - -14.3
    - -15
    - 33
output.console:
  pretty: true

Instead of

query: "ar=ar1&ar=ar2&ar=ar3&ar2=13&ar2=-14.300000&ar2=-15&ar2=33&key=value&key2=11&key3=12.300000&key4=true&key5=-4"

What do you guys think @exekias @jsoriano ?

@exekias
Copy link
Contributor

exekias commented Sep 17, 2018

That looks better to me, wdyt @jsoriano?

@jsoriano
Copy link
Member

It looks good to me too.

// specific language governing permissions and limitations
// under the License.

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name

// specific language governing permissions and limitations
// under the License.

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name


// +build ignore

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name

// specific language governing permissions and limitations
// under the License.

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name

// specific language governing permissions and limitations
// under the License.

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name

// specific language governing permissions and limitations
// under the License.

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name

// specific language governing permissions and limitations
// under the License.

package add_process_metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use an underscore in package name

type Reporter interface {
Stop()
}

type ReporterFactory func(beat.Info, *common.Config) (Reporter, error)
type ReporterFactory func(beat.Info, Settings, *common.Config) (Reporter, error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ReporterFactory should have comment or be unexported

@@ -30,11 +30,15 @@ type config struct {
Reporter common.ConfigNamespace `config:",inline"`
}

type Settings struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type Settings should have comment or be unexported

@sayden sayden force-pushed the feature/add-query-configuration-field-to-metricfields-configuration branch from 20d672c to e0a17eb Compare September 19, 2018 15:50
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a minor detail about a function name.

metricbeat/mb/mb_test.go Outdated Show resolved Hide resolved
metricbeat/mb/parse/url_test.go Show resolved Hide resolved
@jsoriano
Copy link
Member

Oh, @sayden, and don't forget about adding a reference to this new setting in metricbeat/docs/metricbeat-options.asciidoc

}

byt, _ := json.MarshalIndent(u, "", " ")
fmt.Println(string(byt), u.Encode())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you want to remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG, yes, sorry about this.

@sayden
Copy link
Contributor Author

sayden commented Sep 26, 2018

Documentation added aaaand conflict in Changelog solved.

[float]
==== `query`

An optional value to pass common query params in YAML. Instead of setting the query params within hosts values using the syntax `?key=value&key2&value2`, you can set it here like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line could be wrapped with similar length as the others in the file.

list:
- 1.1
- 2.95
- -15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

---- missing after the code snippet?

@@ -136,6 +136,7 @@ https://github.com/elastic/beats/compare/v6.4.0...master[Check the HEAD diff]
- Allow TCP helper to support delimiters and graphite module to accept multiple metrics in a single payload. {pull}8278[8278]
- Added 'died' PID state to process_system metricset on system module{pull}8275[8275]
- Added `ccr` metricset to Elasticsearch module. {pull}8335[8335]
- Added support for query params in configuration {pull}8286[8286]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the referenced issue here is the bug report, but the tag is for pull. You can reference any of them here, or both, but use pull for the pull request and issue for the issue. Usually the PR is always referenced and sometimes also the issue.

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for addressing all requested changes. Take a look to failures in CI builds.

@sayden
Copy link
Contributor Author

sayden commented Sep 26, 2018

Travis error seems like a couchbase error. I think we are fine

======================================================================
FAIL: couchbase metricsets tests [with metricset='bucket']
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/parameterized/parameterized.py", line 392, in standalone_func
    return func(*(a + p.args), **p.kwargs)
  File "/go/src/github.com/elastic/beats/metricbeat/tests/system/test_couchbase.py", line 22, in test_couchbase
    self.check_metricset("couchbase", metricset, self.get_hosts(), self.FIELDS)
  File "/go/src/github.com/elastic/beats/metricbeat/tests/system/metricbeat.py", line 93, in check_metricset
    self.assertItemsEqual(self.de_dot(fields), evt.keys())
AssertionError: Element counts were not equal:
First has 1, Second has 0:  'couchbase'
First has 0, Second has 1:  u'error'
-------------------- >> begin captured stdout << ---------------------
{u'beat': {u'hostname': u'2b6ecb6891b9', u'name': u'2b6ecb6891b9', u'version': u'7.0.0-alpha1'}, u'@timestamp': u'2018-09-26T19:44:48.091Z', u'host': {u'name': u'2b6ecb6891b9'}, u'error': {u'message': u'HTTP error 401 in bucket: 401 Unauthorized'}, u'metricset': {u'rtt': 10277, u'host': u'couchbase:8091', u'name': u'bucket', u'module': u'couchbase'}}

@sayden sayden merged commit 7dbb4f1 into elastic:master Sep 27, 2018
sayden added a commit to sayden/beats that referenced this pull request Sep 27, 2018
…8292)

Added 'query' parameter to metricbeats global configuration

(cherry picked from commit 7dbb4f1)
@sayden sayden added v6.5.0 and removed needs_backport PR is waiting to be backported to other branches. labels Sep 27, 2018
sayden added a commit that referenced this pull request Sep 28, 2018
…al configuration (#8463)

Added 'query' parameter to metricbeats global configuration (#8292)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in progress Pull request is currently in progress. Metricbeat Metricbeat v6.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants