-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
…try-cpp into origin/propagation
- Loading branch information
Showing
7 changed files
with
241 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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,31 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_binary( | ||
name = "zpages_example", | ||
srcs = [ | ||
"zpages_example.cc", | ||
], | ||
linkopts = select({ | ||
"//bazel:windows": [], | ||
"//conditions:default": ["-pthread"], | ||
}), | ||
deps = [ | ||
"//ext:headers", | ||
"//ext/src/zpages", | ||
"//sdk/src/trace", | ||
], | ||
) |
This file contains 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,57 @@ | ||
/** | ||
* This is a basic example for zpages that helps users get familiar with how to | ||
* use this feature in OpenTelemetery | ||
*/ | ||
#include <chrono> | ||
#include <iostream> | ||
#include <string> | ||
#include "opentelemetry/context/threadlocal_context.h" | ||
|
||
#include "opentelemetry/ext/zpages/zpages.h" // Required file include for zpages | ||
|
||
using opentelemetry::core::SteadyTimestamp; | ||
|
||
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::Initialize(); | ||
auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(""); | ||
|
||
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"; | ||
|
||
// Error span | ||
std::map<std::string, opentelemetry::common::AttributeValue> attribute_map; | ||
attribute_map["completed_search_for"] = "Unknown user"; | ||
tracer->StartSpan("find user", attribute_map) | ||
->SetStatus(opentelemetry::trace::CanonicalCode::NOT_FOUND, "User not found"); | ||
|
||
// Long time duration span | ||
std::map<std::string, opentelemetry::common::AttributeValue> attribute_map2; | ||
attribute_map2["completed_search_for"] = "John Doe"; | ||
opentelemetry::trace::StartSpanOptions start; | ||
start.start_steady_time = SteadyTimestamp(nanoseconds(1)); | ||
opentelemetry::trace::EndSpanOptions end; | ||
end.end_steady_time = SteadyTimestamp(nanoseconds(1000000000000)); | ||
tracer->StartSpan("find user", attribute_map2, start)->End(end); | ||
|
||
// Running(deadlock) span | ||
std::map<std::string, opentelemetry::common::AttributeValue> attribute_map3; | ||
attribute_map3["searching_for"] = "Deleted user"; | ||
auto running_span = tracer->StartSpan("find user", attribute_map3); | ||
|
||
// 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("ping user")->End(); | ||
} | ||
} |
This file contains 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,65 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <memory> | ||
|
||
#include "opentelemetry/ext/zpages/tracez_data_aggregator.h" | ||
#include "opentelemetry/ext/zpages/tracez_http_server.h" | ||
#include "opentelemetry/ext/zpages/tracez_processor.h" | ||
|
||
#include "opentelemetry/nostd/shared_ptr.h" | ||
#include "opentelemetry/sdk/trace/tracer_provider.h" | ||
#include "opentelemetry/trace/provider.h" | ||
|
||
using opentelemetry::ext::zpages::TracezDataAggregator; | ||
using opentelemetry::ext::zpages::TracezHttpServer; | ||
using opentelemetry::ext::zpages::TracezSpanProcessor; | ||
using std::chrono::microseconds; | ||
|
||
/** | ||
* Wrapper for zPages that initializes all the components required for zPages, | ||
* and starts the HTTP server in the constructor and ends it in the destructor. | ||
* The constructor and destructor for this object is private to prevent | ||
* creation other than by calling the static function Initialize(). This follows the | ||
* meyers singleton pattern and only a single instance of the class is allowed. | ||
*/ | ||
class ZPages | ||
{ | ||
public: | ||
/** | ||
* This function is called if the user wishes to include zPages in their | ||
* application. It creates a static instance of this class. | ||
*/ | ||
static void Initialize() { static ZPages instance; } | ||
|
||
private: | ||
/** | ||
* Constructor is responsible for initializing the tracer, tracez processor, | ||
* tracez data aggregator and the tracez server. The server is also started in | ||
* constructor. | ||
*/ | ||
ZPages() | ||
{ | ||
auto tracez_processor_ = std::make_shared<TracezSpanProcessor>(); | ||
auto tracez_provider_ = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>( | ||
new opentelemetry::sdk::trace::TracerProvider(tracez_processor_)); | ||
|
||
auto tracez_aggregator = | ||
std::unique_ptr<TracezDataAggregator>(new TracezDataAggregator(tracez_processor_)); | ||
|
||
tracez_server_ = | ||
std::unique_ptr<TracezHttpServer>(new TracezHttpServer(std::move(tracez_aggregator))); | ||
|
||
tracez_server_->start(); | ||
|
||
opentelemetry::trace::Provider::SetTracerProvider(tracez_provider_); | ||
} | ||
|
||
~ZPages() | ||
{ | ||
// shut down the server when the object goes out of scope(at the end of the | ||
// program) | ||
tracez_server_->stop(); | ||
} | ||
std::unique_ptr<TracezHttpServer> tracez_server_; | ||
}; |
This file contains 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 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
Oops, something went wrong.