GRPC Core  3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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 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 
64 CENSUSAPI int census_initialize(int features);
65 CENSUSAPI void census_shutdown(void);
66 
69 CENSUSAPI int census_supported(void);
70 
72 CENSUSAPI int census_enabled(void);
73 
84 
85 /* A tag is a key:value pair. Both keys and values are nil-terminated strings,
86  containing printable ASCII characters (decimal 32-126). Keys must be at
87  least one character in length. Both keys and values can have at most
88  CENSUS_MAX_TAG_KB_LEN characters (including the terminating nil). The
89  maximum number of tags that can be propagated is
90  CENSUS_MAX_PROPAGATED_TAGS. Users should also remember that some systems
91  may have limits on, e.g., the number of bytes that can be transmitted as
92  metadata, and that larger tags means more memory consumed and time in
93  processing. */
94 typedef struct {
95  const char *key;
96  const char *value;
97  uint8_t flags;
98 } census_tag;
99 
100 /* Maximum length of a tag's key or value. */
101 #define CENSUS_MAX_TAG_KV_LEN 255
102 /* Maximum number of propagatable tags. */
103 #define CENSUS_MAX_PROPAGATED_TAGS 255
104 
105 /* Tag flags. */
106 #define CENSUS_TAG_PROPAGATE 1 /* Tag should be propagated over RPC */
107 #define CENSUS_TAG_STATS 2 /* Tag will be used for statistics aggregation */
108 #define CENSUS_TAG_RESERVED 4 /* Reserved for internal use. */
109 /* Flag values 4,8,16,32,64,128 are reserved for future/internal use. Clients
110  should not use or rely on their values. */
111 
112 #define CENSUS_TAG_IS_PROPAGATED(flags) (flags & CENSUS_TAG_PROPAGATE)
113 #define CENSUS_TAG_IS_STATS(flags) (flags & CENSUS_TAG_STATS)
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 tags */
119  int n_local_tags; /* number of non-propagated (local) tags */
120  int n_deleted_tags; /* number of tags that were deleted */
121  int n_added_tags; /* number of tags that were added */
122  int n_modified_tags; /* number of tags that were modified */
123  int n_invalid_tags; /* number of tags with bad keys or values (e.g.
124  longer than CENSUS_MAX_TAG_KV_LEN) */
125  int n_ignored_tags; /* number of tags ignored because of
126  CENSUS_MAX_PROPAGATED_TAGS limit. */
128 
129 /* Create a new context, adding and removing tags from an existing context.
130  This will copy all tags from the 'tags' input, so it is recommended
131  to add as many tags in a single operation as is practical for the client.
132  @param base Base context to build upon. Can be NULL.
133  @param tags A set of tags to be added/changed/deleted. Tags with keys that
134  are in 'tags', but not 'base', are added to the context. Keys that are in
135  both 'tags' and 'base' will have their value/flags modified. Tags with keys
136  in both, but with NULL values, will be deleted from the context. Tags with
137  invalid (too long or short) keys or values will be ignored.
138  If adding a tag will result in more than CENSUS_MAX_PROPAGATED_TAGS in either
139  binary or non-binary tags, they will be ignored, as will deletions of
140  tags that don't exist.
141  @param ntags number of tags in 'tags'
142  @param status If not NULL, will return a pointer to a census_context_status
143  structure containing information about the new context and status of the
144  tags used in its creation.
145  @return A new, valid census_context.
146 */
148  const census_context *base, const census_tag *tags, int ntags,
149  census_context_status const **status);
150 
151 /* Destroy a context. Once this function has been called, the context cannot
152  be reused. */
154 
155 /* Get a pointer to the original status from the context creation. */
157  const census_context *context);
158 
159 /* Structure used for iterating over the tags in a context. API clients should
160  not use or reference internal fields - neither their contents or
161  presence/absence are guaranteed. */
162 typedef struct {
164  int base;
165  int index;
166  char *kvm;
168 
169 /* Initialize a census_tag_iterator. Must be called before first use. */
171  const census_context *context, census_context_iterator *iterator);
172 
173 /* Get the contents of the "next" tag in the context. If there are no more
174  tags, returns 0 (and 'tag' contents will be unchanged), otherwise returns 1.
175  */
177  census_tag *tag);
178 
179 /* Get a context tag by key. Returns 0 if the key is not present. */
181  const char *key, census_tag *tag);
182 
183 /* Tag set encode/decode functionality. These functions are intended
184  for use by RPC systems only, for purposes of transmitting/receiving contexts.
185  */
186 
187 /* Encode a context into a buffer.
188  @param context context to be encoded
189  @param buffer buffer into which the context will be encoded.
190  @param buf_size number of available bytes in buffer.
191  @return The number of buffer bytes consumed for the encoded context, or
192  zero if the buffer was of insufficient size. */
193 CENSUSAPI size_t census_context_encode(const census_context *context,
194  char *buffer, size_t buf_size);
195 
196 /* Decode context buffer encoded with census_context_encode(). Returns NULL
197  if there is an error in parsing either buffer. */
199  size_t size);
200 
201 /* Distributed traces can have a number of options. */
203  CENSUS_TRACE_MASK_NONE = 0, /* Default, empty flags */
204  CENSUS_TRACE_MASK_IS_SAMPLED = 1 /* RPC tracing enabled for this context. */
205 };
206 
209 CENSUSAPI int census_trace_mask(const census_context *context);
210 
212 CENSUSAPI void census_set_trace_mask(int trace_mask);
213 
214 /* The concept of "operation" is a fundamental concept for Census. In an RPC
215  system, an operation typically represents a single RPC, or a significant
216  sub-part thereof (e.g. a single logical "read" RPC to a distributed storage
217  system might do several other actions in parallel, from looking up metadata
218  indices to making requests of other services - each of these could be a
219  sub-operation with the larger RPC operation). Census uses operations for the
220  following:
221 
222  CPU accounting: If enabled, census will measure the thread CPU time
223  consumed between operation start and end times.
224 
225  Active operations: Census will maintain information on all currently
226  active operations.
227 
228  Distributed tracing: Each operation serves as a logical trace span.
229 
230  Stats collection: Stats are broken down by operation (e.g. latency
231  breakdown for each unique RPC path).
232 
233  The following functions serve to delineate the start and stop points for
234  each logical operation. */
235 
240 typedef struct {
241  /* Use gpr_timespec for default implementation. High performance
242  * implementations should use a cycle-counter based timestamp. */
245 
261 
273 typedef struct {
274  const char *(*get_rpc_service_name)(int64_t id);
275  const char *(*get_rpc_method_name)(int64_t id);
277 
313  const census_context *context, int64_t rpc_name_id,
314  const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
315  const census_timestamp *start_time);
316 
321  const char *peer);
322 
342  const char *buffer, int64_t rpc_name_id,
343  const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
344  census_timestamp *start_time);
345 
372  const char *family, const char *name,
373  int trace_mask);
374 
384 CENSUSAPI void census_end_op(census_context *context, int status);
385 
386 #define CENSUS_TRACE_RECORD_START_OP ((uint32_t)0)
387 #define CENSUS_TRACE_RECORD_END_OP ((uint32_t)1)
388 
396 CENSUSAPI void census_trace_print(census_context *context, uint32_t type,
397  const char *buffer, size_t n);
398 
400 typedef struct {
401  census_timestamp timestamp; /* Time of record creation */
402  uint64_t trace_id; /* Trace ID associated with record */
403  uint64_t op_id; /* Operation ID associated with record */
404  uint32_t type; /* Type (as used in census_trace_print() */
405  const char *buffer; /* Buffer (from census_trace_print() */
406  size_t buf_size; /* Number of bytes inside buffer */
408 
418 CENSUSAPI int census_trace_scan_start(int consume);
419 
430 
433 
434 /* Core stats collection API's. The following concepts are used:
435  * Resource: Users record measurements for a single resource. Examples
436  include RPC latency, CPU seconds consumed, and bytes transmitted.
437  * Aggregation: An aggregation of a set of measurements. Census supports the
438  following aggregation types:
439  * Distribution - statistical distribution information, used for
440  recording average, standard deviation etc. Can include a histogram.
441  * Interval - a count of events that happen in a rolling time window.
442  * View: A view is a combination of a Resource, a set of tag keys and an
443  Aggregation. When a measurement for a Resource matches the View tags, it is
444  recorded (for each unique set of tag values) using the Aggregation type.
445  Each resource can have an arbitrary number of views by which it will be
446  broken down.
447 
448  Census uses protos to define each of the above, and output results. This
449  ensures unification across the different language and runtime
450  implementations. The proto definitions can be found in src/proto/census.
451 */
452 
453 /* Define a new resource. `resource_pb` should contain an encoded Resource
454  protobuf, `resource_pb_size` being the size of the buffer. Returns a -ve
455  value on error, or a positive (>= 0) resource id (for use in
456  census_delete_resource() and census_record_values()). In order to be valid, a
457  resource must have a name, and at least one numerator in its unit type. The
458  resource name must be unique, and an error will be returned if it is not. */
459 CENSUSAPI int32_t census_define_resource(const uint8_t *resource_pb,
460  size_t resource_pb_size);
461 
462 /* Delete a resource created by census_define_resource(). */
463 CENSUSAPI void census_delete_resource(int32_t resource_id);
464 
465 /* Determine the id of a resource, given its name. returns -1 if the resource
466  does not exist. */
467 CENSUSAPI int32_t census_resource_id(const char *name);
468 
469 /* A single value to be recorded comprises two parts: an ID for the particular
470  * resource and the value to be recorded against it. */
471 typedef struct {
472  int32_t resource_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 
480 #ifdef __cplusplus
481 }
482 #endif
483 
484 #endif /* GRPC_CENSUS_H */
int n_modified_tags
Definition: census.h:122
CENSUSAPI int census_enabled(void)
Return the census features currently enabled.
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.
Definition: census.h:471
double value
Definition: census.h:473
int32_t resource_id
Definition: census.h:472
Definition: census.h:117
CENSUSAPI const census_context_status * census_context_get_status(const census_context *context)
int n_added_tags
Definition: census.h:121
Definition: census.h:50
CENSUSAPI int census_context_get_tag(const census_context *context, const char *key, census_tag *tag)
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:120
int n_invalid_tags
Definition: census.h:123
Definition: census.h:52
Trace record.
Definition: census.h:400
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:202
uint8_t flags
Definition: census.h:97
Represent functions to map RPC name ID to service/method names.
Definition: census.h:273
Definition: census.h:53
uint64_t op_id
Definition: census.h:403
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.
int n_local_tags
Definition: census.h:119
int n_ignored_tags
Definition: census.h:125
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:166
CENSUSAPI void census_context_destroy(census_context *context)
CENSUSAPI size_t census_context_encode(const census_context *context, char *buffer, size_t buf_size)
census_timestamp timestamp
Definition: census.h:401
gpr_timespec ts
Definition: census.h:243
uint32_t type
Definition: census.h:404
int base
Definition: census.h:164
This structure represents a timestamp as used by census to record the time at which an operation begi...
Definition: census.h:240
Definition: census.h:204
CENSUSAPI census_context * census_context_decode(const char *buffer, size_t size)
CENSUSAPI int census_context_next_tag(census_context_iterator *iterator, census_tag *tag)
CENSUSAPI void census_delete_resource(int32_t resource_id)
const census_context * context
Definition: census.h:163
CENSUSAPI int census_supported(void)
Return the features supported by the current census implementation (not all features will be availabl...
CENSUSAPI int32_t census_resource_id(const char *name)
CENSUSAPI int census_get_trace_record(census_trace_record *trace_record)
Get a trace record.
const char * key
Definition: census.h:95
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.
uint64_t trace_id
Definition: census.h:402
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:94
CENSUSAPI int32_t census_define_resource(const uint8_t *resource_pb, size_t resource_pb_size)
#define CENSUSAPI
Definition: port_platform.h:418
Definition: gpr_types.h:63
const char * value
Definition: census.h:96
CENSUSAPI void census_context_initialize_iterator(const census_context *context, census_context_iterator *iterator)
Definition: census.h:203
const char * buffer
Definition: census.h:405
size_t buf_size
Definition: census.h:406
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:165
CENSUSAPI census_timestamp census_start_rpc_op_timestamp(void)
Mark the beginning of an RPC operation.
Definition: census.h:162
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:83
Definition: census.h:49