Compare commits
5 Commits
Author | Date | ||
---|---|---|---|
![]() |
98eac47127 | ||
![]() |
69b0fb05aa | ||
![]() |
ba9357e166 | ||
![]() |
9df516247c | ||
![]() |
93bdc425e5 |
6
api.go
6
api.go
@ -54,6 +54,12 @@ func (e *Engine) POST(uri string, handler Handler) {
|
|||||||
e.Engine.POST(uri, h)
|
e.Engine.POST(uri, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Engine) GET(uri string, handler Handler) {
|
||||||
|
h := handlerWrapper(handler)
|
||||||
|
log.Infof("api handler [%v] registor success", handler)
|
||||||
|
e.Engine.GET(uri, h)
|
||||||
|
}
|
||||||
|
|
||||||
// handlerWrapper
|
// handlerWrapper
|
||||||
func handlerWrapper(handler Handler) gin.HandlerFunc {
|
func handlerWrapper(handler Handler) gin.HandlerFunc {
|
||||||
handlerFunc := handler.HandlerFunc()
|
handlerFunc := handler.HandlerFunc()
|
||||||
|
@ -120,7 +120,14 @@ func (c *Context) SetCookie(cookie *http.Cookie) {
|
|||||||
http.SetCookie(c.Writer, cookie)
|
http.SetCookie(c.Writer, cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) AlreadyHaveResult() (done bool, entry string) {
|
||||||
|
return c.resultentry != "", c.resultentry
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) RESULT(output interface{}) error {
|
func (c *Context) RESULT(output interface{}) error {
|
||||||
|
if c.resultentry == "" {
|
||||||
|
c.resultentry = "RESULT"
|
||||||
|
}
|
||||||
var t = reflect.TypeOf(output)
|
var t = reflect.TypeOf(output)
|
||||||
defer func(o *interface{}) {
|
defer func(o *interface{}) {
|
||||||
b, err := json.Marshal(o)
|
b, err := json.Marshal(o)
|
||||||
@ -149,7 +156,6 @@ func (c *Context) RESULT(output interface{}) error {
|
|||||||
cost := time.Now().Sub(c.starttime).Milliseconds()
|
cost := time.Now().Sub(c.starttime).Milliseconds()
|
||||||
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
|
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
|
||||||
log.NoticefWithDepth(calldepth, noticeStr)
|
log.NoticefWithDepth(calldepth, noticeStr)
|
||||||
|
|
||||||
}(&output)
|
}(&output)
|
||||||
|
|
||||||
if _, ok := t.FieldByName("ErrCode"); !ok {
|
if _, ok := t.FieldByName("ErrCode"); !ok {
|
||||||
|
@ -15,9 +15,10 @@ import (
|
|||||||
var (
|
var (
|
||||||
MetricExporterPusher = func() error { return nil }
|
MetricExporterPusher = func() error { return nil }
|
||||||
MetricExporterHandler Handler = nil
|
MetricExporterHandler Handler = nil
|
||||||
metricRegistry *prometheus.Registry = nil
|
MetricRegistry *prometheus.Registry = nil
|
||||||
metricApiCounter *prometheus.CounterVec = nil
|
metricApiCounter *prometheus.CounterVec = nil
|
||||||
metricApiSummary *prometheus.SummaryVec = nil
|
metricApiSummary *prometheus.SummaryVec = nil
|
||||||
|
instance string = "--unknown--"
|
||||||
)
|
)
|
||||||
|
|
||||||
type metricExporterHandler struct {
|
type metricExporterHandler struct {
|
||||||
@ -41,7 +42,6 @@ func dumpMetricExporterPusher() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitMetrics(instanceValue string) {
|
func InitMetrics(instanceValue string) {
|
||||||
var instance = instanceValue
|
|
||||||
if instanceValue == "" {
|
if instanceValue == "" {
|
||||||
instance = getInstanceIpAddress()
|
instance = getInstanceIpAddress()
|
||||||
} else if ap := strings.Split(instanceValue, ":"); len(ap) == 2 &&
|
} else if ap := strings.Split(instanceValue, ":"); len(ap) == 2 &&
|
||||||
@ -52,25 +52,25 @@ func InitMetrics(instanceValue string) {
|
|||||||
|
|
||||||
metricApiCounter = prometheus.NewCounterVec(
|
metricApiCounter = prometheus.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "api_requests_total",
|
Name: "api_requests_total",
|
||||||
Help: "How many HTTP requests processed",
|
Help: "How many HTTP requests processed",
|
||||||
ConstLabels: prometheus.Labels{"instance": instance},
|
//ConstLabels: prometheus.Labels{"instance": instance},
|
||||||
},
|
},
|
||||||
[]string{"api", "errcode", "appid"},
|
[]string{"api", "errcode", "appid"},
|
||||||
)
|
)
|
||||||
metricApiSummary = prometheus.NewSummaryVec(
|
metricApiSummary = prometheus.NewSummaryVec(
|
||||||
prometheus.SummaryOpts{
|
prometheus.SummaryOpts{
|
||||||
Name: "api_requests_summary",
|
Name: "api_requests_summary",
|
||||||
Help: "The api request summary of cost.",
|
Help: "The api request summary of cost.",
|
||||||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
||||||
ConstLabels: prometheus.Labels{"instance": instance},
|
//ConstLabels: prometheus.Labels{"instance": instance},
|
||||||
},
|
},
|
||||||
[]string{"api", "errcode", "appid"},
|
[]string{"api", "errcode", "appid"},
|
||||||
)
|
)
|
||||||
|
|
||||||
metricRegistry = prometheus.NewRegistry()
|
MetricRegistry = prometheus.NewRegistry()
|
||||||
metricRegistry.MustRegister(metricApiCounter)
|
MetricRegistry.MustRegister(metricApiCounter)
|
||||||
metricRegistry.MustRegister(metricApiSummary)
|
MetricRegistry.MustRegister(metricApiSummary)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupMetricsExporterHandler(apiname string) {
|
func SetupMetricsExporterHandler(apiname string) {
|
||||||
@ -78,10 +78,10 @@ func SetupMetricsExporterHandler(apiname string) {
|
|||||||
apiname = "premetheus_metrics_exporter"
|
apiname = "premetheus_metrics_exporter"
|
||||||
}
|
}
|
||||||
|
|
||||||
opt := promhttp.HandlerOpts{Registry: metricRegistry}
|
opt := promhttp.HandlerOpts{Registry: MetricRegistry}
|
||||||
MetricExporterHandler = &metricExporterHandler{
|
MetricExporterHandler = &metricExporterHandler{
|
||||||
name: apiname,
|
name: apiname,
|
||||||
promhttpHandler: promhttp.HandlerFor(metricRegistry, opt),
|
promhttpHandler: promhttp.HandlerFor(MetricRegistry, opt),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,11 @@ func SetupMetricsExporterPusher(pushgateway string, jobname string) {
|
|||||||
jobname = fmt.Sprintf("api-metrics-job-%s", getExeFilename())
|
jobname = fmt.Sprintf("api-metrics-job-%s", getExeFilename())
|
||||||
}
|
}
|
||||||
|
|
||||||
var pusher = push.New(pushgateway, jobname).Gatherer(metricRegistry)
|
var pusher = push.
|
||||||
|
New(pushgateway, jobname).
|
||||||
|
Grouping("instance", instance).
|
||||||
|
Gatherer(MetricRegistry)
|
||||||
|
|
||||||
MetricExporterPusher = func() error {
|
MetricExporterPusher = func() error {
|
||||||
RecordMetrics("push_metrics_to_prometheus", "0", "selfmonitoring", 1.0)
|
RecordMetrics("push_metrics_to_prometheus", "0", "selfmonitoring", 1.0)
|
||||||
return pusher.Push()
|
return pusher.Push()
|
||||||
|
Loading…
Reference in New Issue
Block a user