Skip to content

Commit a25127a

Browse files
committed
otlp: add test to validate compression header
Signed-off-by: inge4pres <[email protected]>
1 parent f841872 commit a25127a

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/otlp_test.zig

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ test "otlp HTTPClient uncompressed protobuf metrics payload" {
9595
try otlp.Export(allocator, config, otlp.Signal.Data{ .metrics = req });
9696
}
9797

98+
test "otlp HTTPClient compressed protobuf metrics payload" {
99+
const allocator = std.testing.allocator;
100+
101+
var server = try HTTPTestServer.init(allocator, assertCompressionHeaderGzip);
102+
defer server.deinit();
103+
104+
const thread = try std.Thread.spawn(.{}, HTTPTestServer.processSingleRequest, .{server});
105+
defer thread.join();
106+
107+
const config = try ConfigOptions.init(allocator);
108+
defer config.deinit();
109+
const endpoint = try std.fmt.allocPrint(allocator, "127.0.0.1:{d}", .{server.port()});
110+
defer allocator.free(endpoint);
111+
config.endpoint = endpoint;
112+
113+
config.compression = otlp.Compression.gzip;
114+
const req = try oneDataPointMetricsExportRequest(allocator);
115+
defer req.deinit();
116+
117+
try otlp.Export(allocator, config, otlp.Signal.Data{ .metrics = req });
118+
}
119+
98120
fn emptyMetricsExportRequest(allocator: std.mem.Allocator) !pbcollector_metrics.ExportMetricsServiceRequest {
99121
const rm = try allocator.alloc(pbmetrics.ResourceMetrics, 1);
100122
const rm0 = pbmetrics.ResourceMetrics{
@@ -179,12 +201,28 @@ fn assertUncompressedProtobufMetricsBodyCanBeParsed(request: *http.Server.Reques
179201
};
180202
defer proto.deinit();
181203
if (proto.resource_metrics.items.len != 1) {
182-
std.debug.print("otlp HTTP test - decoded protobuf: {}\n", proto);
204+
std.debug.print("otlp HTTP test - decoded protobuf: {}\n", .{proto});
183205
return AssertionError.ProtobufBodyMismatch;
184206
}
185207
try request.respond("", .{ .status = .ok });
186208
}
187209

210+
fn assertCompressionHeaderGzip(request: *http.Server.Request) anyerror!void {
211+
var headers = request.iterateHeaders();
212+
while (headers.next()) |header| {
213+
if (std.mem.eql(u8, header.name, "accept-encoding")) {
214+
const content_encoding = header.value;
215+
if (!std.mem.eql(u8, content_encoding, "gzip")) {
216+
std.debug.print("otlp HTTP test - compression header mismatch, want 'gzip' got '{s}'\n", .{content_encoding});
217+
return AssertionError.CompressionMismatch;
218+
}
219+
try request.respond("", .{ .status = .ok });
220+
return;
221+
}
222+
}
223+
return AssertionError.CompressionMismatch;
224+
}
225+
188226
const HTTPTestServer = struct {
189227
const Self = @This();
190228

0 commit comments

Comments
 (0)