github.com/oam-dev/kubevela@v1.9.11/pkg/monitor/README.md (about) 1 # Package Usage 2 3 ## Context 4 First, this context is compatible with built-in context interface. 5 Also, it supports fork and commit like trace span. 6 7 ### Fork 8 `Fork` will generate a sub context that inherit the parent's tags. When new tags are added to the `sub-context`, the `parent-context` will not be affected. 9 10 ### Commit 11 `Commit` will log the context duration, and export metrics or other execution information. 12 13 ### usage 14 ``` 15 tracerCtx:=context.NewTraceContext(stdCtx,"$id") 16 defer tracerCtx.Commit("success") 17 18 // Execute sub-code logic 19 subCtx:=tracerCtx.Fork("sub-id") 20 ... 21 subCtx.Commit("step is executed") 22 23 ``` 24 25 ## Metrics 26 First, you need register `metricVec` in package `pkg/monitor/metrics`, like below: 27 ``` 28 StepDurationSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{ 29 Name: "step_duration_ms", 30 Help: "step latency distributions.", 31 Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, 32 ConstLabels: prometheus.Labels{}, 33 }, []string{"application", "workflow_revision", "step_name", "step_type"}) 34 ``` 35 36 Now, you can export metrics by context,for example 37 ``` 38 subCtx:=tracerCtx.Fork("sub-id",DurationMetric(func(v float64) { 39 metrics.StepDurationSummary.WithLabelValues(e.app.Name, e.status.AppRevision, stepStatus.Name, stepStatus.Type).Observe(v) 40 }) 41 subCtx.Commit("export") // At this time, it will export the StepDurationSummary metrics. 42 43 ``` 44 45 Context only support `DurationMetric` exporter. you can submit pr to support more exporters. 46 If metrics have nothing to do with context, there is no need to extend it through context exporter