Skip to content

Commit

Permalink
zPages: zPages tracez basic example (open-telemetry#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanghat committed Aug 20, 2020
1 parent c0c4fb3 commit 021bd5e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/zpages/BUILD
@@ -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",
],
)
57 changes: 57 additions & 0 deletions examples/zpages/zpages_example.cc
@@ -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();
}
}

0 comments on commit 021bd5e

Please sign in to comment.