Skip to content

Commit

Permalink
Http module improvements (#4170)
Browse files Browse the repository at this point in the history
This is a follow up PR for #4156

* Add environment with small golang web service
* Remove not needed body fields
* Add defaults to configs
* Add system tests
* Update config with all config options
  • Loading branch information
ruflin authored and monicasarbu committed May 4, 2017
1 parent 158ded2 commit c1132fa
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 54 deletions.
5 changes: 5 additions & 0 deletions metricbeat/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ services:
- ${PWD}/module/dropwizard/_meta/env
- ${PWD}/module/elasticsearch/_meta/env
- ${PWD}/module/haproxy/_meta/env
- ${PWD}/module/http/_meta/env
- ${PWD}/module/jolokia/_meta/env
- ${PWD}/module/kafka/_meta/env
- ${PWD}/module/kibana/_meta/env
Expand All @@ -46,6 +47,7 @@ services:
dropwizard: { condition: service_healthy }
elasticsearch: { condition: service_healthy }
haproxy: { condition: service_healthy }
http: { condition: service_healthy }
jolokia: { condition: service_healthy }
kafka: { condition: service_healthy }
kibana: { condition: service_healthy }
Expand Down Expand Up @@ -80,6 +82,9 @@ services:
haproxy:
build: ${PWD}/module/haproxy/_meta

http:
build: ${PWD}/module/http/_meta

jolokia:
build: ${PWD}/module/jolokia/_meta

Expand Down
11 changes: 1 addition & 10 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -3446,7 +3446,7 @@ The average queue time in ms over the last 1024 requests.
[[exported-fields-http]]
== http Fields
== HTTP Fields
http Module
Expand Down Expand Up @@ -3526,15 +3526,6 @@ The HTTP payload received
json metricset
[float]
=== http.json.body
type: keyword
The HTTP payload received
[[exported-fields-jolokia]]
== Jolokia Fields
Expand Down
17 changes: 11 additions & 6 deletions metricbeat/docs/modules/http.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ This file is generated! See scripts/docs_collector.py
[[metricbeat-module-http]]
== http Module

Http module is a [Metricbeat](https://www.elastic.co/products/beats/metricbeat) used to call arbitrary HTTP endpoints for which not a dedicated metricbeat is available.
Multiple endpoints can be configured which are polled in a regular interval and the result is shipped to the configured output channel.
Http module is a [Metricbeat](https://www.elastic.co/products/beats/metricbeat) module used to call arbitrary HTTP endpoints for which a dedicated metricbeat module is not available.

Multiple endpoints can be configured which are polled in a regular interval and the result is shipped to the configured output channel. It is recommended to install a metricbeat instance on each host from which data should be fetched.

Httpbeat is inspired by the Logstash [http_poller](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http_poller.html) input filter but doesn't require that the endpoint is reachable by Logstash as the Metricbeat module pushes the data to the configured output channels, e.g. Logstash or Elasticsearch.
This is often necessary in security restricted network setups, where Logstash is not able to reach all servers. Instead the server to be monitored itself has Metricbeat installed and can send the data or a collector server has Metricbeat installed which is deployed in the secured network environment and can reach all servers to be monitored.
Expand All @@ -15,7 +16,7 @@ This is often necessary in security restricted network setups, where Logstash is
[float]
=== Example Configuration

The http module supports the standard configuration options that are described
The HTTP module supports the standard configuration options that are described
in <<configuration-metricbeat>>. Here is an example configuration:

[source,yaml]
Expand All @@ -25,9 +26,13 @@ metricbeat.modules:
metricsets: ["json"]
enabled: false
period: 10s
hosts: ["httpbin.org"]
namespace: "http_json_namespace"
path: "/headers"
hosts: ["localhost:80"]
namespace: "json_namespace"
path: "/"
#body: ""
#method: "GET"
#request.enabled: false
#response.enabled: false
----

[float]
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/docs/modules_list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This file is generated! See scripts/docs_collector.py
* <<metricbeat-module-elasticsearch,elasticsearch>>
* <<metricbeat-module-golang,golang>>
* <<metricbeat-module-haproxy,HAProxy>>
* <<metricbeat-module-http,http>>
* <<metricbeat-module-http,HTTP>>
* <<metricbeat-module-jolokia,Jolokia>>
* <<metricbeat-module-kafka,kafka>>
* <<metricbeat-module-kibana,kibana>>
Expand Down
12 changes: 8 additions & 4 deletions metricbeat/metricbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,18 @@ metricbeat.modules:
period: 10s
hosts: ["tcp://127.0.0.1:14567"]

#-------------------------------- http Module --------------------------------
#-------------------------------- HTTP Module --------------------------------
- module: http
metricsets: ["json"]
enabled: false
period: 10s
hosts: ["httpbin.org"]
namespace: "http_json_namespace"
path: "/headers"
hosts: ["localhost:80"]
namespace: "json_namespace"
path: "/"
#body: ""
#method: "GET"
#request.enabled: false
#response.enabled: false

#------------------------------- Jolokia Module ------------------------------
- module: jolokia
Expand Down
15 changes: 7 additions & 8 deletions metricbeat/module/http/_meta/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Tomcat is started to fetch Jolokia metrics from it
FROM jolokia/java-jolokia:7
ENV TOMCAT_VERSION 7.0.55
ENV TC apache-tomcat-${TOMCAT_VERSION}
FROM golang:1.8.1

HEALTHCHECK CMD curl -f curl localhost:8778/jolokia/
EXPOSE 8778
RUN wget http://archive.apache.org/dist/tomcat/tomcat-7/v${TOMCAT_VERSION}/bin/${TC}.tar.gz
RUN tar xzf ${TC}.tar.gz -C /opt
COPY test/main.go main.go

CMD env CATALINA_OPTS=$(jolokia_opts) /opt/${TC}/bin/catalina.sh run
EXPOSE 8080

HEALTHCHECK CMD curl -f curl localhost:8080/

CMD go run main.go
10 changes: 7 additions & 3 deletions metricbeat/module/http/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
metricsets: ["json"]
enabled: false
period: 10s
hosts: ["httpbin.org"]
namespace: "http_json_namespace"
path: "/headers"
hosts: ["localhost:80"]
namespace: "json_namespace"
path: "/"
#body: ""
#method: "GET"
#request.enabled: false
#response.enabled: false
5 changes: 3 additions & 2 deletions metricbeat/module/http/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
== http Module

Http module is a [Metricbeat](https://www.elastic.co/products/beats/metricbeat) used to call arbitrary HTTP endpoints for which not a dedicated metricbeat is available.
Multiple endpoints can be configured which are polled in a regular interval and the result is shipped to the configured output channel.
Http module is a [Metricbeat](https://www.elastic.co/products/beats/metricbeat) module used to call arbitrary HTTP endpoints for which a dedicated metricbeat module is not available.

Multiple endpoints can be configured which are polled in a regular interval and the result is shipped to the configured output channel. It is recommended to install a metricbeat instance on each host from which data should be fetched.

Httpbeat is inspired by the Logstash [http_poller](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http_poller.html) input filter but doesn't require that the endpoint is reachable by Logstash as the Metricbeat module pushes the data to the configured output channels, e.g. Logstash or Elasticsearch.
This is often necessary in security restricted network setups, where Logstash is not able to reach all servers. Instead the server to be monitored itself has Metricbeat installed and can send the data or a collector server has Metricbeat installed which is deployed in the secured network environment and can reach all servers to be monitored.
4 changes: 2 additions & 2 deletions metricbeat/module/http/_meta/env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
JOLOKIA_HOST=jolokia
JOLOKIA_PORT=8778
HTTP_HOST=http
HTTP_PORT=8080
2 changes: 1 addition & 1 deletion metricbeat/module/http/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- key: http
title: "http"
title: "HTTP"
description: >
http Module
fields:
Expand Down
19 changes: 19 additions & 0 deletions metricbeat/module/http/_meta/test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"log"
"net/http"
)

func main() {
http.HandleFunc("/", serve)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

func serve(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"hello":"world"}`)
}
23 changes: 15 additions & 8 deletions metricbeat/module/http/json/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ This is the json metricset of the module http.

The JSON structure returned by the HTTP endpoint will be added to the provided `namespace` field as shown in the following example:

```json
[source,json]
----
{
"@timestamp": "2017-05-01T13:00:24.745Z",
"beat": {
Expand All @@ -31,16 +32,18 @@ The JSON structure returned by the HTTP endpoint will be added to the provided `
},
"type": "metricsets"
}
```
----

Here the response from `date.jsontest.com` is returned in the configured `http_json_namespace` namespace:
```json

[source,json]
----
{
"date": "05-01-2017",
"milliseconds_since_epoch": 1493643625474.000000,
"time": "01:00:25 PM"
}
```
----

It is required to set a namespace in the general module config section.

Expand All @@ -53,7 +56,9 @@ With this configuration enabled additional information about the request are inc
* Body/Payload

Example:
```json

[source,json]
----
{
"@timestamp": "2017-05-01T13:00:24.745Z",
"beat": {
Expand Down Expand Up @@ -84,7 +89,7 @@ Example:
},
"type": "metricsets"
}
```
----

[float]
==== response.enabled
Expand All @@ -94,7 +99,9 @@ With this configuration enabled additional information about the response are in
* HTTP Status Code

Example:
```json

[source,json]
----
{
"@timestamp": "2017-05-01T13:00:24.745Z",
"beat": {
Expand Down Expand Up @@ -129,7 +136,7 @@ Example:
},
"type": "metricsets"
}
```
----


[float]
Expand Down
4 changes: 0 additions & 4 deletions metricbeat/module/http/json/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
description: >
json metricset
fields:
- name: body
type: keyword
description: >
The HTTP payload received
7 changes: 6 additions & 1 deletion metricbeat/module/http/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
Body string `config:"body"`
RequestEnabled bool `config:"request.enabled"`
ResponseEnabled bool `config:"response.enabled"`
}{}
}{
Method: "GET",
Body: "",
RequestEnabled: false,
ResponseEnabled: false,
}

if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions metricbeat/module/http/json/json_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func getConfig() map[string]interface{} {
"module": "http",
"metricsets": []string{"json"},
"hosts": []string{getEnvHost() + ":" + getEnvPort()},
"path": "/jolokia/?ignoreErrors=true&canonicalNaming=false",
"path": "/",
"namespace": "testnamespace",
}
}

func getEnvHost() string {
host := os.Getenv("JOLOKIA_HOST")
host := os.Getenv("HTTP_HOST")

if len(host) == 0 {
host = "127.0.0.1"
Expand All @@ -48,10 +48,10 @@ func getEnvHost() string {
}

func getEnvPort() string {
port := os.Getenv("JOLOKIA_PORT")
port := os.Getenv("HTTP_PORT")

if len(port) == 0 {
port = "8778"
port = "8080"
}
return port
}
46 changes: 46 additions & 0 deletions metricbeat/tests/system/test_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import metricbeat
import unittest
from nose.plugins.attrib import attr

HTTP_FIELDS = metricbeat.COMMON_FIELDS + ["http"]


class Test(metricbeat.BaseTest):

@unittest.skipUnless(metricbeat.INTEGRATION_TESTS, "integration test")
def test_json(self):
"""
http json metricset test
"""
self.render_config_template(modules=[{
"name": "http",
"metricsets": ["json"],
"hosts": self.get_hosts(),
"period": "5s",
"namespace": "test",
}])
proc = self.start_beat()
self.wait_until(lambda: self.output_lines() > 0)
proc.check_kill_and_wait()

# Ensure no errors or warnings exist in the log.
log = self.get_log()
self.assertNotRegexpMatches(log.replace("WARN The http json metricset is in beta.", ""), "ERR|WARN")

output = self.read_output_json()
self.assertEqual(len(output), 1)
evt = output[0]

assert evt["http"]["test"]["hello"] == "world"

# Delete dynamic namespace part for fields comparison
del evt["http"]["test"]

self.assertItemsEqual(self.de_dot(HTTP_FIELDS), evt.keys(), evt)

self.assert_fields_are_documented(evt)

def get_hosts(self):
return ["http://" + os.getenv('HTTP_HOST', 'localhost') + ':' +
os.getenv('HTTP_PORT', '8080')]

0 comments on commit c1132fa

Please sign in to comment.