Giter Club home page Giter Club logo

cpp2sky's Introduction

cpp2sky

cpp2sky test

Distributed tracing and monitor SDK in CPP for Apache SkyWalking APM. This SDK is compatible with C++ 17, C++ 14, and C++ 11.

Build

Bazel

Download cpp2sky tarball with specified version.

http_archive(
  name = "com_github_skyapm_cpp2sky",
  sha256 = <SHA256>,
  urls = ["https://github.com/skyAPM/cpp2sky/archive/<VERSION>.tar.gz"],
)

Add interface definition and library to your project.

cc_binary(
  name = "example",
  srcs = ["example.cc"],
  deps = [
    "@com_github_skyapm_cpp2sky//cpp2sky:cpp2sky_interface",
    "@com_github_skyapm_cpp2sky//source:cpp2sky_lib"
  ],
)

Docs

cpp2sky configration is based on protobuf, and docs are generated by protodoc. If you have any API change, you should run below.

protodoc --directory=./cpp2sky --parse="message" --languages="C++" --title=cpp2sky config --output=docs/README.md

Basic usage

Config

cpp2sky provides simple configuration for tracer. API docs are available at docs/README.md. The detail information is described in official protobuf definition.

#include <cpp2sky/config.pb.h>

int main() {
  using namespace cpp2sky;

  static const std::string service_name = "service_name";
  static const std::string instance_name = "instance_name";
  static const std::string oap_addr = "oap:12800";
  static const std::string token = "token";

  TracerConfig tracer_config;

  config.set_instance_name(instance_name);
  config.set_service_name(service_name);
  config.set_address(oap_addr);
  config.set_token(token);
}

Create tracer

After you constructed config, then setup tracer. Tracer supports gRPC reporter only, also TLS adopted gRPC reporter isn't available now. TLS adoption and REST tracer will be supported in the future.

TracerConfig tracer_config;

// Setup

TracerPtr tracer = createInsecureGrpcTracer(tracer_config);

Fetch propagated span

cpp2sky supports only HTTP tracer now. Tracing span will be delivered from sw8 and sw8-x HTTP headers. For more detail, please visit here Then, you can create propagated span object by decoding these items.

SpanContextPtr parent_span = createSpanContext(parent);

Create span

First, you must create tracing context that holds all spans, then crete initial entry span.

TracingContextPtr tracing_context = tracer->newContext();
TracingSpanPtr tracing_span = tracing_context->createEntrySpan();

After that, you can create another span to trace another workload, such as RPC to other services. Note that you must have parent span to create secondary span. It will construct parent-child relation when analysis.

TracingSpanPtr current_span = tracing_context->createExitSpan(current_span);

Alternative approach is RAII based one. It is used like below,

{
  StartEntrySpan entry_span(tracing_context, "sample_op1");
  {
    StartExitSpan exit_span(tracing_context, entry_span.get(), "sample_op2");

    // something...
  }
}

Send segment to OAP

Note that TracingContext is unique pointer. So when you'd like to send data, you must move it and don't refer after sending, to avoid undefined behavior.

TracingContextPtr tracing_context = tracer->newContext();
TracingSpanPtr tracing_span = tracing_context->createEntrySpan();

tracing_span->startSpan("sample_workload");
tracing_span->endSpan();

tracer->report(std::move(tracing_context));

Skywalking CDS

C++ agent implements Skywalking CDS feature it allows to change bootstrap config dynamically from the response of sync request, invoked from this periodically. Dynamically configurable values are described in description of properties on `docs/README.md'.

TracerConfig config;
// If you set this value as zero, CDS request won't occur.
config.set_cds_request_interval(5); // CDS request interval should be 5sec

If you are using Consul KVS as backend, we could put configuration value through HTTP request.

configurations:
  service_name:
    ignore_suffix: '/ignore, /hoge'

After setup configurations, try to put values with

curl --request PUT --data-binary "@./config.yaml" http://localhost:8500/v1/kv/configuration-discovery.default.agentConfigurations

Trace and Log integration

cpp2sky implements to output logs which is the key to integrate with actual tracing context.

Supported Logger

#include <spdlog/spdlog.h>
#include <cpp2sky/trace_log.h>

int main() {
  auto logger = spdlog::default_logger();
  // set_pattern must be called.
  logger->set_pattern(logFormat<decltype(logger)::element_type>());

  // It will generate log message as follows.
  //
  // {"level": "warning", "msg": "sample", "SW_CTX": ["service","instance","trace_id","segment_id","span_id"]}
  //
  logger->warn(tracing_context->logMessage("sample"));
}

Security

If you've found any security issues, please read Security Reporting Process and take described steps.

LICENSE

Apache 2.0 License. See LICENSE for more detail.

cpp2sky's People

Contributors

shikugawa avatar wuwen5 avatar wu-sheng avatar makefriend8 avatar wbpcode avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.