forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_build.rs
More file actions
161 lines (142 loc) · 6.56 KB
/
Copy pathgrpc_build.rs
File metadata and controls
161 lines (142 loc) · 6.56 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
use std::collections::HashMap;
use std::path::Path;
use tempfile::TempDir;
const TONIC_OUT_DIR: &str = "src/proto/tonic";
const TONIC_PROTO_FILES: &[&str] = &[
"src/proto/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/trace/v1/trace.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/collector/trace/v1/trace_service.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/collector/metrics/v1/metrics_service.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/logs/v1/logs.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/collector/logs/v1/logs_service.proto",
"src/proto/tracez.proto",
];
const TONIC_INCLUDES: &[&str] = &["src/proto/opentelemetry-proto", "src/proto"];
#[test]
fn build_tonic() {
let before_build = build_content_map(TONIC_OUT_DIR, true);
let out_dir = TempDir::new().expect("failed to create temp dir to store the generated files");
// build the generated files into OUT_DIR for now so we don't have to touch the src unless we have to
let mut builder = tonic_build::configure()
.build_server(true)
.build_client(true)
.server_mod_attribute(".", "#[cfg(feature = \"gen-tonic\")]")
.client_mod_attribute(".", "#[cfg(feature = \"gen-tonic\")]")
.type_attribute(
".",
"#[cfg_attr(feature = \"with-schemars\", derive(schemars::JsonSchema))]",
)
.type_attribute(
".",
"#[cfg_attr(feature = \"with-serde\", derive(serde::Serialize, serde::Deserialize))]",
)
.type_attribute(
".",
"#[cfg_attr(feature = \"with-serde\", serde(rename_all = \"camelCase\"))]",
);
// optional numeric and String field need to default it to 0 otherwise JSON files without those field
// cannot deserialize
// we cannot add serde(default) to all generated types because enums cannot be annotated with serde(default)
for path in [
"trace.v1.Span",
"trace.v1.Span.Link",
"trace.v1.ScopeSpans",
"trace.v1.ResourceSpans",
"common.v1.InstrumentationScope",
"resource.v1.Resource",
"trace.v1.Span.Event",
"trace.v1.Status",
"logs.v1.LogRecord",
"logs.v1.ScopeLogs",
"logs.v1.ResourceLogs",
] {
builder = builder.type_attribute(
path,
"#[cfg_attr(feature = \"with-serde\", serde(default))]",
)
}
// special serializer and deserializer for traceId and spanId
// OTLP/JSON format uses hex string for traceId and spanId
// the proto file uses bytes for traceId and spanId
// Thus, special serializer and deserializer are needed
for path in [
"trace.v1.Span.trace_id",
"trace.v1.Span.span_id",
"trace.v1.Span.parent_span_id",
"logs.v1.LogRecord.span_id",
"logs.v1.LogRecord.trace_id",
] {
builder = builder
.field_attribute(path, "#[cfg_attr(feature = \"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_hex_string\", deserialize_with = \"crate::proto::serializers::deserialize_from_hex_string\"))]")
}
// special serializer and deserializer for timestamp
// OTLP/JSON format may uses string for timestamp
// the proto file uses u64 for timestamp
// Thus, special serializer and deserializer are needed
for path in [
"trace.v1.Span.start_time_unix_nano",
"trace.v1.Span.end_time_unix_nano",
"trace.v1.Span.Event.time_unix_nano",
"logs.v1.LogRecord.time_unix_nano",
"logs.v1.LogRecord.observed_time_unix_nano",
] {
builder = builder
.field_attribute(path, "#[cfg_attr(feature = \"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_u64_to_string\", deserialize_with = \"crate::proto::serializers::deserialize_string_to_u64\"))]")
}
// add custom serializer and deserializer for AnyValue
for path in ["common.v1.KeyValue.value", "logs.v1.LogRecord.body"] {
builder = builder
.field_attribute(path, "#[cfg_attr(feature =\"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_value\", deserialize_with = \"crate::proto::serializers::deserialize_from_value\"))]");
}
builder
.out_dir(out_dir.path())
.compile(TONIC_PROTO_FILES, TONIC_INCLUDES)
.expect("cannot compile protobuf using tonic");
let after_build = build_content_map(out_dir.path(), true);
ensure_files_are_same(before_build, after_build, TONIC_OUT_DIR);
}
fn build_content_map(path: impl AsRef<Path>, normalize_line_feed: bool) -> HashMap<String, String> {
std::fs::read_dir(path)
.expect("cannot open dictionary of generated files")
.flatten()
.map(|entry| {
let path = entry.path();
let file_name = path
.file_name()
.expect("file name should always exist for generated files");
let mut file_contents = std::fs::read_to_string(path.clone())
.expect("cannot read from existing generated file");
if normalize_line_feed {
file_contents = get_platform_specific_string(file_contents);
}
(file_name.to_string_lossy().to_string(), file_contents)
})
.collect()
}
/// Returns a String which uses the platform specific new line feed character.
fn get_platform_specific_string(input: String) -> String {
if cfg!(windows) && !input.ends_with("\r\n") && input.ends_with('\n') {
return input.replace('\n', "\r\n");
}
input
}
fn ensure_files_are_same(
before_build: HashMap<String, String>,
after_build: HashMap<String, String>,
target_dir: &'static str,
) {
if after_build == before_build {
return;
}
if std::env::var("CI").is_ok() {
panic!("generated file has changed but it's a CI environment, please rerun this test locally and commit the changes");
}
// if there is at least one changes we will just copy the whole directory over
for (file_name, content) in after_build {
std::fs::write(Path::new(target_dir).join(file_name), content)
.expect("cannot write to the proto generate file. If it's happening in CI env, please return the test locally and commit the change");
}
panic!("generated file has changed, please commit the change file and rerun the test");
}