pinpoint-apm / pinpoint-c-agent

Pinpoint C Agent helps your monitor your PHP/PYTHON applications into [pinpoint-apm](https://github.com/pinpoint-apm/pinpoint).
http://pinpoint-apm.github.io/pinpoint/
Apache License 2.0
262 stars 78 forks source link

pinpoint-c-agen directly monitor C/C++ applications? #690

Closed enhon1992 closed 1 month ago

enhon1992 commented 1 month ago

Can we use pinpoint-c-agent to directly monitor our C/C++ applications, similar to how the official pinpoint Java agent automatically collects JVM metrics and performs trace instrumentation for some common frameworks?

eeliu commented 1 month ago

Good question !

similar to how the official pinpoint Java agent automatically collects JVM metrics

I don't know what's is "JVM metrics in cpp applications", do you want to get JVM metric in dynamic library which writen by cpp/c from your java application? if that, use pinpoint-java agent please.

performs trace instrumentation for some common frameworks?

What's your common framework and what's kind of it ? (http/https server or tcp/udp event )

out-of-box plugin like php/python agent

Not yet

If your application is totally a cpp/c application, read below please.

I'm here to share some experiments about cpp/c application in pinpoint.

There are two important topic : monitor entry and monitor chain

Monitor Entry

Http server or some like gRpc, message queue .. etc, we call them as event driver server. It will produce a monitor chain when it parse a message(request) and pinpoint trace Entry starts from here.

  1. So, define your pinpoint entry. Here is the example for cpp-httplib

https://github.com/pinpoint-apm/pinpoint-c-agent/blob/58857816c21f9ce4a97b5bb834379617f20d452c/testapps/cpp/server.cpp#L25-L32

Next topic is monitor chain, that describes how to arrange your work flow on execution unit.

Monitor Chain

It's a set of function calling process(called interceptor in pinpoint),like redis.get,mysql_client.query

Monitor a calling process

C/C++ does not support AOP

Just decorate monitor function, like pin_func

https://github.com/pinpoint-apm/pinpoint-c-agent/blob/58857816c21f9ce4a97b5bb834379617f20d452c/testapps/cpp/pinpoint_helper.h#L34-L37

Combine calling process together

Every traced process must come with a parent, we combined these processes with tree structure.

current_trace_id = pinpoint_start_trace(parent_trace_id);

image

Keep trace chain

By pinpoint-c-agent,you can fetch and modify a trace chain by itself id.

  1. If application is synchronous thread mode, just store this id into thread_local (like cpp-httplib) like below https://github.com/pinpoint-apm/pinpoint-c-agent/blob/58857816c21f9ce4a97b5bb834379617f20d452c/testapps/cpp/pinpoint_helper.h#L27-L29

cpp-httplib exmaple

  1. Application is based on libevent/libev

Things are starting to get complicated

The chain id should be kept into request context and shared to every subsequent calls.

Example libevent-example. Read more

  1. Blending application framework: event driver , thread pool, or more complex use coroutine

pinpoint provides asynchronous context API which allows to combine multiple calls chains into one.

More detail goes to https://github.com/pinpoint-apm/pinpoint-c-agent/issues/689

If you have more question, please left comment

eeliu commented 1 month ago

close as completed

Feel free to re-open, if have more questions.