From 18303eb1b5441f9d70d9506210be5e74cd303678 Mon Sep 17 00:00:00 2001 From: Manendra Pal Singh Date: Tue, 24 Feb 2026 13:35:44 +0530 Subject: [PATCH] Fix : update the matric arrtibute --- core/module/handler/http_metric.go | 10 +++------- core/module/handler/stdHandler.go | 30 +++++++++++++++++++++++------- pkg/telemetry/pluginMetrics.go | 2 ++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/module/handler/http_metric.go b/core/module/handler/http_metric.go index 54e3323..f52fa2f 100644 --- a/core/module/handler/http_metric.go +++ b/core/module/handler/http_metric.go @@ -4,12 +4,9 @@ import ( "context" "fmt" "net/http" - "strconv" "sync" - "time" "github.com/beckn-one/beckn-onix/pkg/telemetry" - "github.com/google/uuid" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" @@ -66,7 +63,7 @@ func StatusClass(statusCode int) string { } } -func RecordHTTPRequest(ctx context.Context, statusCode int, action, role, caller string) { +func RecordHTTPRequest(ctx context.Context, statusCode int, action, role, senderID, recipientID string) { m, err := GetHTTPMetrics(ctx) if err != nil || m == nil { return @@ -76,7 +73,8 @@ func RecordHTTPRequest(ctx context.Context, statusCode int, action, role, caller telemetry.AttrHTTPStatus.String(status), telemetry.AttrAction.String(action), telemetry.AttrRole.String(role), - telemetry.AttrCaller.String(caller), + telemetry.AttrSenderID.String(senderID), + telemetry.AttrRecipientID.String(recipientID), } metric_code := action + "_api_total_count" @@ -110,11 +108,9 @@ func specHttpMetricAttr(metricCode, category string) []attribute.KeyValue { granularity, frequency := telemetry.GetNetworkMetricsConfig() return []attribute.KeyValue{ - telemetry.AttrMetricUUID.String(uuid.New().String()), telemetry.AttrMetricCode.String(metricCode), telemetry.AttrMetricCategory.String(category), telemetry.AttrMetricGranularity.String(granularity), telemetry.AttrMetricFrequency.String(frequency), - telemetry.AttrObservedTimeUnixNano.String(strconv.FormatInt(time.Now().UnixNano(), 10)), } } diff --git a/core/module/handler/stdHandler.go b/core/module/handler/stdHandler.go index facb668..6c2831a 100644 --- a/core/module/handler/stdHandler.go +++ b/core/module/handler/stdHandler.go @@ -122,14 +122,23 @@ func (h *stdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { record: nil, } - caller := "unknown" - if v, ok := r.Context().Value(model.ContextKeyCallerID).(string); ok && v != "" { - caller = v + selfID := h.SubscriberID + remoteID := "" + if v, ok := r.Context().Value(model.ContextKeyCallerID).(string); ok { + remoteID = v + } + var senderID, receiverID string + if strings.Contains(h.moduleName, "Caller") { + senderID = selfID + receiverID = remoteID + } else { + senderID = remoteID + receiverID = selfID } httpMeter, _ := GetHTTPMetrics(r.Context()) if httpMeter != nil { recordOnce = func() { - RecordHTTPRequest(r.Context(), wrapped.statusCode, r.URL.Path, string(h.role), caller) + RecordHTTPRequest(r.Context(), wrapped.statusCode, r.URL.Path, string(h.role), senderID, receiverID) } wrapped.record = recordOnce } @@ -152,7 +161,7 @@ func (h *stdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } body := stepCtx.Body - go telemetry.EmitAuditLogs(r.Context(), body, auditlog.Int("http.response.status_code", wrapped.statusCode)) + go telemetry.EmitAuditLogs(r.Context(), body, auditlog.Int("http.response.status_code", wrapped.statusCode), auditlog.String("http.response.error", errString(err))) span.End() }() @@ -392,8 +401,8 @@ func setBecknAttr(span trace.Span, r *http.Request, h *stdHandler) { receiverID = selfID } attrs := []attribute.KeyValue{ - attribute.String("recipient.id", receiverID), - attribute.String("sender.id", senderID), + telemetry.AttrRecipientID.String(receiverID), + telemetry.AttrSenderID.String(senderID), attribute.String("span_uuid", uuid.New().String()), attribute.String("http.request.method", r.Method), attribute.String("http.route", r.URL.Path), @@ -418,3 +427,10 @@ func setBecknAttr(span trace.Span, r *http.Request, h *stdHandler) { span.SetAttributes(attrs...) } + +func errString(e error) string { + if e == nil { + return "" + } + return e.Error() +} diff --git a/pkg/telemetry/pluginMetrics.go b/pkg/telemetry/pluginMetrics.go index 7bb73c8..a26601b 100644 --- a/pkg/telemetry/pluginMetrics.go +++ b/pkg/telemetry/pluginMetrics.go @@ -51,6 +51,8 @@ var ( AttrMetricFrequency = attribute.Key("metric.frequency") AttrObservedTimeUnixNano = attribute.Key("observedTimeUnixNano") AttrMatricLabels = attribute.Key("metric.labels") + AttrSenderID = attribute.Key("sender.id") + AttrRecipientID = attribute.Key("recipient.id") ) var (