Fix : update the matric arrtibute

This commit is contained in:
Manendra Pal Singh
2026-02-24 13:35:44 +05:30
parent c2f3dbce2c
commit 18303eb1b5
3 changed files with 28 additions and 14 deletions

View File

@@ -4,12 +4,9 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"sync" "sync"
"time"
"github.com/beckn-one/beckn-onix/pkg/telemetry" "github.com/beckn-one/beckn-onix/pkg/telemetry"
"github.com/google/uuid"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric" "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) m, err := GetHTTPMetrics(ctx)
if err != nil || m == nil { if err != nil || m == nil {
return return
@@ -76,7 +73,8 @@ func RecordHTTPRequest(ctx context.Context, statusCode int, action, role, caller
telemetry.AttrHTTPStatus.String(status), telemetry.AttrHTTPStatus.String(status),
telemetry.AttrAction.String(action), telemetry.AttrAction.String(action),
telemetry.AttrRole.String(role), telemetry.AttrRole.String(role),
telemetry.AttrCaller.String(caller), telemetry.AttrSenderID.String(senderID),
telemetry.AttrRecipientID.String(recipientID),
} }
metric_code := action + "_api_total_count" metric_code := action + "_api_total_count"
@@ -110,11 +108,9 @@ func specHttpMetricAttr(metricCode, category string) []attribute.KeyValue {
granularity, frequency := telemetry.GetNetworkMetricsConfig() granularity, frequency := telemetry.GetNetworkMetricsConfig()
return []attribute.KeyValue{ return []attribute.KeyValue{
telemetry.AttrMetricUUID.String(uuid.New().String()),
telemetry.AttrMetricCode.String(metricCode), telemetry.AttrMetricCode.String(metricCode),
telemetry.AttrMetricCategory.String(category), telemetry.AttrMetricCategory.String(category),
telemetry.AttrMetricGranularity.String(granularity), telemetry.AttrMetricGranularity.String(granularity),
telemetry.AttrMetricFrequency.String(frequency), telemetry.AttrMetricFrequency.String(frequency),
telemetry.AttrObservedTimeUnixNano.String(strconv.FormatInt(time.Now().UnixNano(), 10)),
} }
} }

View File

@@ -122,14 +122,23 @@ func (h *stdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
record: nil, record: nil,
} }
caller := "unknown" selfID := h.SubscriberID
if v, ok := r.Context().Value(model.ContextKeyCallerID).(string); ok && v != "" { remoteID := ""
caller = v 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()) httpMeter, _ := GetHTTPMetrics(r.Context())
if httpMeter != nil { if httpMeter != nil {
recordOnce = func() { 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 wrapped.record = recordOnce
} }
@@ -152,7 +161,7 @@ func (h *stdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
body := stepCtx.Body 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() span.End()
}() }()
@@ -392,8 +401,8 @@ func setBecknAttr(span trace.Span, r *http.Request, h *stdHandler) {
receiverID = selfID receiverID = selfID
} }
attrs := []attribute.KeyValue{ attrs := []attribute.KeyValue{
attribute.String("recipient.id", receiverID), telemetry.AttrRecipientID.String(receiverID),
attribute.String("sender.id", senderID), telemetry.AttrSenderID.String(senderID),
attribute.String("span_uuid", uuid.New().String()), attribute.String("span_uuid", uuid.New().String()),
attribute.String("http.request.method", r.Method), attribute.String("http.request.method", r.Method),
attribute.String("http.route", r.URL.Path), attribute.String("http.route", r.URL.Path),
@@ -418,3 +427,10 @@ func setBecknAttr(span trace.Span, r *http.Request, h *stdHandler) {
span.SetAttributes(attrs...) span.SetAttributes(attrs...)
} }
func errString(e error) string {
if e == nil {
return ""
}
return e.Error()
}

View File

@@ -51,6 +51,8 @@ var (
AttrMetricFrequency = attribute.Key("metric.frequency") AttrMetricFrequency = attribute.Key("metric.frequency")
AttrObservedTimeUnixNano = attribute.Key("observedTimeUnixNano") AttrObservedTimeUnixNano = attribute.Key("observedTimeUnixNano")
AttrMatricLabels = attribute.Key("metric.labels") AttrMatricLabels = attribute.Key("metric.labels")
AttrSenderID = attribute.Key("sender.id")
AttrRecipientID = attribute.Key("recipient.id")
) )
var ( var (