Repository navigation
Expand file tree
/
Copy pathhoneycomb.rs
More file actions
170 lines (140 loc) · 4.59 KB
/
Copy pathhoneycomb.rs
File metadata and controls
170 lines (140 loc) · 4.59 KB
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
166
167
168
169
170
use crate::{
config::{log_schema, DataType, GenerateConfig, SinkConfig, SinkContext, SinkDescription},
event::{Event, Value},
http::HttpClient,
sinks::util::{
http::{BatchedHttpSink, HttpSink},
BatchConfig, BatchSettings, BoxedRawValue, JsonArrayBuffer, TowerRequestConfig,
},
};
use futures::{FutureExt, SinkExt};
use http::{Request, StatusCode, Uri};
use serde::{Deserialize, Serialize};
use serde_json::json;
lazy_static::lazy_static! {
static ref HOST: Uri = Uri::from_static("https://api.honeycomb.io/1/batch");
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HoneycombConfig {
api_key: String,
// TODO: we probably want to make this a template
// but this limits us in how we can do our healthcheck.
dataset: String,
#[serde(default)]
batch: BatchConfig,
#[serde(default)]
request: TowerRequestConfig,
}
inventory::submit! {
SinkDescription::new::<HoneycombConfig>("honeycomb")
}
impl GenerateConfig for HoneycombConfig {
fn generate_config() -> toml::Value {
toml::from_str(
r#"api_key = "${HONEYCOMB_API_KEY}"
dataset = "my-honeycomb-dataset""#,
)
.unwrap()
}
}
#[async_trait::async_trait]
#[typetag::serde(name = "honeycomb")]
impl SinkConfig for HoneycombConfig {
async fn build(
&self,
cx: SinkContext,
) -> crate::Result<(super::VectorSink, super::Healthcheck)> {
let request_settings = self.request.unwrap_with(&TowerRequestConfig::default());
let batch_settings = BatchSettings::default()
.bytes(bytesize::kib(100u64))
.timeout(1)
.parse_config(self.batch)?;
let client = HttpClient::new(None)?;
let sink = BatchedHttpSink::new(
self.clone(),
JsonArrayBuffer::new(batch_settings.size),
request_settings,
batch_settings.timeout,
client.clone(),
cx.acker(),
)
.sink_map_err(|error| error!(message = "Fatal honeycomb sink error.", %error));
let healthcheck = healthcheck(self.clone(), client).boxed();
Ok((super::VectorSink::Sink(Box::new(sink)), healthcheck))
}
fn input_type(&self) -> DataType {
DataType::Log
}
fn sink_type(&self) -> &'static str {
"honeycomb"
}
}
#[async_trait::async_trait]
impl HttpSink for HoneycombConfig {
type Input = serde_json::Value;
type Output = Vec<BoxedRawValue>;
fn encode_event(&self, event: Event) -> Option<Self::Input> {
let mut log = event.into_log();
let timestamp = if let Some(Value::Timestamp(ts)) = log.remove(log_schema().timestamp_key())
{
ts
} else {
chrono::Utc::now()
};
Some(json!({
"timestamp": timestamp.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true),
"data": log.all_fields(),
}))
}
async fn build_request(&self, events: Self::Output) -> crate::Result<http::Request<Vec<u8>>> {
let uri = self.build_uri();
let request = Request::post(uri).header("X-Honeycomb-Team", self.api_key.clone());
let buf = serde_json::to_vec(&events).unwrap();
request.body(buf).map_err(Into::into)
}
}
impl HoneycombConfig {
fn build_uri(&self) -> Uri {
let uri = format!("{}/{}", HOST.clone(), self.dataset);
uri.parse::<http::Uri>()
.expect("This should be a valid uri")
}
}
async fn healthcheck(config: HoneycombConfig, client: HttpClient) -> crate::Result<()> {
let req = config
.build_request(Vec::new())
.await?
.map(hyper::Body::from);
let res = client.send(req).await?;
let status = res.status();
let body = hyper::body::to_bytes(res.into_body()).await?;
if status == StatusCode::BAD_REQUEST {
Ok(())
} else if status == StatusCode::UNAUTHORIZED {
let json: serde_json::Value = serde_json::from_slice(&body[..])?;
let message = if let Some(s) = json
.as_object()
.and_then(|o| o.get("error"))
.and_then(|s| s.as_str())
{
s.to_string()
} else {
"Token is not valid, 401 returned.".to_string()
};
Err(message.into())
} else {
let body = String::from_utf8_lossy(&body[..]);
Err(format!(
"Server returned unexpected error status: {} body: {}",
status, body
)
.into())
}
}
#[cfg(test)]
mod test {
#[test]
fn generate_config() {
crate::test_util::test_generate_config::<super::HoneycombConfig>();
}
}