<commit_before>#include <stdlib.h>
#include <uv.h>
#include <estragon.h>

static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
  return uv_buf_init((char*) malloc(suggested_size), suggested_size);
}

void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf, const char* type) {
  char* str;
  estragon_metric_t* metric;

  if (nread == -1) {
    //
    // EOF. We should probably notify logging server, but ignore for now.
    //
    return;
  }

  metric = estragon_new_metric();

  str = malloc((nread + 1) * sizeof(char));
  memcpy(str, rdbuf.base, nread);
  str[nread] = '\0';

  metric->metric = 1.0;
  metric->description = str;
  metric->service = (type == "stdout") ? "logs/stdout" : "logs/stderr";

  estragon_send(metric);

  free(str);
  free(rdbuf.base);
}

void on_stdout_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf) {
  on_read(tcp, nread, rdbuf, "stdout");
}

void on_stderr_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf) {
  on_read(tcp, nread, rdbuf, "stderr");
}
<commit_msg>[fix] Do not leak metric objects<commit_after>#include <stdlib.h>
#include <uv.h>
#include <estragon.h>

static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
  return uv_buf_init((char*) malloc(suggested_size), suggested_size);
}

void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf, const char* type) {
  char* str;
  estragon_metric_t* metric;

  if (nread == -1) {
    //
    // EOF. We should probably notify logging server, but ignore for now.
    //
    return;
  }

  metric = estragon_new_metric();

  str = malloc((nread + 1) * sizeof(char));
  memcpy(str, rdbuf.base, nread);
  str[nread] = '\0';

  metric->metric = 1.0;
  metric->description = str;
  metric->service = (type == "stdout") ? "logs/stdout" : "logs/stderr";

  estragon_send(metric);

  estragon_free_metric(metric);
  free(str);
  free(rdbuf.base);
}

void on_stdout_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf) {
  on_read(tcp, nread, rdbuf, "stdout");
}

void on_stderr_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf) {
  on_read(tcp, nread, rdbuf, "stderr");
}

{'fraction_non_alphanumeric': 0.14813061997160434, 'fraction_numerical': 0.00473260766682442, 'mean_word_length': 4.271820448877805, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '612969', 'n_tokens_mistral': 880, 'n_tokens_neox': 846, 'n_words': 186}