Skip to content

Commit

Permalink
concurrent map write handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed Oct 26, 2024
1 parent d397769 commit 6aa83aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
24 changes: 24 additions & 0 deletions pkg/transaction/metric/centralmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metric
import (
"fmt"
"strings"
"sync"
"time"

"github.com/Axway/agent-sdk/pkg/transaction/models"
Expand All @@ -11,17 +12,40 @@ import (
)

type groupedMetrics struct {
lock *sync.Mutex
counters map[string]metrics.Counter
histograms map[string]metrics.Histogram
}

func newGroupedMetric() groupedMetrics {
return groupedMetrics{
lock: &sync.Mutex{},
counters: make(map[string]metrics.Counter),
histograms: make(map[string]metrics.Histogram),
}
}

func (g groupedMetrics) getOrCreateHistogram(key string) metrics.Histogram {
g.lock.Lock()
defer g.lock.Unlock()

if _, ok := g.histograms[key]; !ok {
sampler := metrics.NewUniformSample(2048)
g.histograms[key] = metrics.NewHistogram(sampler)
}
return g.histograms[key]
}

func (g groupedMetrics) getOrCreateCounter(key string) metrics.Counter {
g.lock.Lock()
defer g.lock.Unlock()

if _, ok := g.counters[key]; !ok {
g.counters[key] = metrics.NewCounter()
}
return g.counters[key]
}

type centralMetric struct {
Subscription *models.ResourceReference `json:"subscription,omitempty"`
App *models.ApplicationResourceReference `json:"app,omitempty"`
Expand Down
31 changes: 14 additions & 17 deletions pkg/transaction/metric/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ func (c *collector) AddMetricDetail(metricDetail Detail) {
c.AddMetric(metricDetail.APIDetails, metricDetail.StatusCode, metricDetail.Duration, metricDetail.Bytes, metricDetail.APIDetails.Name)
c.createOrUpdateHistogram(metricDetail)
// TODO remove this after testing
c.AddCustomMetricDetail(CustomMetricDetail{
APIDetails: metricDetail.APIDetails,
AppDetails: metricDetail.AppDetails,
UnitDetails: models.Unit{
Name: "x-custom-token",
},
Count: 30,
})
// c.AddCustomMetricDetail(CustomMetricDetail{
// APIDetails: metricDetail.APIDetails,
// AppDetails: metricDetail.AppDetails,
// UnitDetails: models.Unit{
// Name: "x-custom-token",
// },
// Count: 30,
// })
}

// AddAPIMetricDetail - add metric details for several response codes and transactions
Expand Down Expand Up @@ -309,6 +309,10 @@ func (c *collector) AddCustomMetricDetail(detail CustomMetricDetail) {
if !c.metricConfig.CanPublish() || c.usageConfig.IsOfflineMode() {
return
}
c.lock.Lock()
defer c.lock.Unlock()
c.batchLock.Lock()
defer c.batchLock.Unlock()

logger := c.logger.WithField("handler", "customMetric").
WithField("apiID", detail.APIDetails.ID).
Expand Down Expand Up @@ -948,21 +952,14 @@ func (c *collector) getOrRegisterGroupedCounter(name string) metrics.Counter {
groupKey, countKey := splitMetricKey(name)
groupedMetric := c.getOrRegisterGroupedMetrics(groupKey)

if _, ok := groupedMetric.counters[countKey]; !ok {
groupedMetric.counters[countKey] = metrics.NewCounter()
}
return groupedMetric.counters[countKey]
return groupedMetric.getOrCreateCounter(countKey)
}

func (c *collector) getOrRegisterGroupedHistogram(name string) metrics.Histogram {
groupKey, histoKey := splitMetricKey(name)
groupedMetric := c.getOrRegisterGroupedMetrics(groupKey)

if _, ok := groupedMetric.histograms[histoKey]; !ok {
sampler := metrics.NewUniformSample(2048)
groupedMetric.histograms[histoKey] = metrics.NewHistogram(sampler)
}
return groupedMetric.histograms[histoKey]
return groupedMetric.getOrCreateHistogram(histoKey)
}

func (c *collector) publishEvents() {
Expand Down

0 comments on commit 6aa83aa

Please sign in to comment.