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

OpenCensus Stats implementation #220

Merged
merged 46 commits into from
Jan 26, 2019
Merged

OpenCensus Stats implementation #220

merged 46 commits into from
Jan 26, 2019

Conversation

basvanbeek
Copy link
Member

@basvanbeek basvanbeek commented Nov 27, 2018

Here is the initial work on OpenCensus Stats for PHP.

  • PHP Userland API for Stats (requires PHP 7.1+ see Bump minimum required PHP version to 7.1 for next releases #219)
  • Daemon Service for bridging PHP processes with OC Agent (written in Go)
  • Custom protocol for communication between PHP and Daemon over Unix Socket (Linux/OS X) or Named Pipe (Windows)
  • PHP Extension enables improved communication with Daemon over Unix Socket (communication in dedicated thread, minimizing time in PHP request path).

- Userland Stats API for PHP 7.1+
- Userland DaemonClient for sending all PHP Stats (and Trace/Span export) over a Unix Socket
- Daemon Service written in Go to consume PHP Stats directives and Span Exports
- Relies on OC Agent functionality for exporting out stats and traces.

It is possible to add regular OpenCensus-Go exporters to the Daemon however we desire to have
the census-ecosystem/opencensus-go-exporter-ocagent to be finalized as all data should go through
the OC Agent (thus needing as little configuration and no duplication of effort on supporting exporters)
Copy link
Member

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome @basvanbeek! I've done the first pass of the Go code and skimmed over the PHP one, please take a look at my comments.

daemon/connection.go Outdated Show resolved Hide resolved
daemon/connection.go Outdated Show resolved Hide resolved
daemon/connection.go Outdated Show resolved Hide resolved
daemon/connection.go Outdated Show resolved Hide resolved
daemon/connection.go Show resolved Hide resolved
daemon/processor/local/processor.go Outdated Show resolved Hide resolved
daemon/processor/local/processor.go Show resolved Hide resolved
daemon/unixsocket/unixsocket.go Outdated Show resolved Hide resolved
daemon/unixsocket/unixsocket.go Outdated Show resolved Hide resolved
daemon/processor/local/processor.go Show resolved Hide resolved
daemon/cmd/main.go Outdated Show resolved Hide resolved
@odeke-em
Copy link
Member

Needs OC Agent updates, see: census-ecosystem/opencensus-go-exporter-ocagent#32

@basvanbeek we've finished working on the ocagent-go-exporter so it can now upload stats and transform them into metrics. I plan on wrapping up at least one Metrics exporter by the end of this week.

daemon/cmd/main.go Show resolved Hide resolved
daemon/connection.go Outdated Show resolved Hide resolved
)
if remainder < 0 {
// truncate data as it holds more then we need
data = data[0 : msgLen-offset]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this operation could become a hotspot and we can improve it to have a constant behavior
whether remainder < 0 or not

i, max := 0, len(data)
if nr := msgLen - (offset + len(data)); nr < 0 {
     // Truncate data indices to limit what we need.
     max = msgLen-offset
}

// TODO: (@basvanbeek) investigate if this reslicing is needed.
// Re-slice RawPayload to fit additional data.
mh.RawPayload = mh.RawPayload[0: offset+max]
copy(mh.RawPayload[offset:], data[i:max])

private final function send(string $type, string $msg = ''): bool
{
$start = microtime(true);
$maxEnd = $start + $this->maxSendTime;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we exceed maxSendTime X times during a single request, maybe we should have a per-request circuit-breaker that skips send calls during the rest of the request?

I'm thinking of a case where the agent is hanging and we end up waiting maxSendTime for each measurement.

Copy link
Member Author

@basvanbeek basvanbeek Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll add it. Also there will be an extension version of the daemon client which will allow stats recording to be handled outside the php request path.

Some background:
Typically a single call to send is returned very quickly either with success or an error (temporary failure if buffer is full) as we're using non blocking sockets. The reason the code is cycling here and setting a max send time is to set a limit to the amount of time allowed to transport large payloads needing multiple calls to send (ex. very elaborate tracing spans).
On Mac OS X the default (and unchangeable) size is 8192 bytes so it potentially needs to be called a couple of times. Most stats record operations are well below this limit and OpenCensus TagContext itself is limited to 8192 bytes (so a maxed out stats record call probably won't go beyond 2 calls to send).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there will be an extension version of the daemon client which will allow stats recording to be handled outside the php request path.

That's terrific.

Copy link
Member Author

@basvanbeek basvanbeek Jan 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greggdonovan initial version of php extension powered daemonclient transport has been added to this PR.

This was referenced Jan 15, 2019
Copy link
Member

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this @basvanbeek! Thank you @greggdonovan for the feedback too.

We've let it simmer for a while and I think with our discussions and tests this is good to go. I am making the approval to get the ball rolling as we haven't heard much back. We can keep updating as we go on.

@basvanbeek basvanbeek merged commit d1512ab into master Jan 26, 2019
@bshaffer bshaffer mentioned this pull request Jan 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants