GRPC Core  0.13.1-pre1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
census.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 /* RPC-internal Census API's. These are designed to be generic enough that
35  * they can (ultimately) be used in many different RPC systems (with differing
36  * implementations). */
37 
38 #ifndef CENSUS_CENSUS_H
39 #define CENSUS_CENSUS_H
40 
41 #include <grpc/grpc.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /* Identify census features that can be enabled via census_initialize(). */
49  CENSUS_FEATURE_NONE = 0, /* Do not enable census. */
50  CENSUS_FEATURE_TRACING = 1, /* Enable census tracing. */
51  CENSUS_FEATURE_STATS = 2, /* Enable Census stats collection. */
52  CENSUS_FEATURE_CPU = 4, /* Enable Census CPU usage collection. */
55 };
56 
62 CENSUSAPI int census_initialize(int features);
63 CENSUSAPI void census_shutdown(void);
64 
67 CENSUSAPI int census_supported(void);
68 
70 CENSUSAPI int census_enabled(void);
71 
82 
83 /* A tag is a key:value pair. The key is a non-empty, printable (UTF-8
84  encoded), nil-terminated string. The value is a binary string, that may be
85  printable. There are limits on the sizes of both keys and values (see
86  CENSUS_MAX_TAG_KB_LEN definition below), and the number of tags that can be
87  propagated (CENSUS_MAX_PROPAGATED_TAGS). Users should also remember that
88  some systems may have limits on, e.g., the number of bytes that can be
89  transmitted as metadata, and that larger tags means more memory consumed
90  and time in processing. */
91 typedef struct {
92  const char *key;
93  const char *value;
94  size_t value_len;
95  uint8_t flags;
96 } census_tag;
97 
98 /* Maximum length of a tag's key or value. */
99 #define CENSUS_MAX_TAG_KV_LEN 255
100 /* Maximum number of propagatable tags. */
101 #define CENSUS_MAX_PROPAGATED_TAGS 255
102 
103 /* Tag flags. */
104 #define CENSUS_TAG_PROPAGATE 1 /* Tag should be propagated over RPC */
105 #define CENSUS_TAG_STATS 2 /* Tag will be used for statistics aggregation */
106 #define CENSUS_TAG_BINARY 4 /* Tag value is not printable */
107 #define CENSUS_TAG_RESERVED 8 /* Reserved for internal use. */
108 /* Flag values 8,16,32,64,128 are reserved for future/internal use. Clients
109  should not use or rely on their values. */
110 
111 #define CENSUS_TAG_IS_PROPAGATED(flags) (flags & CENSUS_TAG_PROPAGATE)
112 #define CENSUS_TAG_IS_STATS(flags) (flags & CENSUS_TAG_STATS)
113 #define CENSUS_TAG_IS_BINARY(flags) (flags & CENSUS_TAG_BINARY)
114 
115 /* An instance of this structure is kept by every context, and records the
116  basic information associated with the creation of that context. */
117 typedef struct {
118  int n_propagated_tags; /* number of propagated printable tags */
119  int n_propagated_binary_tags; /* number of propagated binary tags */
120  int n_local_tags; /* number of non-propagated (local) tags */
121  int n_deleted_tags; /* number of tags that were deleted */
122  int n_added_tags; /* number of tags that were added */
123  int n_modified_tags; /* number of tags that were modified */
124  int n_invalid_tags; /* number of tags with bad keys or values (e.g.
125  longer than CENSUS_MAX_TAG_KV_LEN) */
126  int n_ignored_tags; /* number of tags ignored because of
127  CENSUS_MAX_PROPAGATED_TAGS limit. */
129 
130 /* Create a new context, adding and removing tags from an existing context.
131  This will copy all tags from the 'tags' input, so it is recommended
132  to add as many tags in a single operation as is practical for the client.
133  @param base Base context to build upon. Can be NULL.
134  @param tags A set of tags to be added/changed/deleted. Tags with keys that
135  are in 'tags', but not 'base', are added to the tag set. Keys that are in
136  both 'tags' and 'base' will have their value/flags modified. Tags with keys
137  in both, but with NULL or zero-length values, will be deleted from the tag
138  set. Tags with invalid (too long or short) keys or values will be ignored.
139  If adding a tag will result in more than CENSUS_MAX_PROPAGATED_TAGS in either
140  binary or non-binary tags, they will be ignored, as will deletions of
141  tags that don't exist.
142  @param ntags number of tags in 'tags'
143  @param status If not NULL, will return a pointer to a census_context_status
144  structure containing information about the new context and status of the
145  tags used in its creation.
146  @return A new, valid census_context.
147 */
149  const census_context *base, const census_tag *tags, int ntags,
150  census_context_status const **status);
151 
152 /* Destroy a context. Once this function has been called, the context cannot
153  be reused. */
155 
156 /* Get a pointer to the original status from the context creation. */
158  const census_context *context);
159 
160 /* Structure used for iterating over the tegs in a context. API clients should
161  not use or reference internal fields - neither their contents or
162  presence/absence are guaranteed. */
163 typedef struct {
165  int base;
166  int index;
167  char *kvm;
169 
170 /* Initialize a census_tag_iterator. Must be called before first use. */
172  const census_context *context, census_context_iterator *iterator);
173 
174 /* Get the contents of the "next" tag in the context. If there are no more
175  tags, returns 0 (and 'tag' contents will be unchanged), otherwise returns 1.
176  */
178  census_tag *tag);
179 
180 /* Get a context tag by key. Returns 0 if the key is not present. */
182  const char *key, census_tag *tag);
183 
184 /* Tag set encode/decode functionality. These functionas are intended
185  for use by RPC systems only, for purposes of transmitting/receiving contexts.
186  */
187 
188 /* Encode a context into a buffer. The propagated tags are encoded into the
189  buffer in two regions: one for printable tags, and one for binary tags.
190  @param context context to be encoded
191  @param buffer pointer to buffer. This address will be used to encode the
192  printable tags.
193  @param buf_size number of available bytes in buffer.
194  @param print_buf_size Will be set to the number of bytes consumed by
195  printable tags.
196  @param bin_buf_size Will be set to the number of bytes used to encode the
197  binary tags.
198  @return A pointer to the binary tag's encoded, or NULL if the buffer was
199  insufficiently large to hold the encoded tags. Thus, if successful,
200  printable tags are encoded into
201  [buffer, buffer + *print_buf_size) and binary tags into
202  [returned-ptr, returned-ptr + *bin_buf_size) (and the returned
203  pointer should be buffer + *print_buf_size) */
204 CENSUSAPI char *census_context_encode(const census_context *context,
205  char *buffer, size_t buf_size,
206  size_t *print_buf_size,
207  size_t *bin_buf_size);
208 
209 /* Decode context buffers encoded with census_context_encode(). Returns NULL
210  if there is an error in parsing either buffer. */
211 CENSUSAPI census_context *census_context_decode(const char *buffer, size_t size,
212  const char *bin_buffer,
213  size_t bin_size);
214 
215 /* Distributed traces can have a number of options. */
217  CENSUS_TRACE_MASK_NONE = 0, /* Default, empty flags */
218  CENSUS_TRACE_MASK_IS_SAMPLED = 1 /* RPC tracing enabled for this context. */
219 };
220 
223 CENSUSAPI int census_trace_mask(const census_context *context);
224 
226 CENSUSAPI void census_set_trace_mask(int trace_mask);
227 
228 /* The concept of "operation" is a fundamental concept for Census. In an RPC
229  system, and operation typcially represents a single RPC, or a significant
230  sub-part thereof (e.g. a single logical "read" RPC to a distributed storage
231  system might do several other actions in parallel, from looking up metadata
232  indices to making requests of other services - each of these could be a
233  sub-operation with the larger RPC operation). Census uses operations for the
234  following:
235 
236  CPU accounting: If enabled, census will measure the thread CPU time
237  consumed between operation start and end times.
238 
239  Active operations: Census will maintain information on all currently
240  active operations.
241 
242  Distributed tracing: Each operation serves as a logical trace span.
243 
244  Stats collection: Stats are broken down by operation (e.g. latency
245  breakdown for each unique RPC path).
246 
247  The following functions serve to delineate the start and stop points for
248  each logical operation. */
249 
254 typedef struct {
255  /* Use gpr_timespec for default implementation. High performance
256  * implementations should use a cycle-counter based timestamp. */
259 
275 
287 typedef struct {
288  const char *(*get_rpc_service_name)(int64_t id);
289  const char *(*get_rpc_method_name)(int64_t id);
291 
327  const census_context *context, int64_t rpc_name_id,
328  const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
329  const census_timestamp *start_time);
330 
335  const char *peer);
336 
356  const char *buffer, int64_t rpc_name_id,
357  const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
358  census_timestamp *start_time);
359 
386  const char *family, const char *name,
387  int trace_mask);
388 
398 CENSUSAPI void census_end_op(census_context *context, int status);
399 
400 #define CENSUS_TRACE_RECORD_START_OP ((uint32_t)0)
401 #define CENSUS_TRACE_RECORD_END_OP ((uint32_t)1)
402 
410 CENSUSAPI void census_trace_print(census_context *context, uint32_t type,
411  const char *buffer, size_t n);
412 
414 typedef struct {
415  census_timestamp timestamp; /* Time of record creation */
416  uint64_t trace_id; /* Trace ID associated with record */
417  uint64_t op_id; /* Operation ID associated with record */
418  uint32_t type; /* Type (as used in census_trace_print() */
419  const char *buffer; /* Buffer (from census_trace_print() */
420  size_t buf_size; /* Number of bytes inside buffer */
422 
432 CENSUSAPI int census_trace_scan_start(int consume);
433 
444 
447 
448 /* Core stats collection API's. The following concepts are used:
449  * Aggregation: A collection of values. Census supports the following
450  aggregation types:
451  Sum - a single summation type. Typically used for keeping (e.g.)
452  counts of events.
453  Distribution - statistical distribution information, used for
454  recording average, standard deviation etc.
455  Histogram - a histogram of measurements falling in defined bucket
456  boundaries.
457  Window - a count of events that happen in reolling time window.
458  New aggregation types can be added by the user, if desired (see
459  census_register_aggregation()).
460  * Metric: Each measurement is for a single metric. Examples include RPC
461  latency, CPU seconds consumed, and bytes transmitted.
462  * View: A view is a combination of a metric, a tag set (in which the tag
463  values are regular expressions) and a set of aggregations. When a
464  measurement for a metric matches the view tags, it is recorded (for each
465  unique set of tags) against each aggregation. Each metric can have an
466  arbitrary number of views by which it will be broken down.
467 */
468 
469 /* A single value to be recorded comprises two parts: an ID for the particular
470  * metric and the value to be recorded against it. */
471 typedef struct {
472  uint32_t metric_id;
473  double value;
474 } census_value;
475 
476 /* Record new usage values against the given context. */
478  census_value *values, size_t nvalues);
479 
482 
483 /* Predefined aggregation types, for use with census_view_create(). */
488 
491 typedef struct {
493  const void *create_arg; /* Aaggregation initialization argument. */
495 
497 typedef struct census_view census_view;
498 
508 /* TODO(aveitch): consider if context is the right argument type to pass in
509  tags. */
511  uint32_t metric_id, const census_context *tags,
512  const census_aggregation *aggregations, size_t naggregations);
513 
516 
518 CENSUSAPI size_t census_view_metric(const census_view *view);
519 
522 
525 
528  const census_view *view);
529 
532 typedef struct {
533  const census_context *tags; /* Tags for this set of aggregations. */
534  const void **data; /* One data set for every aggregation in the view. */
536 
538 typedef struct {
539  size_t n_tag_sets; /* Number of unique tag sets that matched view. */
540  const census_view_aggregation_data *data; /* n_tag_sets entries */
542 
548 
551 
552 #ifdef __cplusplus
553 }
554 #endif
555 
556 #endif /* CENSUS_CENSUS_H */
int n_modified_tags
Definition: census.h:123
census_aggregation_ops census_agg_histogram
CENSUSAPI int census_enabled(void)
Return the census features currently enabled.
CENSUSAPI void census_view_delete(census_view *view)
Destroy a previously created view.
int n_propagated_tags
Definition: census.h:118
CENSUSAPI census_context * census_start_server_rpc_op(const char *buffer, int64_t rpc_name_id, const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask, census_timestamp *start_time)
Start a server RPC operation.
struct census_aggregation_ops census_aggregation_ops
Type representing a particular aggregation.
Definition: census.h:481
Definition: census.h:471
double value
Definition: census.h:473
CENSUSAPI size_t census_view_metric(const census_view *view)
Metric ID associated with a view.
Definition: census.h:117
CENSUSAPI const census_context_status * census_context_get_status(const census_context *context)
struct census_view census_view
A census view type.
Definition: census.h:497
size_t value_len
Definition: census.h:94
int n_added_tags
Definition: census.h:122
Definition: census.h:50
Holds all the aggregation data for a particular view instantiation.
Definition: census.h:532
const void ** data
Definition: census.h:534
CENSUSAPI const census_aggregation * census_view_aggregrations(const census_view *view)
Get aggregation descriptors associated with a view.
CENSUSAPI int census_context_get_tag(const census_context *context, const char *key, census_tag *tag)
const census_view_aggregation_data * data
Definition: census.h:540
CENSUSAPI int census_trace_mask(const census_context *context)
Get the current trace mask associated with this context.
CENSUSAPI void census_record_values(census_context *context, census_value *values, size_t nvalues)
int n_deleted_tags
Definition: census.h:121
Information needed to instantiate a new aggregation.
Definition: census.h:491
CENSUSAPI census_context * census_context_decode(const char *buffer, size_t size, const char *bin_buffer, size_t bin_size)
int n_invalid_tags
Definition: census.h:124
Definition: census.h:52
Trace record.
Definition: census.h:414
CENSUSAPI char * census_context_encode(const census_context *context, char *buffer, size_t buf_size, size_t *print_buf_size, size_t *bin_buf_size)
CENSUSAPI void census_trace_print(census_context *context, uint32_t type, const char *buffer, size_t n)
Insert a trace record into the trace stream.
census_trace_mask_values
Definition: census.h:216
uint8_t flags
Definition: census.h:95
Represent functions to map RPC name ID to service/method names.
Definition: census.h:287
Definition: census.h:53
uint64_t op_id
Definition: census.h:417
Definition: census.h:51
CENSUSAPI census_context * census_context_create(const census_context *base, const census_tag *tags, int ntags, census_context_status const **status)
CENSUSAPI census_context * census_start_op(census_context *context, const char *family, const char *name, int trace_mask)
Start a new, non-RPC operation.
CENSUSAPI int census_trace_scan_start(int consume)
Start a scan of existing trace records.
const census_aggregation_ops * ops
Definition: census.h:492
int n_local_tags
Definition: census.h:120
CENSUSAPI const census_context * census_view_tags(const census_view *view)
Get tags associated with view.
int n_ignored_tags
Definition: census.h:126
CENSUSAPI void census_shutdown(void)
CENSUSAPI void census_trace_scan_end()
End a scan previously started by census_trace_scan_start()
char * kvm
Definition: census.h:167
CENSUSAPI void census_context_destroy(census_context *context)
CENSUSAPI size_t census_view_naggregations(const census_view *view)
Number of aggregations associated with view.
CENSUSAPI void census_view_reset(census_view *view)
Reset all view data to zero for the specified view.
size_t n_tag_sets
Definition: census.h:539
census_timestamp timestamp
Definition: census.h:415
gpr_timespec ts
Definition: census.h:257
uint32_t type
Definition: census.h:418
census_aggregation_ops census_agg_distribution
int base
Definition: census.h:165
This structure represents a timestamp as used by census to record the time at which an operation begi...
Definition: census.h:254
uint32_t metric_id
Definition: census.h:472
Definition: census.h:218
CENSUSAPI const census_view_data * census_view_get_data(const census_view *view)
Get data from aggregations associated with a view.
CENSUSAPI int census_context_next_tag(census_context_iterator *iterator, census_tag *tag)
census_aggregation_ops census_agg_sum
const census_context * context
Definition: census.h:164
CENSUSAPI int census_supported(void)
Return the features supported by the current census implementation (not all features will be availabl...
CENSUSAPI int census_get_trace_record(census_trace_record *trace_record)
Get a trace record.
const char * key
Definition: census.h:92
CENSUSAPI void census_end_op(census_context *context, int status)
End an operation started by any of the census_start_*_op*() calls.
CENSUSAPI int census_initialize(int features)
Shutdown and startup census subsystem.
CENSUSAPI void census_set_trace_mask(int trace_mask)
Set the trace mask associated with a context.
Census view data as returned by census_view_get_data().
Definition: census.h:538
uint64_t trace_id
Definition: census.h:416
census_features
Definition: census.h:48
CENSUSAPI void census_set_rpc_client_peer(census_context *context, const char *peer)
Add peer information to a context representing a client RPC operation.
Definition: census.h:91
int n_propagated_binary_tags
Definition: census.h:119
#define CENSUSAPI
Definition: port_platform.h:367
Definition: time.h:63
const char * value
Definition: census.h:93
const census_context * tags
Definition: census.h:533
const void * create_arg
Definition: census.h:493
CENSUSAPI void census_context_initialize_iterator(const census_context *context, census_context_iterator *iterator)
CENSUSAPI census_view * census_view_create(uint32_t metric_id, const census_context *tags, const census_aggregation *aggregations, size_t naggregations)
Create a new view.
Definition: census.h:217
const char * buffer
Definition: census.h:419
size_t buf_size
Definition: census.h:420
census_aggregation_ops census_agg_window
CENSUSAPI census_context * census_start_client_rpc_op(const census_context *context, int64_t rpc_name_id, const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask, const census_timestamp *start_time)
Start a client rpc operation.
int index
Definition: census.h:166
CENSUSAPI census_timestamp census_start_rpc_op_timestamp(void)
Mark the beginning of an RPC operation.
Definition: census.h:163
struct census_context census_context
A Census Context is a handle used by Census to represent the current tracing and stats collection inf...
Definition: census.h:81
Definition: census.h:49