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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::collections::HashMap;

use vector_lib::lookup::path;
use vector_lib::{
    config::{LegacyKey, LogNamespace},
    event::Event,
};

use crate::sources::http_server::HttpConfigParamKind;

pub fn add_query_parameters(
    events: &mut [Event],
    query_parameters_config: &[HttpConfigParamKind],
    query_parameters: &HashMap<String, String>,
    log_namespace: LogNamespace,
    source_name: &'static str,
) {
    for qp in query_parameters_config {
        match qp {
            // Add each non-wildcard containing query_parameter that was specified
            // in the `query_parameters` config option to the event if an exact match
            // is found.
            HttpConfigParamKind::Exact(query_parameter_name) => {
                let value = query_parameters.get(query_parameter_name);

                for event in events.iter_mut() {
                    if let Event::Log(log) = event {
                        log_namespace.insert_source_metadata(
                            source_name,
                            log,
                            Some(LegacyKey::Overwrite(path!(query_parameter_name))),
                            path!("query_parameters", query_parameter_name),
                            crate::event::Value::from(value.map(String::to_owned)),
                        );
                    }
                }
            }
            // Add all query_parameters that match against wildcard pattens specified
            // in the `query_parameters` config option to the event.
            HttpConfigParamKind::Glob(query_parameter_pattern) => {
                for query_parameter_name in query_parameters.keys() {
                    if query_parameter_pattern
                        .matches_with(query_parameter_name.as_str(), glob::MatchOptions::default())
                    {
                        let value = query_parameters.get(query_parameter_name);

                        for event in events.iter_mut() {
                            if let Event::Log(log) = event {
                                log_namespace.insert_source_metadata(
                                    source_name,
                                    log,
                                    Some(LegacyKey::Overwrite(path!(query_parameter_name))),
                                    path!("query_parameters", query_parameter_name),
                                    crate::event::Value::from(value.map(String::to_owned)),
                                );
                            }
                        }
                    }
                }
            }
        };
    }
}

#[cfg(test)]
mod tests {
    use crate::event::LogEvent;
    use crate::sources::{http_server::HttpConfigParamKind, util::add_query_parameters};

    use vector_lib::config::LogNamespace;
    use vrl::{path, value};

    #[test]
    fn multiple_query_params() {
        let query_params_names = [
            HttpConfigParamKind::Exact("param1".into()),
            HttpConfigParamKind::Exact("param2".into()),
        ];
        let query_params = [
            ("param1".into(), "value1".into()),
            ("param2".into(), "value2".into()),
            ("param3".into(), "value3".into()),
        ]
        .into();

        let mut base_log = [LogEvent::from(value!({})).into()];
        add_query_parameters(
            &mut base_log,
            &query_params_names,
            &query_params,
            LogNamespace::Legacy,
            "test",
        );
        let mut namespaced_log = [LogEvent::from(value!({})).into()];
        add_query_parameters(
            &mut namespaced_log,
            &query_params_names,
            &query_params,
            LogNamespace::Vector,
            "test",
        );

        assert_eq!(
            base_log[0].as_log().value(),
            namespaced_log[0]
                .metadata()
                .value()
                .get(path!("test", "query_parameters"))
                .unwrap()
        );
    }
    #[test]
    fn multiple_query_params_wildcard() {
        let query_params_names = [HttpConfigParamKind::Glob(glob::Pattern::new("*").unwrap())];
        let query_params = [
            ("param1".into(), "value1".into()),
            ("param2".into(), "value2".into()),
            ("param3".into(), "value3".into()),
        ]
        .into();

        let mut base_log = [LogEvent::from(value!({})).into()];
        add_query_parameters(
            &mut base_log,
            &query_params_names,
            &query_params,
            LogNamespace::Legacy,
            "test",
        );
        let mut namespaced_log = [LogEvent::from(value!({})).into()];
        add_query_parameters(
            &mut namespaced_log,
            &query_params_names,
            &query_params,
            LogNamespace::Vector,
            "test",
        );

        let log = base_log[0].as_log();
        assert_eq!(
            log.value(),
            namespaced_log[0]
                .metadata()
                .value()
                .get(path!("test", "query_parameters"))
                .unwrap(),
            "Checking legacy and namespaced log contain query parameters string"
        );
        assert_eq!(
            log["param1"],
            "value1".into(),
            "Checking log contains first query parameter"
        );
        assert_eq!(
            log["param2"],
            "value2".into(),
            "Checking log contains second query parameter"
        );
        assert_eq!(
            log["param3"],
            "value3".into(),
            "Checking log contains third query parameter"
        );
    }
}