forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Zpages example #16
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
Open
kmanghat
wants to merge
52
commits into
data-aggregator-modifications
Choose a base branch
from
zpages-example
base: data-aggregator-modifications
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Zpages example #16
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
81be8e2
Add base HTTP server files
jajanet 7e89c87
Add index error status/tooltip description, precise timestamps
jajanet 6fe0d38
Move refresh + time to top right
jajanet f315a19
Change html double quotes to single quotes
jajanet beac08a
Add working emitted HTML file
jajanet 5b74c3e
Merge remote-tracking branch 'origin/master' into http-server
jajanet e297076
Remove handler
jajanet 0390b2a
Replace static files with emitted c++
jajanet 336593b
Autoformat. No functional changes. (#223)
g-easy 2d1cdea
Fix format on CI. (#222)
g-easy 9195cf7
Add shasum for grpc and format file. (#221)
g-easy 4b85762
Add other zpages ex, working emitted static files
jajanet 4e2aeee
Context helper functions (#225)
satac2 41af3ac
Add Counter and Histogram Aggregators (#178)
ankit-bhargava 4caf0bb
Add MinMaxSumCount and Gauge Aggregators (#181)
Brandon-Kimberly e73dffa
Add Exact Aggregator (#198)
Brandon-Kimberly 97df587
Fix Metrics SDK CMake tests (#230)
Brandon-Kimberly d24c740
Send variant attributes, cleanup, resolve review comments
jajanet 7470070
Merge resolved review comments, attributes json
jajanet c1b7ab7
Silence bazel error, unused variable
jajanet 30ae204
Merge main repo servers
jajanet 8d5e9d1
Rename file to OT naming convention
jajanet ca57282
Add derived file server
jajanet 0ba096f
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet 7e67beb
Update references to new header file names
jajanet efc5522
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet da8d9bf
Update reference
jajanet e7b4385
Add return statement in = overload
jajanet da074f8
Add return statement in = overload
jajanet 70a5cf4
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet 15b4178
pass in library_name and library_version into meter (#232)
HudsonHumphries 822230c
zPages example
kmanghat 5569837
Fixed typo
kmanghat 349cb2a
Added zpages example
kmanghat 0a35712
Run formatter
jajanet de0d4a9
Run formatter with style=google
jajanet adac7cf
Try running formatter again, default settings
jajanet 700d423
Loop fix and example update
kmanghat 379661a
Merge branch 'data-aggregator-modifications' into zpages-example
kmanghat b1c00b3
Merge
jajanet 5b02ebc
Merge branch 'zpages-example' into zpages-server-emit-static-files
jajanet 332a9dd
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet 4ff777c
Fix wrong type inputs
jajanet 160eed4
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet 9ea1145
Get files working when emitted as string
jajanet 516f3d9
Removed import
kmanghat 6162892
Turn on and render attributes correctly
jajanet ac46705
Merge branch 'zpages-example' of https://github.com/kmanghat/opentele…
jajanet a1ce2e9
Updated zPages example
kmanghat 4fcebfc
Merge master changes, formatting conflicts
jajanet 0524e69
Revert "Merge master changes, formatting conflicts"
jajanet 1a99330
Updated zpages example
kmanghat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,48 @@ | ||
| #pragma once | ||
|
|
||
| #include <fstream> | ||
| /** | ||
| * This is a basic example for zpages that helps users get familiar with how to | ||
| * use this feature in OpenTelemetery | ||
| */ | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <chrono> | ||
| #include <iostream> | ||
|
|
||
| #include "opentelemetry/ext/zpages/zpages.h" | ||
|
|
||
| using opentelemetry::core::SteadyTimestamp; | ||
| namespace nostd = opentelemetry::nostd; | ||
| using opentelemetry::v0::trace::Span; | ||
| #include "opentelemetry/ext/zpages/zpages.h" // Required file include for zpages | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
|
|
||
| /** | ||
| * The following line initializes zPages and starts a webserver at | ||
| * http://localhost:30000/tracez/ where spans that are created in the application | ||
| * can be viewed. | ||
| * Note that the webserver is destroyed after the application ends execution. | ||
| */ | ||
| zPages(); | ||
| auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(""); | ||
| std::vector<nostd::unique_ptr<Span>> running_span_container; | ||
| while (1) { | ||
| std::string span_name; | ||
| std::cout << "Enter span name or CTRL C to exit: "; | ||
| std::cin >> span_name; | ||
|
|
||
| char span_type; | ||
| std::cout << "Enter span type Error(E), Completed(C), Running(R)"; | ||
| std::cin >> span_type; | ||
|
|
||
| if(span_type == 'R'){ | ||
| running_span_container.push_back(tracer->StartSpan(span_name)); | ||
| } | ||
| else if(span_type == 'C'){ | ||
| unsigned long long int start_time; | ||
| std::cout << "Start time in nanoseconds: "; | ||
| std::cin >> start_time; | ||
|
|
||
| unsigned long long int end_time; | ||
| std::cout << "End time in nanoseconds: "; | ||
| std::cin >> end_time; | ||
|
|
||
| opentelemetry::trace::StartSpanOptions start; | ||
| start.start_steady_time = SteadyTimestamp(nanoseconds(start_time)); | ||
| opentelemetry::trace::EndSpanOptions end; | ||
| end.end_steady_time = SteadyTimestamp(nanoseconds(end_time)); | ||
| tracer->StartSpan(span_name,start)->End(end); | ||
| } else { | ||
| std::string description; | ||
| int error_code = 0; | ||
| std::cout << "Enter an error code (integer between 1-16): "; | ||
| std::cin >> error_code; | ||
| if(error_code < 1 || error_code > 16) error_code = 0; | ||
| std::cout << "Enter a span description: "; | ||
| std::cin.get(); | ||
| getline(std::cin,description); | ||
| tracer->StartSpan(span_name)->SetStatus((opentelemetry::trace::CanonicalCode)error_code, | ||
| description); | ||
| } | ||
| std::cout << "\n"; | ||
|
|
||
| std::cout << "This example for zPages creates a few types of spans and then " | ||
| << "creates a span every second for the duration of the application" | ||
| << "\n"; | ||
|
|
||
| // Create a map of attributes for the span | ||
| int listInt[] = {1, 2, 3}; | ||
| std::map<std::string, opentelemetry::common::AttributeValue> attribute_map; | ||
| attribute_map["attribute1"] = opentelemetry::nostd::span<int>(listInt); | ||
| attribute_map["attribute2"] = 314159; | ||
|
|
||
| // Create a span of each type(running, completed and error) | ||
| tracer->StartSpan("examplespan1",attribute_map)->End(); | ||
|
|
||
| tracer->StartSpan("examplespan1")->End(); | ||
| tracer->StartSpan("examplespan1")->SetStatus( | ||
| opentelemetry::trace::CanonicalCode::CANCELLED, "Cancelled example"); | ||
|
|
||
| // Create another running span with a different name | ||
| auto running_span = tracer->StartSpan("examplespan2"); | ||
|
|
||
| // Create a completed span every second till user stops the loop | ||
| std::cout << "Presss CTRL+C to stop...\n"; | ||
| while(true){ | ||
| std::this_thread::sleep_for(seconds(1)); | ||
| tracer->StartSpan("examplespan")->End(); | ||
| } | ||
| std::cout << "Presss <ENTER> to stop...\n"; | ||
| std::cin.get(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <chrono> | ||
| #include <iostream> | ||
|
|
||
| #include "opentelemetry/ext/zpages/zpages.h" | ||
|
|
||
| std::string GetRandomString() { | ||
| std::string s = ""; | ||
| const char alphanum[] = | ||
| "0123456789" | ||
| "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
| "abcdefghijklmnopqrstuvwxyz"; | ||
| for (int i = 0; i < rand() % 8 + 2; ++i) { | ||
| s += alphanum[rand() % (sizeof(alphanum) - 1)]; | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| void MakeUniqueSpans() { | ||
| auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(""); | ||
| auto span = tracer->StartSpan(GetRandomString()); | ||
| if (rand() % 5 == 0) std::this_thread::sleep_for(std::chrono::seconds(rand() % 120)); | ||
| else std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 10000000)); | ||
| span->End(); | ||
| } | ||
|
|
||
| void MakeSpans(int i) { | ||
| auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(""); | ||
| auto span = tracer->StartSpan("span" + std::to_string(i)); | ||
| if (rand() % 5 == 0) std::this_thread::sleep_for(std::chrono::seconds(rand() % 120)); | ||
| else std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 10000000)); | ||
| span->End(); | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| zPages(); | ||
| auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(""); | ||
| auto run_span = tracer->StartSpan("always running"); | ||
|
|
||
| while (1) { | ||
| for (int i = 0; i < 1000; i++) { | ||
| std::thread(MakeSpans, i % 5).detach(); | ||
| //std::thread(MakeUniqueSpans).detach(); | ||
| } | ||
| std::cout << "Press <ENTER> for more spans"; | ||
| std::cin.get(); | ||
| } | ||
| run_span->End(); | ||
| } |
145 changes: 145 additions & 0 deletions
145
ext/include/opentelemetry/ext/http/server/file_http_server.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| #pragma once | ||
|
|
||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "opentelemetry/ext/http/server/http_server.h" | ||
|
|
||
| namespace HTTP_SERVER_NS | ||
| { | ||
|
|
||
| class FileHttpServer : public HTTP_SERVER_NS::HttpServer | ||
| { | ||
| protected: | ||
| /** | ||
| * Construct the server by initializing the endpoint for serving static files, | ||
| * which show up on the web if the user is on the given host:port. Static | ||
| * files can be seen relative to the folder where the executable was ran. | ||
| */ | ||
| FileHttpServer(const std::string &host = "127.0.0.1", int port = 3333) : HttpServer() | ||
| { | ||
| std::ostringstream os; | ||
| os << host << ":" << port; | ||
| setServerName(os.str()); | ||
| addListeningPort(port); | ||
| }; | ||
|
|
||
| /** | ||
| * Set the HTTP server to serve static files from the root of host:port. | ||
| * Derived HTTP servers should initialize the file endpoint AFTER they | ||
| * initialize their own, otherwise everything will be served like a file | ||
| * @param server should be an instance of this object | ||
| */ | ||
| void InitializeFileEndpoint(FileHttpServer &server) { server[root_endpt_] = ServeFile; } | ||
|
|
||
| private: | ||
| /** | ||
| * Return whether a file is found whose location is searched for relative to | ||
| * where the executable was triggered. If the file is valid, fill result with | ||
| * the file data/information required to display it on a webpage | ||
| * @param name of the file to look for, | ||
| * @param resulting file information, necessary for displaying them on a | ||
| * webpage | ||
| * @returns whether a file was found and result filled with display | ||
| * information | ||
| */ | ||
| bool FileGetSuccess(const std::string &filename, std::vector<char> &result) | ||
| { | ||
| #ifdef _WIN32 | ||
| std::replace(filename.begin(), filename.end(), '/', '\\'); | ||
| #endif | ||
| std::streampos size; | ||
| std::ifstream file(filename, std::ios::in | std::ios::binary | std::ios::ate); | ||
| if (file.is_open()) | ||
| { | ||
| size = file.tellg(); | ||
| if (size) | ||
| { | ||
| result.resize(size); | ||
| file.seekg(0, std::ios::beg); | ||
| file.read(result.data(), size); | ||
| } | ||
| file.close(); | ||
| return true; | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns the extension of a file | ||
| * @param name of the file | ||
| * @returns file extension type under HTTP protocol | ||
| */ | ||
| std::string GetMimeContentType(const std::string &filename) | ||
| { | ||
| std::string file_ext = filename.substr(filename.find_last_of(".") + 1); | ||
| auto file_type = mime_types_.find(file_ext); | ||
| return (file_type != mime_types_.end()) ? file_type->second : HTTP_SERVER_NS::CONTENT_TYPE_TEXT; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns the standardized name of a file by removing backslashes, and | ||
| * assuming index.html is the wanted file if a directory is given | ||
| * @param name of the file | ||
| */ | ||
| std::string GetFileName(std::string S) | ||
| { | ||
| if (S.back() == '/') | ||
| { | ||
| auto temp = S.substr(0, S.size() - 1); | ||
| S = temp; | ||
| } | ||
| // If filename appears to be a directory, serve the hypothetical index.html | ||
| // file there | ||
| if (S.find(".") == std::string::npos) | ||
| S += "/index.html"; | ||
|
|
||
| return S; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the response object with the correct file data based on the requested | ||
| * file address, or return 404 error if a file isn't found | ||
| * @param req is the HTTP request, which we use to figure out the response to | ||
| * send | ||
| * @param resp is the HTTP response we want to send to the frontend, including | ||
| * file data | ||
| */ | ||
| HTTP_SERVER_NS::HttpRequestCallback ServeFile{ | ||
| [&](HTTP_SERVER_NS::HttpRequest const &req, HTTP_SERVER_NS::HttpResponse &resp) { | ||
| LOG_INFO("File: %s\n", req.uri.c_str()); | ||
| auto f = GetFileName(req.uri); | ||
| auto filename = f.c_str() + 1; | ||
|
|
||
| std::vector<char> content; | ||
| if (FileGetSuccess(filename, content)) | ||
| { | ||
| resp.headers[HTTP_SERVER_NS::CONTENT_TYPE] = GetMimeContentType(filename); | ||
| resp.body = std::string(content.data(), content.size()); | ||
| resp.code = 200; | ||
| resp.message = HTTP_SERVER_NS::HttpServer::getDefaultResponseMessage(resp.code); | ||
| return resp.code; | ||
| } | ||
| // Two additional 'special' return codes possible here: | ||
| // 0 - proceed to next handler | ||
| // -1 - immediately terminate and close connection | ||
| resp.headers[HTTP_SERVER_NS::CONTENT_TYPE] = HTTP_SERVER_NS::CONTENT_TYPE_TEXT; | ||
| resp.code = 404; | ||
| resp.message = HTTP_SERVER_NS::HttpServer::getDefaultResponseMessage(resp.code); | ||
| resp.body = resp.message; | ||
| return 404; | ||
| }}; | ||
|
|
||
| // Maps file extensions to their HTTP-compatible mime file type | ||
| const std::unordered_map<std::string, std::string> mime_types_ = { | ||
| {"css", "text/css"}, {"png", "image/png"}, {"js", "text/javascript"}, | ||
| {"htm", "text/html"}, {"html", "text/html"}, {"json", "application/json"}, | ||
| {"txt", "text/plain"}, {"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"}, | ||
| }; | ||
| const std::string root_endpt_ = "/"; | ||
| }; | ||
|
|
||
| } // namespace HTTP_SERVER_NS |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add to the cout statement that a span is generated every second here would be a good idea? Also, directing the user to the right host:post url could be beneficial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did add a comment right at the top with the url.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I mean in the cout statement could be also a good place maybe?