MCPcopy Create free account
hub / github.com/Icinga/icinga2 / SendRequest

Method SendRequest

lib/perfdata/elasticsearchwriter.cpp:454–598  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

452}
453
454void ElasticsearchWriter::SendRequest(const String& body)
455{
456 namespace beast = boost::beast;
457 namespace http = beast::http;
458
459 Url::Ptr url = new Url();
460
461 url->SetScheme(GetEnableTls() ? "https" : "http");
462 url->SetHost(GetHost());
463 url->SetPort(GetPort());
464
465 std::vector<String> path;
466
467 /* Specify the index path. Best practice is a daily rotation.
468 * Example: http://localhost:9200/icinga2-2017.09.11?pretty=1
469 */
470 path.emplace_back(GetIndex() + "-" + Utility::FormatDateTime("%Y.%m.%d", Utility::GetTime()));
471
472 /* Use the bulk message format. */
473 path.emplace_back("_bulk");
474
475 url->SetPath(path);
476
477 OptionalTlsStream stream;
478
479 try {
480 stream = Connect();
481 } catch (const std::exception& ex) {
482 Log(LogWarning, "ElasticsearchWriter")
483 << "Flush failed, cannot connect to Elasticsearch: " << DiagnosticInformation(ex, false);
484 return;
485 }
486
487 Defer s ([&stream]() {
488 if (stream.first) {
489 stream.first->next_layer().shutdown();
490 }
491 });
492
493 http::request<http::string_body> request (http::verb::post, std::string(url->Format(true)), 10);
494
495 request.set(http::field::user_agent, "Icinga/" + Application::GetAppVersion());
496 request.set(http::field::host, url->GetHost() + ":" + url->GetPort());
497
498 /* Specify required headers by Elasticsearch. */
499 request.set(http::field::accept, "application/json");
500
501 /* Use application/x-ndjson for bulk streams. While ES
502 * is able to handle application/json, the newline separator
503 * causes problems with Logstash (#6609).
504 */
505 request.set(http::field::content_type, "application/x-ndjson");
506
507 /* Send authentication if configured. */
508 String username = GetUsername();
509 String password = GetPassword();
510
511 if (!username.IsEmpty() && !password.IsEmpty())

Callers

nothing calls this directly

Calls 12

LogClass · 0.85
SetSchemeMethod · 0.80
SetHostMethod · 0.80
SetPortMethod · 0.80
SetPathMethod · 0.80
GetPortMethod · 0.80
ConnectFunction · 0.50
FormatMethod · 0.45
GetHostMethod · 0.45
IsEmptyMethod · 0.45
sizeMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected