Skip to content

Commit

Permalink
Fix panic on concurrent map read/write (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
lammel committed May 14, 2024
1 parent 452c4a2 commit c6a00b8
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var metricSet *metrics.Set

// Fix missing cmpGrp label when C5 component is shutdown
var gCmpGrp map[string]MetricAttribute
var gDc map[string]MetricAttribute
var attributesMtx sync.Mutex
var gDc map[string]MetricAttribute
var attributesMtx sync.RWMutex

type eventCounter struct {
ID string
Expand Down Expand Up @@ -98,7 +98,7 @@ func buildMetricName(prefix string, name string, attrs []MetricAttribute) string
if len(attrs) > 0 {
labels := string("{")
for _, v := range attrs {
if (len(v.name) > 0 && len(v.value) > 0) {
if len(v.name) > 0 && len(v.value) > 0 {
labels += v.name + string(`="`) + v.value + `",`
}
}
Expand Down Expand Up @@ -484,21 +484,21 @@ func processC5StateCounter(prefix string, lines []interface{}, attrs []MetricAtt
// processC5CounterMetrics will parse a counter output of type EVENT and USAGE for
// a specific C5 metric.
//
// {
// "proxyResponseTimeStampAndState:" : "2021-02-25 10:31:48 active",
// "clusterInfo" : "DC=1 {Wien} CompGrpId=31 [VAS-1] (masterId=8)",
// "counterName" : "BT_CALLS_LIMIT_REACHED",
// "counterType" : "EVENT",
// "absoluteValue" : 0,
// "currentValue" : 0,
// "lastValue" : 0,
// "tableValues" : [
// "name absolute curr last",
// "trunkname1.ipcentrex.internal 0 0 0",
// "trunk2.otherprovider.at 0 0 0",
// ],
// "tableCountInfo" : "curComponentCount2: 14 (10000) "
// }
// {
// "proxyResponseTimeStampAndState:" : "2021-02-25 10:31:48 active",
// "clusterInfo" : "DC=1 {Wien} CompGrpId=31 [VAS-1] (masterId=8)",
// "counterName" : "BT_CALLS_LIMIT_REACHED",
// "counterType" : "EVENT",
// "absoluteValue" : 0,
// "currentValue" : 0,
// "lastValue" : 0,
// "tableValues" : [
// "name absolute curr last",
// "trunkname1.ipcentrex.internal 0 0 0",
// "trunk2.otherprovider.at 0 0 0",
// ],
// "tableCountInfo" : "curComponentCount2: 14 (10000) "
// }
func processC5CounterMetrics(basePrefix string, data c5CounterResponse, attrs []MetricAttribute) {
const event, usage string = "EVENT", "USAGE"
prefix := basePrefix + "_" + strings.ToLower(data.CounterName)
Expand Down Expand Up @@ -565,6 +565,9 @@ func processBaseMetrics(prefix string, state c5StateResponse, attrs []MetricAttr
}

func getGlobalAttrs(prefix string) []MetricAttribute {
attributesMtx.RLock()
defer attributesMtx.RUnlock()

var tmpAttrs []MetricAttribute
tmpAttrs = append(tmpAttrs, gCmpGrp[prefix])
tmpAttrs = append(tmpAttrs, gDc[prefix])
Expand Down Expand Up @@ -856,7 +859,7 @@ func main() {
}
})

if (conf.SIPProxydExtEnabled) {
if conf.SIPProxydExtEnabled {
// dedicated endpoint for per-service-provider metrics
http.HandleFunc("/metrics-extended", func(httpResponse http.ResponseWriter, req *http.Request) {
metricSet = metrics.NewSet()
Expand Down

0 comments on commit c6a00b8

Please sign in to comment.