|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use log::info; |
| 16 | +use proxy_wasm::traits::*; |
| 17 | +use proxy_wasm::types::*; |
| 18 | + |
| 19 | +#[derive(Copy, Clone)] |
| 20 | +struct HttpAuthMetrics { |
| 21 | + counter_example: u32, |
| 22 | + gauge_example: u32, |
| 23 | + histogram_example: u32, |
| 24 | +} |
| 25 | + |
| 26 | +struct HttpAuthRoot { |
| 27 | + metrics: HttpAuthMetrics, |
| 28 | + monitored_path: String, |
| 29 | +} |
| 30 | + |
| 31 | +struct HttpAuth { |
| 32 | + metrics: HttpAuthMetrics, |
| 33 | + monitored_path: String, |
| 34 | +} |
| 35 | + |
| 36 | +proxy_wasm::main! {{ |
| 37 | + proxy_wasm::set_log_level(LogLevel::Trace); |
| 38 | + proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { |
| 39 | + Box::new(HttpAuthRoot { |
| 40 | + metrics: HttpAuthMetrics { |
| 41 | + counter_example: 0, |
| 42 | + gauge_example: 0, |
| 43 | + histogram_example: 0, |
| 44 | + }, |
| 45 | + monitored_path: "".to_string(), |
| 46 | + }) |
| 47 | + }); |
| 48 | +}} |
| 49 | + |
| 50 | +impl Context for HttpAuth {} |
| 51 | +impl HttpContext for HttpAuth { |
| 52 | + fn on_http_request_headers(&mut self, num_headers: usize, _: bool) -> Action { |
| 53 | + self.record_metric(self.metrics.histogram_example, num_headers as u64) |
| 54 | + .unwrap(); |
| 55 | + |
| 56 | + match self.get_http_request_header(":path") { |
| 57 | + Some(path) if path == self.monitored_path => { |
| 58 | + info!("Monitored path {} accessed.", self.monitored_path); |
| 59 | + |
| 60 | + self.increment_metric(self.metrics.counter_example, 1) |
| 61 | + .unwrap(); |
| 62 | + |
| 63 | + Action::Continue |
| 64 | + } |
| 65 | + _ => Action::Continue, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action { |
| 70 | + let counter_value = self.get_metric(self.metrics.counter_example).unwrap(); |
| 71 | + let gauge_value = self.get_metric(self.metrics.gauge_example).unwrap(); |
| 72 | + // histogram retrieval isn't supported |
| 73 | + |
| 74 | + self.set_http_response_header("Powered-By", Some("proxy-wasm")); |
| 75 | + self.set_http_response_header("My-Counter", Some(format!("{}", counter_value).as_str())); |
| 76 | + self.set_http_response_header("My-Gauge", Some(format!("{}", gauge_value).as_str())); |
| 77 | + |
| 78 | + Action::Continue |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Context for HttpAuthRoot {} |
| 83 | + |
| 84 | +impl RootContext for HttpAuthRoot { |
| 85 | + fn on_configure(&mut self, _: usize) -> bool { |
| 86 | + self.metrics.counter_example = self |
| 87 | + .define_metric(MetricType::Counter, "counter_example") |
| 88 | + .expect("failed defining counter_example metric"); |
| 89 | + self.metrics.gauge_example = self |
| 90 | + .define_metric(MetricType::Gauge, "gauge_example") |
| 91 | + .expect("failed defining gauge_example metric"); |
| 92 | + self.metrics.histogram_example = self |
| 93 | + .define_metric(MetricType::Histogram, "histogram_example") |
| 94 | + .expect("failed defining histogram_example metric"); |
| 95 | + |
| 96 | + self.record_metric(self.metrics.gauge_example, 10).unwrap(); |
| 97 | + |
| 98 | + self.monitored_path = "/headers".to_string(); |
| 99 | + |
| 100 | + true |
| 101 | + } |
| 102 | + |
| 103 | + fn create_http_context(&self, _: u32) -> Option<Box<dyn HttpContext>> { |
| 104 | + Some(Box::new(HttpAuth { |
| 105 | + metrics: self.metrics, |
| 106 | + monitored_path: self.monitored_path.clone(), |
| 107 | + })) |
| 108 | + } |
| 109 | + |
| 110 | + fn get_type(&self) -> Option<ContextType> { |
| 111 | + Some(ContextType::HttpContext) |
| 112 | + } |
| 113 | +} |
0 commit comments