Skip to content

Commit

Permalink
feat: add NS selections metric (#124)
Browse files Browse the repository at this point in the history
* feat: add NSSF metrics counters

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

* chore: remove unused metrics

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

---------

Signed-off-by: Dario Faccin <[email protected]>
  • Loading branch information
dariofaccin authored Oct 1, 2024
1 parent 795f957 commit 98dd785
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
37 changes: 37 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,50 @@ import (
"net/http"

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

// NssfStats captures NSSF stats
type NssfStats struct {
nssfNsSelections *prometheus.CounterVec
}

var nssfStats *NssfStats

func initNssfStats() *NssfStats {
return &NssfStats{
nssfNsSelections: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "nssf_ns_selections",
Help: "Counter of total NS selection queries",
}, []string{"target_nf_type", "nf_id", "result"}),
}
}

func (ps *NssfStats) register() error {
if err := prometheus.Register(ps.nssfNsSelections); err != nil {
return err
}
return nil
}

func init() {
nssfStats = initNssfStats()

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

// InitMetrics initialises NSSF 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)
}
}

// IncrementNssfNsSelectionsStats increments number of total NS selection queries
func IncrementNssfNsSelectionsStats(targetNfType, nfId, result string) {
nssfStats.nssfNsSelections.WithLabelValues(targetNfType, nfId, result).Inc()
}
21 changes: 21 additions & 0 deletions producer/network_slice_information_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/omec-project/nssf/logger"
stats "github.com/omec-project/nssf/metrics"
"github.com/omec-project/nssf/plugin"
"github.com/omec-project/nssf/util"
"github.com/omec-project/openapi/models"
Expand Down Expand Up @@ -101,15 +102,21 @@ func HandleNSSelectionGet(request *httpwrapper.Request) *httpwrapper.Response {

response, problemDetails := NSSelectionGetProcedure(query)

nfType := GetNfTypeFromQueryParameters(query)
nfId := GetNfIdFromQueryParameters(query)

if response != nil {
stats.IncrementNssfNsSelectionsStats(nfType, nfId, "SUCCESS")
return httpwrapper.NewResponse(http.StatusOK, nil, response)
} else if problemDetails != nil {
stats.IncrementNssfNsSelectionsStats(nfType, nfId, "FAILURE")
return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails)
}
problemDetails = &models.ProblemDetails{
Status: http.StatusForbidden,
Cause: "UNSPECIFIED",
}
stats.IncrementNssfNsSelectionsStats(nfType, nfId, "FAILURE")
return httpwrapper.NewResponse(http.StatusForbidden, nil, problemDetails)
}

Expand Down Expand Up @@ -164,3 +171,17 @@ func NSSelectionGetProcedure(query url.Values) (*models.AuthorizedNetworkSliceIn
return response, problemDetails
}
}

func GetNfTypeFromQueryParameters(query url.Values) (nfType string) {
if query.Get("nf-type") != "" {
return query.Get("nf-type")
}
return "UNKNOWN_NF"
}

func GetNfIdFromQueryParameters(query url.Values) (nfId string) {
if query.Get("nf-id") != "" {
return query.Get("nf-id")
}
return "UNKNOWN_NF_ID"
}

0 comments on commit 98dd785

Please sign in to comment.