GRPC Core  1.0.0
census.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, 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 GRPC_CENSUS_H
39 #define GRPC_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. Both keys and values are nil-terminated strings,
84  containing printable ASCII characters (decimal 32-126). Keys must be at
85  least one character in length. Both keys and values can have at most
86  CENSUS_MAX_TAG_KB_LEN characters (including the terminating nil). The
87  maximum number of tags that can be propagated is
88  CENSUS_MAX_PROPAGATED_TAGS. Users should also remember that some systems
89  may have limits on, e.g., the number of bytes that can be transmitted as
90  metadata, and that larger tags means more memory consumed and time in
91  processing. */
92 typedef struct {
93  const char *key;
94  const char *value;
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_RESERVED 4 /* Reserved for internal use. */
107 /* Flag values 4,8,16,32,64,128 are reserved for future/internal use. Clients
108  should not use or rely on their values. */
109 
110 #define CENSUS_TAG_IS_PROPAGATED(flags) (flags & CENSUS_TAG_PROPAGATE)
111 #define CENSUS_TAG_IS_STATS(flags) (flags & CENSUS_TAG_STATS)
112 
113 /* An instance of this structure is kept by every context, and records the
114  basic information associated with the creation of that context. */
115 typedef struct {
116  int n_propagated_tags; /* number of propagated tags */
117  int n_local_tags; /* number of non-propagated (local) tags */
118  int n_deleted_tags; /* number of tags that were deleted */
119  int n_added_tags; /* number of tags that were added */
120  int n_modified_tags; /* number of tags that were modified */
121  int n_invalid_tags; /* number of tags with bad keys or values (e.g.
122  longer than CENSUS_MAX_TAG_KV_LEN) */
123  int n_ignored_tags; /* number of tags ignored because of
124  CENSUS_MAX_PROPAGATED_TAGS limit. */
126 
127 /* Create a new context, adding and removing tags from an existing context.
128  This will copy all tags from the 'tags' input, so it is recommended
129  to add as many tags in a single operation as is practical for the client.
130  @param base Base context to build upon. Can be NULL.
131  @param tags A set of tags to be added/changed/deleted. Tags with keys that
132  are in 'tags', but not 'base', are added to the context. Keys that are in
133  both 'tags' and 'base' will have their value/flags modified. Tags with keys
134  in both, but with NULL values, will be deleted from the context. Tags with
135  invalid (too long or short) keys or values will be ignored.
136  If adding a tag will result in more than CENSUS_MAX_PROPAGATED_TAGS in either
137  binary or non-binary tags, they will be ignored, as will deletions of
138  tags that don't exist.
139  @param ntags number of tags in 'tags'
140  @param status If not NULL, will return a pointer to a census_context_status
141  structure containing information about the new context and status of the
142  tags used in its creation.
143  @return A new, valid census_context.
144 */
146  const census_context *base, const census_tag *tags, int ntags,
147  census_context_status const **status);
148 
149 /* Destroy a context. Once this function has been called, the context cannot
150  be reused. */
152 
153 /* Get a pointer to the original status from the context creation. */
155  const census_context *context);
156 
157 /* Structure used for iterating over the tegs in a context. API clients should
158  not use or reference internal fields - neither their contents or
159  presence/absence are guaranteed. */
160 typedef struct {
162  int base;
163  int index;
164  char *kvm;
166 
167 /* Initialize a census_tag_iterator. Must be called before first use. */
169  const census_context *context, census_context_iterator *iterator);
170 
171 /* Get the contents of the "next" tag in the context. If there are no more
172  tags, returns 0 (and 'tag' contents will be unchanged), otherwise returns 1.
173  */
175  census_tag *tag);
176 
177 /* Get a context tag by key. Returns 0 if the key is not present. */
179  const char *key, census_tag *tag);
180 
181 /* Tag set encode/decode functionality. These functionas are intended
182  for use by RPC systems only, for purposes of transmitting/receiving contexts.
183  */
184 
185 /* Encode a context into a buffer.
186  @param context context to be encoded
187  @param buffer buffer into which the context will be encoded.
188  @param buf_size number of available bytes in buffer.
189  @return The number of buffer bytes consumed for the encoded context, or
190  zero if the buffer was of insufficient size. */
191 CENSUSAPI size_t census_context_encode(const census_context *context,
192  char *buffer, size_t buf_size);
193 
194 /* Decode context buffer encoded with census_context_encode(). Returns NULL
195  if there is an error in parsing either buffer. */
197  size_t size);
198 
199 /* Distributed traces can have a number of options. */
201  CENSUS_TRACE_MASK_NONE = 0, /* Default, empty flags */
202  CENSUS_TRACE_MASK_IS_SAMPLED = 1 /* RPC tracing enabled for this context. */
203 };
204 
207 CENSUSAPI int census_trace_mask(const census_context *context);
208 
210 CENSUSAPI void census_set_trace_mask(int trace_mask);
211 
212 /* The concept of "operation" is a fundamental concept for Census. In an RPC
213  system, and operation typcially represents a single RPC, or a significant
214  sub-part thereof (e.g. a single logical "read" RPC to a distributed storage
215  system might do several other actions in parallel, from looking up metadata
216  indices to making requests of other services - each of these could be a
217  sub-operation with the larger RPC operation). Census uses operations for the
218  following:
219 
220  CPU accounting: If enabled, census will measure the thread CPU time
221  consumed between operation start and end times.
222 
223  Active operations: Census will maintain information on all currently
224  active operations.
225 
226  Distributed tracing: Each operation serves as a logical trace span.
227 
228  Stats collection: Stats are broken down by operation (e.g. latency
229  breakdown for each unique RPC path).
230 
231  The following functions serve to delineate the start and stop points for
232  each logical operation. */
233 
238 typedef struct {
239  /* Use gpr_timespec for default implementation. High performance
240  * implementations should use a cycle-counter based timestamp. */
243 
259 
271 typedef struct {
272  const char *(*get_rpc_service_name)(int64_t id);
273  const char *(*get_rpc_method_name)(int64_t id);
275 
311  const census_context *context, int64_t rpc_name_id,
312  const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
313  const census_timestamp *start_time);
314 
319  const char *peer);
320 
340  const char *buffer, int64_t rpc_name_id,
341  const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
342  census_timestamp *start_time);
343 
370  const char *family, const char *name,
371  int trace_mask);
372 
382 CENSUSAPI void census_end_op(census_context *context, int status);
383 
384 #define CENSUS_TRACE_RECORD_START_OP ((uint32_t)0)
385 #define CENSUS_TRACE_RECORD_END_OP ((uint32_t)1)
386 
394 CENSUSAPI void census_trace_print(census_context *context, uint32_t type,
395  const char *buffer, size_t n);
396 
398 typedef struct {
399  census_timestamp timestamp; /* Time of record creation */
400  uint64_t trace_id; /* Trace ID associated with record */
401  uint64_t op_id; /* Operation ID associated with record */
402  uint32_t type; /* Type (as used in census_trace_print() */
403  const char *buffer; /* Buffer (from census_trace_print() */
404  size_t buf_size; /* Number of bytes inside buffer */
406 
416 CENSUSAPI int census_trace_scan_start(int consume);
417 
428 
431 
432 /* Core stats collection API's. The following concepts are used:
433  * Aggregation: A collection of values. Census supports the following
434  aggregation types:
435  Sum - a single summation type. Typically used for keeping (e.g.)
436  counts of events.
437  Distribution - statistical distribution information, used for
438  recording average, standard deviation etc.
439  Histogram - a histogram of measurements falling in defined bucket
440  boundaries.
441  Window - a count of events that happen in reolling time window.
442  New aggregation types can be added by the user, if desired (see
443  census_register_aggregation()).
444  * Metric: Each measurement is for a single metric. Examples include RPC
445  latency, CPU seconds consumed, and bytes transmitted.
446  * View: A view is a combination of a metric, a tag set (in which the tag
447  values are regular expressions) and a set of aggregations. When a
448  measurement for a metric matches the view tags, it is recorded (for each
449  unique set of tags) against each aggregation. Each metric can have an
450  arbitrary number of views by which it will be broken down.
451 */
452 
453 /* A single value to be recorded comprises two parts: an ID for the particular
454  * metric and the value to be recorded against it. */
455 typedef struct {
456  uint32_t metric_id;
457  double value;
458 } census_value;
459 
460 /* Record new usage values against the given context. */
462  census_value *values, size_t nvalues);
463 
466 
467 /* Predefined aggregation types, for use with census_view_create(). */
472 
475 typedef struct {
477  const void *create_arg; /* Aaggregation initialization argument. */
479 
481 typedef struct census_view census_view;
482 
492 /* TODO(aveitch): consider if context is the right argument type to pass in
493  tags. */
495  uint32_t metric_id, const census_context *tags,
496  const census_aggregation *aggregations, size_t naggregations);
497 
500 
502 CENSUSAPI size_t census_view_metric(const census_view *view);
503 
506 
509 
512  const census_view *view);
513 
516 typedef struct {
517  const census_context *tags; /* Tags for this set of aggregations. */
518  const void **data; /* One data set for every aggregation in the view. */
520 
522 typedef struct {
523  size_t n_tag_sets; /* Number of unique tag sets that matched view. */
524  const census_view_aggregation_data *data; /* n_tag_sets entries */
526 
532 
535 
536 #ifdef __cplusplus
537 }
538 #endif
539 
540 #endif /* GRPC_CENSUS_H */
int n_modified_tags
Definition: census.h:120
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:116
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:465
Definition: census.h:455
double value
Definition: census.h:457
CENSUSAPI size_t census_view_metric(const census_view *view)
Metric ID associated with a view.
Definition: census.h:115
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:481
int n_added_tags
Definition: census.h:119
Definition: census.h:50
Holds all the aggregation data for a particular view instantiation.
Definition: census.h:516
const void ** data
Definition: census.h:518
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:524
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:118
Information needed to instantiate a new aggregation.
Definition: census.h:475
int n_invalid_tags
Definition: census.h:121
Definition: census.h:52
Trace record.
Definition: census.h:398
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:200
uint8_t flags
Definition: census.h:95
Represent functions to map RPC name ID to service/method names.
Definition: census.h:271
Definition: census.h:53
uint64_t op_id
Definition: census.h:401
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:476
int n_local_tags
Definition: census.h:117
CENSUSAPI const census_context * census_view_tags(const census_view *view)
Get tags associated with view.
int n_ignored_tags
Definition: census.h:123
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:164
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:523
CENSUSAPI size_t census_context_encode(const census_context *context, char *buffer, size_t buf_size)
census_timestamp timestamp
Definition: census.h:399
gpr_timespec ts
Definition: census.h:241
uint32_t type
Definition: census.h:402
census_aggregation_ops census_agg_distribution
int base
Definition: census.h:162
This structure represents a timestamp as used by census to record the time at which an operation begi...
Definition: census.h:238
uint32_t metric_id
Definition: census.h:456
Definition: census.h:202
CENSUSAPI census_context * census_context_decode(const char *buffer, size_t size)
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:161
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:93
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:522
uint64_t trace_id
Definition: census.h:400
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:92
#define CENSUSAPI
Definition: port_platform.h:471
Definition: time.h:63
const char * value
Definition: census.h:94
const census_context * tags
Definition: census.h:517
const void * create_arg
Definition: census.h:477
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:201
const char * buffer
Definition: census.h:403
size_t buf_size
Definition: census.h:404
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:163
CENSUSAPI census_timestamp census_start_rpc_op_timestamp(void)
Mark the beginning of an RPC operation.
Definition: census.h:160
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