1
#pragma once
2

            
3
#include <chrono>
4
#include <string>
5
#include <vector>
6

            
7
#include "envoy/extensions/access_loggers/open_telemetry/v3/logs_service.pb.h"
8
#include "envoy/formatter/http_formatter_context.h"
9
#include "envoy/local_info/local_info.h"
10
#include "envoy/stats/stats_macros.h"
11
#include "envoy/stream_info/filter_state.h"
12
#include "envoy/stream_info/stream_info.h"
13

            
14
#include "source/common/common/hex.h"
15
#include "source/common/tracing/custom_tag_impl.h"
16

            
17
#include "absl/strings/escaping.h"
18
#include "absl/strings/str_cat.h"
19
#include "absl/strings/string_view.h"
20
#include "opentelemetry/proto/collector/logs/v1/logs_service.pb.h"
21
#include "opentelemetry/proto/common/v1/common.pb.h"
22
#include "opentelemetry/proto/logs/v1/logs.pb.h"
23

            
24
namespace Envoy {
25
namespace Extensions {
26
namespace AccessLoggers {
27
namespace OpenTelemetry {
28

            
29
// Default buffer flush interval (1 second).
30
constexpr std::chrono::milliseconds DefaultBufferFlushInterval{1000};
31
// Default max buffer size (16KB).
32
constexpr uint64_t DefaultMaxBufferSizeBytes = 16384;
33

            
34
// Key used to pack/unpack the body AnyValue to a KeyValueList.
35
constexpr absl::string_view BodyKey = "body";
36

            
37
// OpenTelemetry trace ID length in hex (128-bit = 32 hex chars).
38
constexpr size_t TraceIdHexLength = 32;
39
// Zipkin-style trace ID length in hex (64-bit = 16 hex chars).
40
constexpr size_t ShortTraceIdHexLength = 16;
41

            
42
constexpr absl::string_view OtlpAccessLogStatsPrefix = "access_logs.open_telemetry_access_log.";
43

            
44
#define ALL_OTLP_ACCESS_LOG_STATS(COUNTER)                                                         \
45
19
  COUNTER(logs_written)                                                                            \
46
19
  COUNTER(logs_dropped)
47

            
48
struct OtlpAccessLogStats {
49
  ALL_OTLP_ACCESS_LOG_STATS(GENERATE_COUNTER_STRUCT)
50
};
51

            
52
// Creates a KeyValue protobuf with a string value.
53
opentelemetry::proto::common::v1::KeyValue getStringKeyValue(const std::string& key,
54
                                                             const std::string& value);
55

            
56
// Packs the body "AnyValue" to a "KeyValueList" with a single key.
57
::opentelemetry::proto::common::v1::KeyValueList
58
packBody(const ::opentelemetry::proto::common::v1::AnyValue& body);
59

            
60
// Unpacks the body "AnyValue" from a "KeyValueList".
61
::opentelemetry::proto::common::v1::AnyValue
62
unpackBody(const ::opentelemetry::proto::common::v1::KeyValueList& value);
63

            
64
// User-Agent header per OTLP specification.
65
const std::string& getOtlpUserAgentHeader();
66

            
67
// Populates trace context (trace_id, span_id) on a LogRecord.
68
// Handles 128-bit (32 hex chars) and 64-bit Zipkin-style (16 hex chars) trace IDs.
69
void populateTraceContext(opentelemetry::proto::logs::v1::LogRecord& log_entry,
70
                          const std::string& trace_id_hex, const std::string& span_id_hex);
71

            
72
// Returns log_name, with fallback to common_config.log_name.
73
const std::string& getLogName(
74
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
75
        config);
76

            
77
// Returns grpc_service, with fallback to common_config.grpc_service.
78
const envoy::config::core::v3::GrpcService& getGrpcService(
79
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
80
        config);
81

            
82
std::chrono::milliseconds getBufferFlushInterval(
83
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
84
        config);
85

            
86
uint64_t getBufferSizeBytes(
87
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
88
        config);
89

            
90
std::vector<std::string> getFilterStateObjectsToLog(
91
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
92
        config);
93

            
94
std::vector<Tracing::CustomTagConstSharedPtr> getCustomTags(
95
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
96
        config);
97

            
98
void addFilterStateToAttributes(const StreamInfo::StreamInfo& stream_info,
99
                                const std::vector<std::string>& filter_state_objects_to_log,
100
                                opentelemetry::proto::logs::v1::LogRecord& log_entry);
101

            
102
void addCustomTagsToAttributes(const std::vector<Tracing::CustomTagConstSharedPtr>& custom_tags,
103
                               const Formatter::Context& context,
104
                               const StreamInfo::StreamInfo& stream_info,
105
                               opentelemetry::proto::logs::v1::LogRecord& log_entry);
106

            
107
// Initializes the OTLP message root structure with resource attributes.
108
// Returns a pointer to the ScopeLogs where log records should be added.
109
opentelemetry::proto::logs::v1::ScopeLogs* initOtlpMessageRoot(
110
    opentelemetry::proto::collector::logs::v1::ExportLogsServiceRequest& message,
111
    const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&
112
        config,
113
    const LocalInfo::LocalInfo& local_info);
114

            
115
} // namespace OpenTelemetry
116
} // namespace AccessLoggers
117
} // namespace Extensions
118
} // namespace Envoy