| 1 |
|
| /* otel_clock.h - when a span started, and how long it took. |
| 2 |
|
| * |
| 3 |
|
| * OTLP wants unix nanoseconds for BOTH ends of a span, which invites the |
| 4 |
|
| * obvious implementation: read the wall clock twice and write both numbers. |
| 5 |
|
| * That implementation is wrong, and wrong in a way that only shows up in |
| 6 |
|
| * production. |
| 7 |
|
| * |
| 8 |
|
| * The wall clock is not monotonic. NTP steps it, an operator sets it, a VM |
| 9 |
|
| * resumes from a snapshot and it jumps. Between two reads it can move |
| 10 |
|
| * backwards, and a span whose end precedes its start is one collectors |
| 11 |
|
| * variously drop, clamp to zero, or display as a negative duration - none of |
| 12 |
|
| * which is the truth, and all of which happen to the trace that was |
| 13 |
|
| * interesting enough to be looked at. |
| 14 |
|
| * |
| 15 |
|
| * So: the wall clock is read ONCE, at start, to place the span in time. The |
| 16 |
|
| * DURATION is measured on a monotonic clock, which is what monotonic clocks |
| 17 |
|
| * are for, and the end timestamp is derived as start plus elapsed. The span |
| 18 |
|
| * lands at the right absolute moment and always has a non-negative length. |
| 19 |
|
| * |
| 20 |
|
| * Where no monotonic clock exists the wall clock is used for both, and the |
| 21 |
|
| * derived end is clamped to be no earlier than the start. That is the best |
| 22 |
|
| * available answer rather than a good one. |
| 23 |
|
| */ |
| 24 |
|
| |
| 25 |
|
| #ifndef OTEL_CLOCK_H |
| 26 |
|
| #define OTEL_CLOCK_H |
| 27 |
|
| |
| 28 |
|
| #include <time.h> |
| 29 |
|
| #ifndef _WIN32 |
| 30 |
|
| # include <sys/time.h> |
| 31 |
|
| #endif |
| 32 |
|
| |
| 33 |
|
| /* Wall clock, unix nanoseconds. Where a span sits in time. clock_gettime |
| 34 |
|
| * only where the Makefile.PL probe LINKED it: glibc before 2.17 keeps it in |
| 35 |
|
| * librt, and CLOCK_REALTIME being defined says nothing about that. */ |
| 36 |
23029 |
| static U64TYPE otel_wall_nanos(void) { |
| 37 |
|
| #if defined(OTEL_HAVE_CLOCK_GETTIME) && !defined(_WIN32) |
| 38 |
|
| struct timespec ts; |
| 39 |
23029 |
▶ | if (clock_gettime(CLOCK_REALTIME, &ts) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (clock_gettime(CLOCK_REALTIME, &ts) == 0) |
23029 |
0 |
23029 |
|
| 40 |
23029 |
| return (U64TYPE)ts.tv_sec * 1000000000ULL + (U64TYPE)ts.tv_nsec; |
| 41 |
|
| #endif |
| 42 |
|
| #ifndef _WIN32 |
| 43 |
|
| { |
| 44 |
|
| struct timeval tv; |
| 45 |
0 |
▶ | if (gettimeofday(&tv, NULL) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (gettimeofday(&tv, NULL) == 0) |
0 |
0 |
0 |
|
| 46 |
0 |
| return (U64TYPE)tv.tv_sec * 1000000000ULL |
| 47 |
0 |
| + (U64TYPE)tv.tv_usec * 1000ULL; |
| 48 |
|
| } |
| 49 |
|
| #endif |
| 50 |
0 |
| return (U64TYPE)time(NULL) * 1000000000ULL; |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| /* Monotonic nanoseconds. Not an absolute time - only differences mean |
| 54 |
|
| * anything - which is precisely why it is the right clock for a duration. */ |
| 55 |
5964 |
| static U64TYPE otel_mono_nanos(void) { |
| 56 |
|
| #if defined(OTEL_HAVE_CLOCK_MONOTONIC) && !defined(_WIN32) |
| 57 |
|
| struct timespec ts; |
| 58 |
5964 |
▶ | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
5964 |
0 |
5964 |
|
| 59 |
5964 |
| return (U64TYPE)ts.tv_sec * 1000000000ULL + (U64TYPE)ts.tv_nsec; |
| 60 |
|
| #endif |
| 61 |
0 |
| return otel_wall_nanos(); /* no monotonic clock: see the clamp below */ |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| /* The end timestamp for a span that started at (wall, mono). |
| 65 |
|
| * |
| 66 |
|
| * The clamp matters only on a platform with no monotonic clock, where both |
| 67 |
|
| * reads are of the wall clock and the second can legitimately be smaller. |
| 68 |
|
| * A zero-length span is a lie, but it is a smaller lie than a negative one, |
| 69 |
|
| * and it is the one a collector will actually store. */ |
| 70 |
2551 |
| static U64TYPE otel_end_nanos(U64TYPE start_wall, U64TYPE start_mono) { |
| 71 |
2551 |
| U64TYPE now = otel_mono_nanos(); |
| 72 |
2551 |
▶ | if (now < start_mono) return start_wall; |
Branch: 1 branch
| Branch | True | False | Total |
| if (now < start_mono) return start_wall; |
0 |
2551 |
2551 |
|
| 73 |
2551 |
| return start_wall + (now - start_mono); |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| #endif /* OTEL_CLOCK_H */ |