Beispiel #1
0
/* Tests census noop stubs in a simulated rpc flow */
void test_census_stubs(void) {
  census_op_id op_id;
  census_rpc_stats* stats = census_rpc_stats_create_empty();
  census_aggregated_rpc_stats data_map = {0, NULL};

  /* Initializes census library at server start up time. */
  census_init();
  /* Starts tracing at the beginning of a rpc. */
  op_id = census_tracing_start_op();
  /* Appends custom annotations on a trace object. */
  census_tracing_print(op_id, "annotation foo");
  census_tracing_print(op_id, "annotation bar");
  /* Appends method tag on the trace object. */
  census_add_method_tag(op_id, "service_foo/method.bar");
  /* Either record client side stats or server side stats associated with the
     op_id. Here for testing purpose, we record both. */
  census_record_rpc_client_stats(op_id, stats);
  census_record_rpc_server_stats(op_id, stats);
  /* Ends a tracing. */
  census_tracing_end_op(op_id);
  /* In process stats queries. */
  census_get_server_stats(&data_map);
  census_aggregated_rpc_stats_set_empty(&data_map);
  census_get_client_stats(&data_map);
  census_aggregated_rpc_stats_set_empty(&data_map);
  gpr_free(stats);
  census_shutdown();
}
Beispiel #2
0
static void test_create_and_destroy(void) {
  census_rpc_stats *stats = NULL;
  census_aggregated_rpc_stats agg_stats = {0, NULL};

  stats = census_rpc_stats_create_empty();
  GPR_ASSERT(stats != NULL);
  GPR_ASSERT(stats->cnt == 0 && stats->rpc_error_cnt == 0 &&
             stats->app_error_cnt == 0 && stats->elapsed_time_ms == 0.0 &&
             stats->api_request_bytes == 0 && stats->wire_request_bytes == 0 &&
             stats->api_response_bytes == 0 && stats->wire_response_bytes == 0);
  gpr_free(stats);

  census_aggregated_rpc_stats_set_empty(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 0);
  GPR_ASSERT(agg_stats.stats == NULL);
  agg_stats.num_entries = 1;
  agg_stats.stats = (census_per_method_rpc_stats *)gpr_malloc(
      sizeof(census_per_method_rpc_stats));
  agg_stats.stats[0].method = gpr_strdup("foo");
  census_aggregated_rpc_stats_set_empty(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 0);
  GPR_ASSERT(agg_stats.stats == NULL);
}
Beispiel #3
0
/* Get stats from input stats store */
static void get_stats(census_ht *store, census_aggregated_rpc_stats *data) {
  GPR_ASSERT(data != NULL);
  if (data->num_entries != 0) {
    census_aggregated_rpc_stats_set_empty(data);
  }
  gpr_mu_lock(&g_mu);
  if (store != NULL) {
    size_t n;
    unsigned i, j;
    gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
    census_ht_kv *kv = census_ht_get_all_elements(store, &n);
    if (kv != NULL) {
      data->num_entries = n;
      data->stats =
          (per_method_stats *)gpr_malloc(sizeof(per_method_stats) * n);
      for (i = 0; i < n; i++) {
        census_window_stats_sums sums[NUM_INTERVALS];
        for (j = 0; j < NUM_INTERVALS; j++) {
          sums[j].statistic = (void *)census_rpc_stats_create_empty();
        }
        data->stats[i].method = gpr_strdup(kv[i].k.ptr);
        census_window_stats_get_sums(kv[i].v, now, sums);
        data->stats[i].minute_stats =
            *(census_rpc_stats *)sums[MINUTE_INTERVAL].statistic;
        data->stats[i].hour_stats =
            *(census_rpc_stats *)sums[HOUR_INTERVAL].statistic;
        data->stats[i].total_stats =
            *(census_rpc_stats *)sums[TOTAL_INTERVAL].statistic;
        for (j = 0; j < NUM_INTERVALS; j++) {
          gpr_free(sums[j].statistic);
        }
      }
      gpr_free(kv);
    }
  }
  gpr_mu_unlock(&g_mu);
}