Skip to content

Commit

Permalink
feat: add UDR metrics counters (#128)
Browse files Browse the repository at this point in the history
* feat: add UDR metrics counters

Signed-off-by: Dario Faccin <[email protected]>

* fix: typo in util function

Signed-off-by: Dario Faccin <[email protected]>

* fix: handle correctly error

Signed-off-by: Dario Faccin <[email protected]>

* fix: typo in profile registration log

Signed-off-by: Dario Faccin <[email protected]>

* chore: improve error handling

Signed-off-by: Dario Faccin <[email protected]>

---------

Signed-off-by: Dario Faccin <[email protected]>
  • Loading branch information
dariofaccin authored Oct 11, 2024
1 parent 6657e75 commit 262b573
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 63 deletions.
2 changes: 1 addition & 1 deletion datarepository/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ var subRoutes = Routes{
HTTPGetOdbData,
},

// Sepcial case
// Special case
{
"HTTPRemovesubscriptionDataSubscriptions",
strings.ToUpper("Delete"),
Expand Down
63 changes: 63 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,76 @@ import (
"net/http"

"github.com/omec-project/udr/logger"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// UdrStats captures UDR stats
type UdrStats struct {
udrSubscriptionData *prometheus.CounterVec
udrApplicationData *prometheus.CounterVec
udrPolicyData *prometheus.CounterVec
}

var udrStats *UdrStats

func initUdrStats() *UdrStats {
return &UdrStats{
udrSubscriptionData: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "udr_subscription_data",
Help: "Counter of total Subscription data queries",
}, []string{"query_type", "resource_type", "result"}),
udrApplicationData: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "udr_application_data",
Help: "Counter of total Application data queries",
}, []string{"query_type", "resource_type", "result"}),
udrPolicyData: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "udr_policy_data",
Help: "Counter of total Policy data queries",
}, []string{"query_type", "resource_type", "result"}),
}
}

func (ps *UdrStats) register() error {
if err := prometheus.Register(ps.udrSubscriptionData); err != nil {
return err
}
if err := prometheus.Register(ps.udrApplicationData); err != nil {
return err
}
if err := prometheus.Register(ps.udrPolicyData); err != nil {
return err
}
return nil
}

func init() {
udrStats = initUdrStats()

if err := udrStats.register(); err != nil {
logger.InitLog.Errorln("UDR Stats register failed")
}
}

// InitMetrics initializes UDR metrics
func InitMetrics() {
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":8080", nil); err != nil {
logger.InitLog.Errorf("Could not open metrics port: %v", err)
}
}

// IncrementUdrSubscriptionDataStats increments number of total Subscription data queries
func IncrementUdrSubscriptionDataStats(queryType, resourceType, result string) {
udrStats.udrSubscriptionData.WithLabelValues(queryType, resourceType, result).Inc()
}

// IncrementUdrApplicationDataStats increments number of total Application data queries
func IncrementUdrApplicationDataStats(queryType, resourceType, result string) {
udrStats.udrApplicationData.WithLabelValues(queryType, resourceType, result).Inc()
}

// IncrementUdrPolicyDataStats increments number of total Policy data queries
func IncrementUdrPolicyDataStats(queryType, resourceType, result string) {
udrStats.udrPolicyData.WithLabelValues(queryType, resourceType, result).Inc()
}
Loading

0 comments on commit 262b573

Please sign in to comment.