blob: eea7ddf728328e707b8a422904a4cba1ede9b9dc [file] [log] [blame]
Alistair Veitch9686dab2015-05-26 14:26:47 -07001/*
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 CENSUS_CENSUS_H
39#define CENSUS_CENSUS_H
40
41#include <grpc/grpc.h>
Alistair Veitch9686dab2015-05-26 14:26:47 -070042
Nicolas "Pixel" Noble1ed15e22015-06-09 02:24:35 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Alistair Veitch9686dab2015-05-26 14:26:47 -070047/* Identify census functionality that can be enabled via census_initialize(). */
48enum census_functions {
49 CENSUS_NONE = 0, /* Do not enable census. */
50 CENSUS_TRACING = 1, /* Enable census tracing. */
51 CENSUS_STATS = 2, /* Enable Census stats collection. */
52 CENSUS_CPU = 4, /* Enable Census CPU usage collection. */
53 CENSUS_ALL = CENSUS_TRACING | CENSUS_STATS | CENSUS_CPU
54};
55
56/* Shutdown and startup census subsystem. The 'functions' argument should be
57 * the OR (|) of census_functions values. If census fails to initialize, then
Alistair Veitch26967622015-06-01 10:45:54 -070058 * census_initialize() will return a non-zero value. It is an error to call
59 * census_initialize() more than once (without an intervening
60 * census_shutdown()). */
Alistair Veitch9686dab2015-05-26 14:26:47 -070061int census_initialize(int functions);
62void census_shutdown();
63
Alistair Veitchfc62ddd2015-06-29 02:52:46 -070064/* If any census feature has been initialized, this funtion will return a
65 * non-zero value. */
66int census_available();
67
Alistair Veitch9686dab2015-05-26 14:26:47 -070068/* Internally, Census relies on a context, which should be propagated across
69 * RPC's. From the RPC subsystems viewpoint, this is an opaque data structure.
70 * A context must be used as the first argument to all other census
Alistair Veitch9d48ebf2015-06-01 10:01:03 -070071 * functions. Conceptually, contexts should be thought of as specific to
72 * single RPC/thread. The context can be serialized for passing across the
73 * wire. */
Alistair Veitch9686dab2015-05-26 14:26:47 -070074typedef struct census_context census_context;
75
76/* This function is called by the RPC subsystem whenever it needs to get a
77 * serialized form of the current census context (presumably to pass across
78 * the wire). Arguments:
79 * 'buffer': pointer to memory into which serialized context will be placed
80 * 'buf_size': size of 'buffer'
81 *
82 * Returns: the number of bytes used in buffer if successful, or 0 if the
83 * buffer is of insufficient size.
84 *
85 * TODO(aveitch): determine how best to communicate required/max buffer size
86 * so caller doesn't have to guess. */
87size_t census_context_serialize(const census_context *context, char *buffer,
88 size_t buf_size);
89
90/* Create a new census context, possibly from a serialized buffer. If 'buffer'
91 * is non-NULL, it is assumed that it is a buffer encoded by
92 * census_context_serialize(). If `buffer` is NULL, a new, empty context is
Alistair Veitch980ef762015-06-01 14:35:31 -070093 * created. The decoded/new contest is returned in 'context'.
Alistair Veitch9686dab2015-05-26 14:26:47 -070094 *
Alistair Veitch980ef762015-06-01 14:35:31 -070095 * Returns 0 if no errors, non-zero if buffer is incorrectly formatted, in
96 * which case a new empty context will be returned. */
97int census_context_deserialize(const char *buffer, census_context **context);
Alistair Veitch9686dab2015-05-26 14:26:47 -070098
99/* The given context is destroyed. Once destroyed, using the context in
100 * future census calls will result in undefined behavior. */
101void census_context_destroy(census_context *context);
102
Alistair Veitchf68bf382015-07-28 14:27:28 -0700103/* Max number of characters in tag key */
104#define CENSUS_MAX_TAG_KEY_LENGTH 20
105/* Max number of tag value characters */
106#define CENSUS_MAX_TAG_VALUE_LENGTH 50
107
108/* A Census tag set is a collection of key:value string pairs; these form the
109 basis against which Census metrics will be recorded. Keys are unique within
110 a tag set. All contexts have an associated tag set. */
111typedef struct census_tag_set census_tag_set;
112
113/* Returns a pointer to a newly created, empty tag set. If size_hint > 0,
114 indicates that the tag set is intended to hold approximately that number
115 of tags. */
116census_tag_set *census_tag_set_create(size_t size_hint);
117
118/* Add a new tag key/value to an existing tag set; if the tag key already exists
119 in the tag set, then its value is overwritten with the new one. Can also be
120 used to delete a tag, by specifying a NULL value. If key is NULL, returns
121 the number of tags in the tag set.
122 Return values:
123 -1: invalid length key or value
124 positive values: the number of tags in the tag set. */
125int census_tag_set_add(census_tag_set *tags, const char *key,
126 const char *value);
127
128/* Destroys a tag set. This function must be called to prevent memory leaks.
129 Once called, the tag set cannot be used again. */
130void census_tag_set_destroy(census_tag_set *tags);
131
132/* Get a contexts tag set. */
133census_tag_set *census_context_tag_set(census_context *context);
134
135/* A read-only representation of a tag for use by census clients. */
136typedef struct {
137 size_t key_len; /* Number of bytes in tag key. */
138 const char *key; /* A pointer to the tag key. May not be null-terminated. */
139 size_t value_len; /* Number of bytes in tag value. */
140 const char *value; /* Pointer to the tag value. May not be null-terminated. */
141} census_tag_const;
142
143/* Used to iterate through a tag sets contents. */
144typedef struct census_tag_set_iterator census_tag_set_iterator;
145
146/* Open a tag set for iteration. The tag set must not be modified while
147 iteration is ongoing. Returns an iterator for use in following functions. */
148census_tag_set_iterator *census_tag_set_open(census_tag_set *tags);
149
150/* Get the next tag in the tag set, by writing into the 'tag' argument. Returns
151 1 if there is a "next" tag, 0 if there are no more tags. */
152int census_tag_set_next(census_tag_set_iterator *it, census_tag_const *tag);
153
154/* Close an iterator opened by census_tag_set_open(). The iterator will be
155 invalidated, and should not be used once close is called. */
156void census_tag_set_close(census_tag_set_iterator *it);
157
Alistair Veitch851032a2015-07-20 11:59:13 -0700158/* A census statistic to be recorded comprises two parts: an ID for the
159 * particular statistic and the value to be recorded against it. */
160typedef struct {
161 int id;
162 double value;
163} census_stat;
164
165/* Record new stats against the given context. */
166void census_record_stat(census_context *context, census_stat *stats,
167 size_t nstats);
Alistair Veitch4d1589a2015-07-17 15:13:04 -0700168
Nicolas "Pixel" Noble1ed15e22015-06-09 02:24:35 +0200169#ifdef __cplusplus
170}
171#endif
172
Alistair Veitch9686dab2015-05-26 14:26:47 -0700173#endif /* CENSUS_CENSUS_H */