syntax = "proto3";

package conduit.proxy.telemetry;

import "common/common.proto";

/// Telemetry Service ///
//
// Reports request metadata.

service Telemetry {
  rpc Report(ReportRequest) returns (ReportResponse) {}
}

message ReportRequest {

  Process process = 1;

  enum Proxy {
    INBOUND = 0;
    OUTBOUND = 1;
  }
  Proxy proxy = 2;

  repeated ServerTransport server_transports = 3;
  repeated ClientTransport client_transports = 4;

  repeated RequestScope requests = 5;

  // The inclusive upper bound of each bucket in the response latency histogram,
  // in tenths of a millisecond.
  //
  // Each ResponseScope message will contain an array of numbers representing
  // the number of observed response latencies in each bucket of the latency
  // histogram. Since the structure of the latency histogram will be the same
  // across all ResponseScopes, we only need to report the max values for these
  // buckets a single time.
  repeated uint32 histogram_bucket_bounds_tenth_ms = 6;
}

message Process {
  string node = 1;
  string scheduled_instance = 2;
  string scheduled_namespace = 3;
}

message ServerTransport {
  common.IPAddress source_ip = 1;
  uint32 connects = 2;
  repeated TransportSummary disconnects = 3;
  common.Protocol protocol = 4;
}

message ClientTransport {
  common.TcpAddress target_addr = 1;
  uint32 connects = 2;
  repeated TransportSummary disconnects = 3;
  common.Protocol protocol = 4;
}

message TransportSummary {
  uint64 duration_ms = 1;
  uint64 bytes_sent = 2;
}

message RequestScope {
  RequestCtx ctx = 1;
  uint32 count = 2;
  repeated ResponseScope responses = 3;
}

message RequestCtx {
  common.IPAddress source_ip = 1;
  common.TcpAddress target_addr = 2;
  string authority = 3;
}

message ResponseScope {
  ResponseCtx ctx = 1;
  // Response latencies (time from request headers sent to response headers
  // received). Represented as a histogram with buckets whose inclusive
  // upper bounds are given in the `histogram_bucket_bounds_tenths_ms` array in
  // `ReportRequest`. Each number in this array represents the number of times a
  // latency falling into that bucket was observed.
  repeated uint32 response_latency_counts = 2;
  repeated EosScope ends = 3;
}

message ResponseCtx {
  uint32 http_status_code = 1;
}

message EosScope {
  EosCtx ctx = 1;
  uint32 streams = 2;
}

message EosCtx {
  oneof end {
    uint32 grpc_status_code = 1;
    uint32 reset_error_code = 2;
    bool other = 3; // Stream ended without reset and without grpc status code
  }
}

message ReportResponse {}
