can create matric in there respective module.

This commit is contained in:
Manendra Pal Singh
2025-11-24 10:25:26 +05:30
parent c38e7b75ca
commit 88eef682df
12 changed files with 352 additions and 158 deletions

View File

@@ -0,0 +1,68 @@
package handler
import (
"context"
"fmt"
"sync"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)
// HandlerMetrics exposes handler-related metric instruments.
type HandlerMetrics struct {
SignatureValidationsTotal metric.Int64Counter
SchemaValidationsTotal metric.Int64Counter
RoutingDecisionsTotal metric.Int64Counter
}
var (
handlerMetricsInstance *HandlerMetrics
handlerMetricsOnce sync.Once
handlerMetricsErr error
)
// GetHandlerMetrics lazily initializes handler metric instruments and returns a cached reference.
func GetHandlerMetrics(ctx context.Context) (*HandlerMetrics, error) {
handlerMetricsOnce.Do(func() {
handlerMetricsInstance, handlerMetricsErr = newHandlerMetrics()
})
return handlerMetricsInstance, handlerMetricsErr
}
func newHandlerMetrics() (*HandlerMetrics, error) {
meter := otel.GetMeterProvider().Meter(
"github.com/beckn-one/beckn-onix/handler",
metric.WithInstrumentationVersion("1.0.0"),
)
m := &HandlerMetrics{}
var err error
if m.SignatureValidationsTotal, err = meter.Int64Counter(
"beckn_signature_validations_total",
metric.WithDescription("Signature validation attempts"),
metric.WithUnit("{validation}"),
); err != nil {
return nil, fmt.Errorf("beckn_signature_validations_total: %w", err)
}
if m.SchemaValidationsTotal, err = meter.Int64Counter(
"beckn_schema_validations_total",
metric.WithDescription("Schema validation attempts"),
metric.WithUnit("{validation}"),
); err != nil {
return nil, fmt.Errorf("beckn_schema_validations_total: %w", err)
}
if m.RoutingDecisionsTotal, err = meter.Int64Counter(
"onix_routing_decisions_total",
metric.WithDescription("Routing decisions taken by handler"),
metric.WithUnit("{decision}"),
); err != nil {
return nil, fmt.Errorf("onix_routing_decisions_total: %w", err)
}
return m, nil
}

View File

@@ -301,7 +301,7 @@ func (h *stdHandler) initSteps(ctx context.Context, mgr PluginManager, cfg *Conf
instrumentedStep, wrapErr := telemetry.NewInstrumentedStep(s, step, h.moduleName)
if wrapErr != nil {
log.Warnf(ctx, "Failed to instrument step %s: %v", step, wrapErr)
h.steps = append(h.steps, s)
h.steps = append(h.steps, s)
continue
}
h.steps = append(h.steps, instrumentedStep)

View File

@@ -72,7 +72,7 @@ func (s *signStep) generateAuthHeader(subID, keyID string, createdAt, validTill
type validateSignStep struct {
validator definition.SignValidator
km definition.KeyManager
metrics *telemetry.Metrics
metrics *HandlerMetrics
}
// newValidateSignStep initializes and returns a new validate sign step.
@@ -83,7 +83,7 @@ func newValidateSignStep(signValidator definition.SignValidator, km definition.K
if km == nil {
return nil, fmt.Errorf("invalid config: KeyManager plugin not configured")
}
metrics, _ := telemetry.GetMetrics(context.Background())
metrics, _ := GetHandlerMetrics(context.Background())
return &validateSignStep{
validator: signValidator,
km: km,
@@ -193,7 +193,7 @@ func parseHeader(header string) (*authHeader, error) {
// validateSchemaStep represents the schema validation step.
type validateSchemaStep struct {
validator definition.SchemaValidator
metrics *telemetry.Metrics
metrics *HandlerMetrics
}
// newValidateSchemaStep creates and returns the validateSchema step after validation.
@@ -202,7 +202,7 @@ func newValidateSchemaStep(schemaValidator definition.SchemaValidator) (definiti
return nil, fmt.Errorf("invalid config: SchemaValidator plugin not configured")
}
log.Debug(context.Background(), "adding schema validator")
metrics, _ := telemetry.GetMetrics(context.Background())
metrics, _ := GetHandlerMetrics(context.Background())
return &validateSchemaStep{
validator: schemaValidator,
metrics: metrics,
@@ -238,7 +238,7 @@ func (s *validateSchemaStep) recordMetrics(ctx *model.StepContext, err error) {
// addRouteStep represents the route determination step.
type addRouteStep struct {
router definition.Router
metrics *telemetry.Metrics
metrics *HandlerMetrics
}
// newAddRouteStep creates and returns the addRoute step after validation.
@@ -246,7 +246,7 @@ func newAddRouteStep(router definition.Router) (definition.Step, error) {
if router == nil {
return nil, fmt.Errorf("invalid config: Router plugin not configured")
}
metrics, _ := telemetry.GetMetrics(context.Background())
metrics, _ := GetHandlerMetrics(context.Background())
return &addRouteStep{
router: router,
metrics: metrics,