Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revise metrics code #3430

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/server/rule_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/def"
"github.com/lf-edge/ekuiper/v2/internal/pkg/schedule"
"github.com/lf-edge/ekuiper/v2/internal/server/promMetrics"
"github.com/lf-edge/ekuiper/v2/internal/topo/rule"
"github.com/lf-edge/ekuiper/v2/metrics"
"github.com/lf-edge/ekuiper/v2/pkg/ast"
"github.com/lf-edge/ekuiper/v2/pkg/timex"
)
Expand Down Expand Up @@ -150,10 +150,10 @@
stopCount++
v = RuleStopped
}
promMetrics.SetRuleStatus(id, int(v))
metrics.SetRuleStatus(id, int(v))
}
promMetrics.SetRuleStatusCountGauge(true, runningCount)
promMetrics.SetRuleStatusCountGauge(false, stopCount)
metrics.SetRuleStatusCountGauge(true, runningCount)
metrics.SetRuleStatusCountGauge(false, stopCount)
}
}

Expand Down Expand Up @@ -272,7 +272,7 @@
continue
}
for labelValue, t := range ruleUsage.Stats {
promMetrics.SetRuleCPUUsageGauge(labelValue, t)
metrics.SetRuleCPUUsageGauge(labelValue, t)

Check warning on line 275 in internal/server/rule_init.go

View check run for this annotation

Codecov / codecov/patch

internal/server/rule_init.go#L275

Added line #L275 was not covered by tests
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/def"
"github.com/lf-edge/ekuiper/v2/internal/pkg/store"
"github.com/lf-edge/ekuiper/v2/internal/server/promMetrics"
"github.com/lf-edge/ekuiper/v2/internal/topo/planner"
"github.com/lf-edge/ekuiper/v2/internal/topo/rule"
"github.com/lf-edge/ekuiper/v2/internal/xsql"
"github.com/lf-edge/ekuiper/v2/metrics"
"github.com/lf-edge/ekuiper/v2/pkg/errorx"
"github.com/lf-edge/ekuiper/v2/pkg/infra"
"github.com/lf-edge/ekuiper/v2/pkg/replace"
Expand Down Expand Up @@ -475,6 +475,6 @@ func getRuleState(name string) (rule.RunState, error) {

func deleteRuleMetrics(name string) {
if conf.Config != nil && conf.Config.Basic.Prometheus {
promMetrics.RemoveRuleStatus(name)
metrics.RemoveRuleStatus(name)
}
}
5 changes: 0 additions & 5 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"github.com/lf-edge/ekuiper/v2/internal/plugin/portable/runtime"
"github.com/lf-edge/ekuiper/v2/internal/processor"
"github.com/lf-edge/ekuiper/v2/internal/server/bump"
"github.com/lf-edge/ekuiper/v2/internal/server/promMetrics"
"github.com/lf-edge/ekuiper/v2/internal/topo/rule"
"github.com/lf-edge/ekuiper/v2/pkg/cast"
"github.com/lf-edge/ekuiper/v2/pkg/connection"
Expand Down Expand Up @@ -140,10 +139,6 @@ func StartUp(Version string) {
createPaths()
conf.SetupEnv()
conf.InitConf()
if conf.Config.Basic.Prometheus {
promMetrics.RegisterMetrics()
}

// Print inited modules
for n := range modules.Sources {
conf.Log.Infof("register source %s", n)
Expand Down
29 changes: 29 additions & 0 deletions internal/topo/node/cache/sync_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/store"
"github.com/lf-edge/ekuiper/v2/metrics"
"github.com/lf-edge/ekuiper/v2/pkg/kv"
)

Expand Down Expand Up @@ -94,7 +95,17 @@ func (p *page) reset() {
p.L = 0
}

const (
syncCacheAdd = "add"
syncCachePop = "pop"
syncCacheFlush = "flush"
syncCacheDrop = "drop"
syncCacheLoad = "load"
)

type SyncCache struct {
RuleID string
OpID string
// cache config
cacheConf *conf.SinkConf
maxDiskPage int
Expand Down Expand Up @@ -129,6 +140,11 @@ func NewSyncCache(ctx api.StreamContext, cacheConf *conf.SinkConf) (*SyncCache,
return c, err
}

func (c *SyncCache) SetupMeta(ctx api.StreamContext) {
c.RuleID = ctx.GetRuleId()
c.OpID = ctx.GetOpId()
}

// AddCache not thread safe!
func (c *SyncCache) AddCache(ctx api.StreamContext, item any) error {
isBufferNotFull := c.writeBufferPage.append(item)
Expand All @@ -142,12 +158,18 @@ func (c *SyncCache) AddCache(ctx api.StreamContext, item any) error {
} else {
ctx.GetLogger().Debugf("added cache to disk buffer page %v", c.writeBufferPage)
}
metrics.SyncCacheCounter.WithLabelValues(syncCacheAdd, c.RuleID, c.OpID).Inc()
c.CacheLength++
ctx.GetLogger().Debugf("added cache %d", c.CacheLength)
return nil
}

func (c *SyncCache) appendWriteCache(ctx api.StreamContext) error {
metrics.SyncCacheCounter.WithLabelValues(syncCacheFlush, c.RuleID, c.OpID).Inc()
start := time.Now()
defer func() {
metrics.SyncCacheHist.WithLabelValues(syncCacheFlush, c.RuleID, c.OpID).Observe(float64(time.Since(start).Microseconds()))
}()
if c.diskSize == c.maxDiskPage {
// disk full, replace read buffer page
err := c.deleteDiskPage(ctx, false)
Expand Down Expand Up @@ -229,11 +251,13 @@ func (c *SyncCache) PopCache(ctx api.StreamContext) (any, bool) {
ctx.GetLogger().Debugf("deleted cache: %d", c.CacheLength)
}
ctx.GetLogger().Debugf("deleted cache. CacheLength: %d, diskSize: %d, readPage: %v", c.CacheLength, c.diskSize, c.readBufferPage)
metrics.SyncCacheCounter.WithLabelValues(syncCachePop, c.RuleID, c.OpID).Inc()
return result, true
}

// loaded means whether load the page to memory or just drop
func (c *SyncCache) deleteDiskPage(ctx api.StreamContext, loaded bool) error {
metrics.SyncCacheCounter.WithLabelValues(syncCacheDrop, c.RuleID, c.OpID).Inc()
_ = c.store.Delete(strconv.Itoa(c.diskPageHead))
ctx.GetLogger().Warnf("drop a read page of %d items in memory", c.readBufferPage.L)
c.diskPageHead++
Expand All @@ -258,6 +282,11 @@ func (c *SyncCache) deleteDiskPage(ctx api.StreamContext, loaded bool) error {
}

func (c *SyncCache) loadFromDisk(ctx api.StreamContext) error {
metrics.SyncCacheCounter.WithLabelValues(syncCacheLoad, c.RuleID, c.OpID).Inc()
start := time.Now()
defer func() {
metrics.SyncCacheHist.WithLabelValues(syncCacheLoad, c.RuleID, c.OpID).Observe(float64(time.Since(start).Microseconds()))
}()
// load page from the disk
ctx.GetLogger().Debugf("loading from disk %d. CacheLength: %d, diskSize: %d", c.diskPageTail, c.CacheLength, c.diskSize)
// caution, must create a new page instance
Expand Down
1 change: 1 addition & 0 deletions internal/topo/node/cache_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (s *CacheOp) Exec(ctx api.StreamContext, errCh chan<- error) {
if len(s.outputs) > 1 {
infra.DrainError(ctx, fmt.Errorf("cache op should have only 1 output but got %+v", s.outputs), errCh)
}
s.cache.SetupMeta(ctx)
s.prepareExec(ctx, errCh, "op")
go func() {
err := infra.SafeRun(func() error {
Expand Down
18 changes: 7 additions & 11 deletions internal/server/promMetrics/prom_metrcis.go → metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package promMetrics
package metrics

import "github.com/prometheus/client_golang/prometheus"

const (
LblType = "type"
LblStatusType = "status"
LblRuleIDType = "ruleID"
LblRuleIDType = "rule"
LblOpIDType = "op"

LBlRuleRunning = "running"
LblRuleStop = "stop"
)

var (
RuleStatusCountGauge *prometheus.GaugeVec
RuleStatusGauge *prometheus.GaugeVec
RuleCPUUsageGauge *prometheus.GaugeVec
)

func InitServerMetrics() {
RuleStatusCountGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "kuiper",
Subsystem: "rule",
Expand All @@ -51,10 +47,10 @@ func InitServerMetrics() {
Name: "cpu_ms",
Help: "gauge of rule CPU usage",
}, []string{LblRuleIDType})
}
)

func RegisterMetrics() {
InitServerMetrics()
func init() {
RegisterSyncCache()
prometheus.MustRegister(RuleStatusCountGauge)
prometheus.MustRegister(RuleStatusGauge)
prometheus.MustRegister(RuleCPUUsageGauge)
Expand Down
Loading
Loading