File: otel-protobuf-formatter.hpp

package info (click to toggle)
syslog-ng 4.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,572 kB
  • sloc: ansic: 177,639; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (119 lines) | stat: -rw-r--r-- 4,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2023 Attila Szakacs
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#ifndef OTEL_PROTOBUF_FORMATTER_HPP
#define OTEL_PROTOBUF_FORMATTER_HPP

#include "syslog-ng.h"

#include "compat/cpp-start.h"
#include "logmsg/logmsg.h"
#include "value-pairs/value-pairs.h"
#include "compat/cpp-end.h"

#include "opentelemetry/proto/logs/v1/logs.pb.h"
#include "opentelemetry/proto/metrics/v1/metrics.pb.h"
#include "opentelemetry/proto/trace/v1/trace.pb.h"

namespace syslogng {
namespace grpc {
namespace otel {

enum MessageType : int
{
  UNKNOWN,
  LOG,
  METRIC,
  SPAN,
};

MessageType get_message_type(LogMessage *msg);

using google::protobuf::RepeatedPtrField;

using opentelemetry::proto::resource::v1::Resource;
using opentelemetry::proto::common::v1::InstrumentationScope;
using opentelemetry::proto::common::v1::KeyValue;
using opentelemetry::proto::common::v1::KeyValueList;
using opentelemetry::proto::logs::v1::LogRecord;
using opentelemetry::proto::metrics::v1::Metric;
using opentelemetry::proto::metrics::v1::Gauge;
using opentelemetry::proto::metrics::v1::Sum;
using opentelemetry::proto::metrics::v1::Histogram;
using opentelemetry::proto::metrics::v1::ExponentialHistogram;
using opentelemetry::proto::metrics::v1::Summary;
using opentelemetry::proto::metrics::v1::Exemplar;
using opentelemetry::proto::metrics::v1::NumberDataPoint;
using opentelemetry::proto::metrics::v1::SummaryDataPoint;
using opentelemetry::proto::metrics::v1::HistogramDataPoint;
using opentelemetry::proto::metrics::v1::ExponentialHistogramDataPoint;
using opentelemetry::proto::trace::v1::Span;

class ProtobufFormatter
{
public:
  ProtobufFormatter(GlobalConfig *cfg);

  static void get_metadata_for_syslog_ng(Resource &resource, std::string &resource_schema_url,
                                         InstrumentationScope &scope, std::string &scope_schema_url);
  bool get_metadata(LogMessage *msg, Resource &resource, std::string &resource_schema_url,
                    InstrumentationScope &scope, std::string &scope_schema_url);
  bool format(LogMessage *msg, LogRecord &log_record);
  void format_fallback(LogMessage *msg, LogRecord &log_record);
  void format_syslog_ng(LogMessage *msg, LogRecord &log_record);
  bool format(LogMessage *msg, Metric &metric);
  bool format(LogMessage *msg, Span &span);

private:
  void get_and_set_repeated_KeyValues(LogMessage *msg, const char *prefix, RepeatedPtrField<KeyValue> *key_values);
  bool get_resource_and_schema_url(LogMessage *msg, Resource &resource, std::string &schema_url);
  bool get_scope_and_schema_url(LogMessage *msg, InstrumentationScope &scope, std::string &schema_url);

  /* Metric */
  void add_exemplars(LogMessage *msg, std::string &key_buffer, RepeatedPtrField<Exemplar> *exemplars);
  void add_number_data_points(LogMessage *msg, const char *prefix, RepeatedPtrField<NumberDataPoint> *data_points);
  void set_metric_gauge_values(LogMessage *msg, Gauge *gauge);
  void set_metric_sum_values(LogMessage *msg, Sum *sum);
  void add_histogram_data_points(LogMessage *msg, const char *prefix,
                                 RepeatedPtrField<HistogramDataPoint> *data_points);
  void set_metric_histogram_values(LogMessage *msg, Histogram *histogram);
  void add_exponential_histogram_data_points(LogMessage *msg, const char *prefix,
                                             RepeatedPtrField<ExponentialHistogramDataPoint> *data_points);
  void set_metric_exponential_histogram_values(LogMessage *msg, ExponentialHistogram *exponential_histogram);
  void add_summary_data_points(LogMessage *msg, const char *prefix, RepeatedPtrField<SummaryDataPoint> *data_points);
  void set_metric_summary_values(LogMessage *msg, Summary *summary);

  /* syslog-ng */
  void set_syslog_ng_nv_pairs(LogMessage *msg, LogRecord &log_record);
  void set_syslog_ng_macros(LogMessage *msg, LogRecord &log_record);
  void set_syslog_ng_addresses(LogMessage *msg, LogRecord &log_record);
  void set_syslog_ng_address_attrs(GSockAddr *sa, KeyValueList *address_attrs, bool include_port);

private:
  GlobalConfig *cfg;
};

}
}
}

#endif