diff --git a/gateway/gateway-runtime/policy-engine/internal/analytics/analytics.go b/gateway/gateway-runtime/policy-engine/internal/analytics/analytics.go index 307bd46a8..02b876e39 100644 --- a/gateway/gateway-runtime/policy-engine/internal/analytics/analytics.go +++ b/gateway/gateway-runtime/policy-engine/internal/analytics/analytics.go @@ -229,29 +229,41 @@ func (c *Analytics) prepareAnalyticEvent(logEntry *v3.HTTPAccessLogEntry) *dto.E } properties := logEntry.GetCommonProperties() - if properties != nil && properties.TimeToLastUpstreamRxByte != nil && properties.TimeToFirstUpstreamTxByte != nil && properties.TimeToLastDownstreamTxByte != nil { + if properties != nil && properties.TimeToLastRxByte != nil && + properties.TimeToFirstUpstreamTxByte != nil && properties.TimeToFirstUpstreamRxByte != nil && + properties.TimeToLastUpstreamRxByte != nil && properties.TimeToLastDownstreamTxByte != nil { + + lastRx := + (properties.TimeToLastRxByte.Seconds * 1000) + + (int64(properties.TimeToLastRxByte.Nanos) / 1_000_000) + + firstUpTx := + (properties.TimeToFirstUpstreamTxByte.Seconds * 1000) + + (int64(properties.TimeToFirstUpstreamTxByte.Nanos) / 1_000_000) + + firstUpRx := + (properties.TimeToFirstUpstreamRxByte.Seconds * 1000) + + (int64(properties.TimeToFirstUpstreamRxByte.Nanos) / 1_000_000) - backendResponseRecvTimestamp := + lastUpRx := (properties.TimeToLastUpstreamRxByte.Seconds * 1000) + (int64(properties.TimeToLastUpstreamRxByte.Nanos) / 1_000_000) - backendRequestSendTimestamp := - (properties.TimeToFirstUpstreamTxByte.Seconds * 1000) + - (int64(properties.TimeToFirstUpstreamTxByte.Nanos) / 1_000_000) - - downstreamResponseSendTimestamp := + lastDownTx := (properties.TimeToLastDownstreamTxByte.Seconds * 1000) + (int64(properties.TimeToLastDownstreamTxByte.Nanos) / 1_000_000) - // Prepare Latencies - latencies := dto.Latencies{} - latencies.BackendLatency = backendResponseRecvTimestamp - backendRequestSendTimestamp - latencies.RequestMediationLatency = backendRequestSendTimestamp - latencies.ResponseLatency = downstreamResponseSendTimestamp - latencies.ResponseMediationLatency = downstreamResponseSendTimestamp - backendResponseRecvTimestamp + latencies := dto.Latencies{ + BackendLatency: lastUpRx - firstUpTx, + RequestMediationLatency: firstUpTx - lastRx, + ResponseLatency: lastDownTx - firstUpRx, + ResponseMediationLatency: lastDownTx - lastUpRx, + } + event.Latencies = &latencies } + // prepare metaInfo metaInfo := dto.MetaInfo{} if logEntry.GetCommonProperties().GetStreamId() != "" { diff --git a/gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go b/gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go index a5ac9626b..6523bb617 100644 --- a/gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go +++ b/gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go @@ -627,9 +627,11 @@ func createLogEntryWithMetadata(metadata map[string]string) *v3.HTTPAccessLogEnt func createLogEntryWithLatencies() *v3.HTTPAccessLogEntry { return &v3.HTTPAccessLogEntry{ CommonProperties: &v3.AccessLogCommon{ - TimeToFirstUpstreamTxByte: &durationpb.Duration{Seconds: 0, Nanos: 100000000}, // 100ms - TimeToLastUpstreamRxByte: &durationpb.Duration{Seconds: 0, Nanos: 200000000}, // 200ms - TimeToLastDownstreamTxByte: &durationpb.Duration{Seconds: 0, Nanos: 250000000}, // 250ms + TimeToLastRxByte: &durationpb.Duration{Seconds: 0, Nanos: 50000000}, // 50ms - request fully received from client + TimeToFirstUpstreamTxByte: &durationpb.Duration{Seconds: 0, Nanos: 100000000}, // 100ms - start sending to backend + TimeToFirstUpstreamRxByte: &durationpb.Duration{Seconds: 0, Nanos: 150000000}, // 150ms - start receiving from backend + TimeToLastUpstreamRxByte: &durationpb.Duration{Seconds: 0, Nanos: 200000000}, // 200ms - finish receiving from backend + TimeToLastDownstreamTxByte: &durationpb.Duration{Seconds: 0, Nanos: 250000000}, // 250ms - finish sending to client DownstreamRemoteAddress: &corev3.Address{ Address: &corev3.Address_SocketAddress{ SocketAddress: &corev3.SocketAddress{ diff --git a/gateway/gateway-runtime/policy-engine/internal/analytics/publishers/moesif.go b/gateway/gateway-runtime/policy-engine/internal/analytics/publishers/moesif.go index 3fe5a7af0..642ab2c9e 100644 --- a/gateway/gateway-runtime/policy-engine/internal/analytics/publishers/moesif.go +++ b/gateway/gateway-runtime/policy-engine/internal/analytics/publishers/moesif.go @@ -251,6 +251,34 @@ func (m *Moesif) Publish(event *dto.Event) { metadataMap["response_payload"] = responsePayload } + // responseContentType + if responseContentType, ok := event.Properties["responseContentType"]; ok && responseContentType != nil { + metadataMap["responseContentType"] = responseContentType + } + + // responseSize + if responseSize, ok := event.Properties["responseSize"]; ok && responseSize != nil { + metadataMap["responseSize"] = responseSize + } + + // Advanced latency info + if event.Latencies != nil { + metadataMap["backendLatency"] = event.Latencies.BackendLatency + metadataMap["requestMediationLatency"] = event.Latencies.RequestMediationLatency + metadataMap["responseLatency"] = event.Latencies.ResponseLatency + metadataMap["responseMediationLatency"] = event.Latencies.ResponseMediationLatency + } + + // commonName + if commonName, ok := event.Properties["commonName"]; ok && commonName != nil { + metadataMap["commonName"] = commonName + } + + // isEgress + if isEgress, ok := event.Properties["isEgress"]; ok && isEgress != nil { + metadataMap["isEgress"] = isEgress + } + // Determine user ID - use from event properties if available, otherwise anonymous userID := anonymous if userIDVal, ok := event.Properties[userIDPropertyKey]; ok {