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
171
172
#![allow(clippy::derive_partial_eq_without_eq)]
#[allow(unreachable_pub)]
mod report {
tonic::include_proto!("report");
}
#[allow(unreachable_pub)]
mod agent {
tonic::include_proto!("agent");
}
pub(crate) mod server;
use std::error::Error;
use agent::reporter_client::ReporterClient;
pub(crate) use agent::*;
pub(crate) use prost::*;
pub(crate) use report::*;
use serde::ser::SerializeStruct;
use tokio::task::JoinError;
use tonic::codegen::http::uri::InvalidUri;
use tonic::transport::Channel;
use tonic::transport::Endpoint;
use tonic::Request;
use tonic::Response;
use tonic::Status;
#[derive(Debug)]
pub(crate) struct ReporterError {
source: Box<dyn Error + Send + Sync + 'static>,
msg: String,
}
impl std::error::Error for ReporterError {}
impl From<InvalidUri> for ReporterError {
fn from(error: InvalidUri) -> Self {
ReporterError {
msg: error.to_string(),
source: Box::new(error),
}
}
}
impl From<tonic::transport::Error> for ReporterError {
fn from(error: tonic::transport::Error) -> Self {
ReporterError {
msg: error.to_string(),
source: Box::new(error),
}
}
}
impl From<std::io::Error> for ReporterError {
fn from(error: std::io::Error) -> Self {
ReporterError {
msg: error.to_string(),
source: Box::new(error),
}
}
}
impl From<sys_info::Error> for ReporterError {
fn from(error: sys_info::Error) -> Self {
ReporterError {
msg: error.to_string(),
source: Box::new(error),
}
}
}
impl From<JoinError> for ReporterError {
fn from(error: JoinError) -> Self {
ReporterError {
msg: error.to_string(),
source: Box::new(error),
}
}
}
impl std::fmt::Display for ReporterError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"ReporterError: source: {}, message: {}",
self.source, self.msg
)
}
}
#[derive(Debug)]
pub(crate) struct Reporter {
client: ReporterClient<Channel>,
ep: Endpoint,
}
impl Reporter {
pub(crate) async fn try_new<T: AsRef<str>>(addr: T) -> Result<Self, ReporterError>
where
prost::bytes::Bytes: From<T>,
{
let ep = Endpoint::from_shared(addr)?;
let client = ReporterClient::connect(ep.clone()).await?;
Ok(Self { client, ep })
}
pub(crate) async fn reconnect(&mut self) -> Result<(), ReporterError> {
self.client = ReporterClient::connect(self.ep.clone()).await?;
Ok(())
}
pub(crate) async fn submit(
&mut self,
request: ReporterRequest,
) -> Result<Response<ReporterResponse>, Status> {
self.client.add(Request::new(request)).await
}
}
pub(crate) fn serialize_timestamp<S>(
timestamp: &Option<prost_types::Timestamp>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match timestamp {
Some(ts) => {
let mut ts_strukt = serializer.serialize_struct("Timestamp", 2)?;
ts_strukt.serialize_field("seconds", &ts.seconds)?;
ts_strukt.serialize_field("nanos", &ts.nanos)?;
ts_strukt.end()
}
None => serializer.serialize_none(),
}
}
#[cfg(not(windows))] #[test]
fn check_reports_proto_is_up_to_date() {
let proto_url = "https://usage-reporting.api.apollographql.com/proto/reports.proto";
let response = reqwest::blocking::get(proto_url).unwrap();
let content = response.text().unwrap();
assert!(
content == include_str!("proto/reports.proto"),
"Protobuf file is out of date. Run this command to update it:\n\n \
curl -f {proto_url} > apollo-router/src/spaceport/proto/reports.proto\n\n"
);
}