2
0

client.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "opentelemetry/ext/http/client/http_client_factory.h"
  4. #include "opentelemetry/ext/http/common/url_parser.h"
  5. #include "opentelemetry/semconv/http_attributes.h"
  6. #include "opentelemetry/semconv/url_attributes.h"
  7. #include "tracer_common.h"
  8. namespace
  9. {
  10. using namespace opentelemetry::trace;
  11. namespace http_client = opentelemetry::ext::http::client;
  12. namespace context = opentelemetry::context;
  13. namespace nostd = opentelemetry::nostd;
  14. namespace semconv = opentelemetry::semconv;
  15. void sendRequest(const std::string &url)
  16. {
  17. auto http_client = http_client::HttpClientFactory::CreateSync();
  18. // start active span
  19. StartSpanOptions options;
  20. options.kind = SpanKind::kClient; // client
  21. opentelemetry::ext::http::common::UrlParser url_parser(url);
  22. std::string span_name = url_parser.path_;
  23. auto span = get_tracer("http-client")
  24. ->StartSpan(span_name,
  25. {{semconv::url::kUrlFull, url_parser.url_},
  26. {semconv::url::kUrlScheme, url_parser.scheme_},
  27. {semconv::http::kHttpRequestMethod, "GET"}},
  28. options);
  29. auto scope = get_tracer("http-client")->WithActiveSpan(span);
  30. // inject current context into http header
  31. auto current_ctx = context::RuntimeContext::GetCurrent();
  32. HttpTextMapCarrier<http_client::Headers> carrier;
  33. auto prop = context::propagation::GlobalTextMapPropagator::GetGlobalPropagator();
  34. prop->Inject(carrier, current_ctx);
  35. // send http request
  36. http_client::Result result = http_client->GetNoSsl(url, carrier.headers_);
  37. if (result)
  38. {
  39. // set span attributes
  40. auto status_code = result.GetResponse().GetStatusCode();
  41. span->SetAttribute(semconv::http::kHttpResponseStatusCode, status_code);
  42. result.GetResponse().ForEachHeader(
  43. [&span](nostd::string_view header_name, nostd::string_view header_value) {
  44. span->SetAttribute("http.header." + std::string(header_name.data()), header_value);
  45. return true;
  46. });
  47. if (status_code >= 400)
  48. {
  49. span->SetStatus(StatusCode::kError);
  50. }
  51. }
  52. else
  53. {
  54. span->SetStatus(
  55. StatusCode::kError,
  56. "Response Status :" +
  57. std::to_string(
  58. static_cast<typename std::underlying_type<http_client::SessionState>::type>(
  59. result.GetSessionState())));
  60. }
  61. // end span and export data
  62. span->End();
  63. }
  64. } // namespace
  65. int main(int argc, char *argv[])
  66. {
  67. InitTracer();
  68. constexpr char default_host[] = "localhost";
  69. constexpr char default_path[] = "/helloworld";
  70. constexpr uint16_t default_port = 8800;
  71. uint16_t port;
  72. // The port the validation service listens to can be specified via the command line.
  73. if (argc > 1)
  74. {
  75. port = static_cast<uint16_t>(atoi(argv[1]));
  76. }
  77. else
  78. {
  79. port = default_port;
  80. }
  81. std::string url = "http://" + std::string(default_host) + ":" + std::to_string(port) +
  82. std::string(default_path);
  83. sendRequest(url);
  84. CleanupTracer();
  85. return 0;
  86. }