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

Add support for RFC7231 methods to http monitors #41975

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]

- Added status to monitor run log report.
- Upgrade node to latest LTS v18.20.3. {pull}40038[40038]
- Add support for RFC7231 methods to http monitors. {pull}41975[41975]

*Metricbeat*

Expand Down
4 changes: 2 additions & 2 deletions heartbeat/docs/monitors/monitor-http.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ Example configuration:

Under `check.request`, specify these options:

*`method`*:: The HTTP method to use. Valid values are `"HEAD"`, `"GET"`, `"POST"` and
`"OPTIONS"`.
*`method`*:: The HTTP method to use. Valid values are `"HEAD"`, `"GET"`, `"POST"`, `"PUT"`, `"DELETE"`, `"CONNECT"`,
`"TRACE"` and `"OPTIONS"`.
*`headers`*:: A dictionary of additional HTTP headers to send. By default heartbeat
will set the 'User-Agent' header to identify itself.
*`body`*:: Optional request body content.
Expand Down
3 changes: 2 additions & 1 deletion heartbeat/monitors/active/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (r *responseConfig) Validate() error {
// Validate validates of the requestParameters object is valid or not
func (r *requestParameters) Validate() error {
switch strings.ToUpper(r.Method) {
case "HEAD", "GET", "POST", "OPTIONS":
case http.MethodOptions, http.MethodHead, http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodConnect, http.MethodTrace:
default:
return fmt.Errorf("HTTP method '%v' not supported", r.Method)
}
Expand Down
32 changes: 22 additions & 10 deletions heartbeat/tests/system/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class BaseTest(TestCase):
def setUpClass(self):
self.beat_name = "heartbeat"
self.beat_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../"))
os.path.join(os.path.dirname(__file__), "../../")
)
super(BaseTest, self).setUpClass()

def start_server(self, content, status_code, **kwargs):
class HTTPHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(status_code)
self.send_header('Content-Type', 'application/json')
self.send_header("Content-Type", "application/json")
self.end_headers()
if "write_delay" in kwargs:
sleep(float(kwargs["write_delay"]))
Expand All @@ -30,16 +31,19 @@ def do_GET(self):
class HTTPHandlerEnabledOPTIONS(HTTPHandler):
def do_OPTIONS(self):
self.send_response(status_code)
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'HEAD, GET, POST, OPTIONS')
self.send_header("Access-Control-Allow-Credentials", "true")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header(
"Access-Control-Allow-Methods",
"HEAD, GET, POST, OPTIONS, PUT, DELETE, CONNECT, TRACE",
)
self.end_headers()

# initialize http server based on if it needs to support OPTIONS method
server = http.server.HTTPServer(('localhost', 0), HTTPHandler)
server = http.server.HTTPServer(("localhost", 0), HTTPHandler)
# setup enable_options_method as False if it's not set
if kwargs.get("enable_options_method", False):
server = http.server.HTTPServer(('localhost', 0), HTTPHandlerEnabledOPTIONS)
server = http.server.HTTPServer(("localhost", 0), HTTPHandlerEnabledOPTIONS)

thread = threading.Thread(target=server.serve_forever)
thread.start()
Expand All @@ -54,7 +58,11 @@ def http_cfg(id, url):
schedule: "@every 1s"
timeout: 3s
urls: ["{url}"]
"""[1:-1].format(id=id, url=url)
"""[
1:-1
].format(
id=id, url=url
)

@staticmethod
def tcp_cfg(*hosts):
Expand All @@ -64,13 +72,17 @@ def tcp_cfg(*hosts):
schedule: "@every 1s"
timeout: 3s
hosts: [{host_str}]
"""[1:-1].format(host_str=host_str)
"""[
1:-1
].format(
host_str=host_str
)

def last_output_line(self):
return self.read_output()[-1]

def write_dyn_config(self, filename, cfg):
with open(self.monitors_dir() + filename, 'w') as f:
with open(self.monitors_dir() + filename, "w") as f:
f.write(cfg)

def monitors_dir(self):
Expand Down
Loading