Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

content: add C++ exporter guides and how to create a custom exporter #287

Open
odeke-em opened this issue Aug 26, 2018 · 4 comments
Open
Assignees
Labels

Comments

@odeke-em
Copy link
Member

We've gotten interest from an influential C++ project that would like to instrument their backend.

This issue is a placeholder to add content to the website for C++

@odeke-em
Copy link
Member Author

Alright, and the good news is in, I was able to get the gracious @g-easy to write for us a completely runnable/standalone end-to-end program that doesn't require forking OpenCensus-CPP nor having to tame the beast called Bazel. His guide is at https://docs.google.com/document/d/14GGmrltkXelSPMe585jncFsLavde2Xz-mPXchnJRknE/edit?usp=sharing

or copy+paste from his doc

How to start an OpenCensus C++ Project
@g-easy, 2018-09-12 (US TZ)

We rely on bazel as a build system and the Abseil libraries.

To start, make a new directory and add a file called WORKSPACE. bazel needs this file at the root of your project.

In the WORKSPACE file, pull in OpenCensus and its dependency, Abseil:

-=-
http_archive(
    name = "io_opencensus_cpp",
    strip_prefix = "opencensus-cpp-master",
    urls = ["https://github.com/census-instrumentation/opencensus-cpp/archive/master.zip"],
)

# OpenCensus depends on Abseil so we have to explicitly pull it in.
# This is how diamond dependencies are prevented.
http_archive(
    name = "com_google_absl",
    strip_prefix = "abseil-cpp-master",
    urls = ["https://github.com/abseil/abseil-cpp/archive/master.zip"],
)
-=-

http_archive tells bazel to configure an "external repository" by downloading an archive via http, unpacking it, and removing the top-level directory (strip_prefix)

Next, add a C++ file, e.g. helloworld.cc:

-=-
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
#include "opencensus/trace/sampler.h"
#include "opencensus/trace/span.h"

int main(int argc, char **argv) {
  // Register stdout exporter. This will write span data to stdout for testing.
  opencensus::exporters::trace::StdoutExporter::Register();

  // Samplers are potentially expensive to construct. Use one long-lived sampler
  // instead of constructing one for every Span.
  static opencensus::trace::AlwaysSampler sampler;

  // Done initializing. Make a Span:
  auto span = opencensus::trace::Span::StartSpan("example.org/DoWork", nullptr,
                                                 {&sampler});
  span.AddAnnotation("Starting work.");
  // Sleep for [1,10] milliseconds to fake work.
  absl::SleepFor(absl::Milliseconds(rand() % 10 + 1));
  span.AddAnnotation("Finished work.");
  span.End();  // Spans must be Ended explicitly.

  std::cout << "Waiting 10.1s for exporters to run...\n\n";
  absl::SleepFor(absl::Milliseconds(10100));
}
-=-

Next, add a BUILD file:

-=-
cc_binary(
    name = "helloworld",
    srcs = ["helloworld.cc"],
    linkopts = ["-pthread"],
    deps = [
        "@com_google_absl//absl/time",
        "@io_opencensus_cpp//opencensus/exporters/trace/stdout:stdout_exporter",
        "@io_opencensus_cpp//opencensus/trace:trace",
    ],
)
-=-

"deps" is a list of libraries that our binary depends on. As well as linking these libarier, bazel will also make their headers available to the compiler.

"@io_opencensus_cpp" refers to an external subrepository.
"//opencensus/trace" is a directory path within that subrepository.
And ":trace" is the name of a cc_library target in that directory. (TODO: add link to file and line in github showing cc_library)

From the compiler's point of view, all of the sources and dependencies' headers are merged into a single hierarchy, e.g.:

helloworld.cc
absl/time/time.h
opencensus/trace/span.h

This explains the #include paths above.

Finally, build the binary:

-=-
bazel build :helloworld
-=-

The first time it's run, bazel will download and unpack the http_archives.

The binary will be produced at bazel-bin/helloworld

With these I think we have enough material to make the C++ quickstarts.
/cc @PikBot @hvent90

odeke-em added a commit that referenced this issue Oct 20, 2018
Added a full quickstart tutorial for C++
tracing with the Zipkin exporter.

Updates #287
Updates #343
odeke-em added a commit that referenced this issue Oct 20, 2018
Added a full quickstart tutorial for C++
tracing with the Zipkin exporter.

Updates #287
Updates #343
odeke-em added a commit that referenced this issue Oct 20, 2018
Added a full quickstart tutorial for C++
tracing with the Prometheus exporter.

Updates #287
Updates #343
odeke-em added a commit that referenced this issue Oct 20, 2018
Added a full quickstart tutorial for C++
metrics with the Prometheus exporter and
also updates the C++ tracing quickstart
with some grammatical fixes and added
references.

Updates #287
Updates #343
odeke-em added a commit that referenced this issue Oct 22, 2018
Added a full quickstart tutorial for C++
metrics with the Prometheus exporter and
also updates the C++ tracing quickstart
with some grammatical fixes and added
references.

Updates #287
Updates #343
@odeke-em
Copy link
Member Author

Alright, so the quickstarts are done and shipped:

@odeke-em odeke-em changed the title content: add C++ quickstarts, exporter guide and how to create a custom exporter content: add C++ exporter guides and how to create a custom exporter Oct 22, 2018
@g-easy
Copy link
Contributor

g-easy commented Nov 5, 2018

Is this issue done?

@odeke-em
Copy link
Member Author

odeke-em commented Nov 5, 2018

Not yet @g-easy, we still need to write:

  • Guide for creating a custom stats exporter in C++
  • Document the various trace and stats exporters in C++

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants