Skip to content

Commit

Permalink
use pkg/metrics as standard API
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed Sep 14, 2018
1 parent c083c41 commit 345ad39
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 137 deletions.
3 changes: 1 addition & 2 deletions adminapi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ import (
chassisTLS "github.com/go-chassis/go-chassis/core/tls"
"github.com/go-mesh/mesher/common"
"github.com/go-mesh/mesher/config"
"github.com/go-mesh/mesher/pkg/metrics"
"github.com/go-mesh/openlogging"
)

//Init function initiates admin server config and runs it
func Init() (err error) {
isAdminEnable := config.GetConfig().Admin.Enable
metrics.Init()

if !isAdminEnable {
lager.Logger.Infof("admin api are not enable")
return nil
Expand Down
2 changes: 2 additions & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
chassisHandler "github.com/go-chassis/go-chassis/core/handler"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/core/metadata"
"github.com/go-mesh/mesher/pkg/metrics"
)

// Start initialize configs and components
Expand All @@ -49,6 +50,7 @@ func Start() error {
if err := DecideMode(); err != nil {
return err
}
metrics.Init()
if err := adminapi.Init(); err != nil {
log.Println("Error occurred in starting admin server", err)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ replace (
require (
github.com/emicklei/go-restful-swagger12 v0.0.0-20170926063155-7524189396c6 // indirect
github.com/go-chassis/go-cc-client v0.0.0-20180831085349-c2bb6cef1640
github.com/go-chassis/go-chassis v0.8.3-0.20180904011159-fcc43e7cb6ea
github.com/go-chassis/go-chassis v0.8.3-0.20180914033538-0791a5cec8b4
github.com/go-chassis/gohessian v0.0.0-20180702061429-e5130c25af55
github.com/go-mesh/openlogging v0.0.0-20180831021158-f5d1c4e7e506
github.com/gogo/protobuf v1.1.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v0.8.0
Expand Down
34 changes: 17 additions & 17 deletions integration/mesher-grafana-dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
"tableColumn": "",
"targets": [
{
"expr": "count(count(requests_total{instance=~\"$node\"}) by(servicename))",
"expr": "count(count(requests_total{instance=~\"$node\"}) by(service_name))",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
Expand Down Expand Up @@ -734,10 +734,10 @@
"steppedLine": false,
"targets": [
{
"expr": "(sum(successes_total{instance=~\"$node\"}) by (servicename,appid,version))/(sum(requests_total{instance=~\"$node\"}) by (servicename,appid,version))",
"expr": "(sum(successes_total{instance=~\"$node\"}) by (service_name,app,version))/(sum(requests_total{instance=~\"$node\"}) by (service_name,app,version))",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "{{servicename}}@{{appid}}#{{version}}",
"legendFormat": "{{service_name}}@{{app}}#{{version}}",
"refId": "A",
"step": 10
}
Expand Down Expand Up @@ -810,10 +810,10 @@
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(requests_total{instance=~\"$node\"} [5m])) by(servicename,appid,version)",
"expr": "sum(rate(requests_total{instance=~\"$node\"} [5m])) by(service_name,app,version)",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "{{servicename}}@{{appid}}#{{version}}",
"legendFormat": "{{service_name}}@{{app}}#{{version}}",
"refId": "A",
"step": 10
}
Expand Down Expand Up @@ -934,7 +934,7 @@
"tableColumn": "",
"targets": [
{
"expr": "requests_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"}",
"expr": "requests_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"}",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
Expand Down Expand Up @@ -1011,7 +1011,7 @@
"tableColumn": "",
"targets": [
{
"expr": "sum(successes_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"})/sum(requests_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"}) ",
"expr": "sum(successes_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"})/sum(requests_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"}) ",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
Expand Down Expand Up @@ -1088,7 +1088,7 @@
"tableColumn": "",
"targets": [
{
"expr": "rate(status_5xx{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"} [5m])",
"expr": "rate(status_5xx{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"} [5m])",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
Expand Down Expand Up @@ -1165,7 +1165,7 @@
"tableColumn": "",
"targets": [
{
"expr": "rate(status_4xx{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"} [5m])",
"expr": "rate(status_4xx{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"} [5m])",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
Expand Down Expand Up @@ -1217,7 +1217,7 @@
"steppedLine": false,
"targets": [
{
"expr": "sum(successes_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"})/sum(requests_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"}) ",
"expr": "sum(successes_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"})/sum(requests_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"}) ",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "Quantile : {{quantile}}",
Expand Down Expand Up @@ -1293,10 +1293,10 @@
"steppedLine": false,
"targets": [
{
"expr": "rate(requests_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"} [5m])",
"expr": "rate(requests_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"} [5m])",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "{{servicename}}",
"legendFormat": "{{service_name}}",
"refId": "A",
"step": 10
}
Expand Down Expand Up @@ -1369,7 +1369,7 @@
"steppedLine": false,
"targets": [
{
"expr": "request_latency_seconds{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\",version=~\"$version\"} ",
"expr": "request_latency_seconds{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\",version=~\"$version\"} ",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "Quantile : {{quantile}}",
Expand Down Expand Up @@ -1945,7 +1945,7 @@
"multi": false,
"name": "service",
"options": [],
"query": "label_values(requests_total{instance=~\"$node\"},servicename)",
"query": "label_values(requests_total{instance=~\"$node\"},service_name)",
"refresh": 2,
"regex": "",
"sort": 1,
Expand All @@ -1967,9 +1967,9 @@
"includeAll": false,
"label": "",
"multi": false,
"name": "appid",
"name": "app",
"options": [],
"query": "label_values(requests_total{instance=~\"$node\",servicename=~\"$service\"},appid)",
"query": "label_values(requests_total{instance=~\"$node\",service_name=~\"$service\"},app)",
"refresh": 2,
"regex": "",
"sort": 1,
Expand All @@ -1992,7 +1992,7 @@
"multi": false,
"name": "version",
"options": [],
"query": "label_values(requests_total{instance=~\"$node\",servicename=~\"$service\",appid=~\"$appid\"},version)",
"query": "label_values(requests_total{instance=~\"$node\",service_name=~\"$service\",app=~\"$app\"},version)",
"refresh": 2,
"regex": "",
"sort": 1,
Expand Down
51 changes: 30 additions & 21 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
package metrics

import (
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/pkg/runtime"
mesherConf "github.com/go-mesh/mesher/config"
"sync"
Expand All @@ -40,8 +38,8 @@ const (
LRequestLatencySeconds = "request_latency_seconds"
LError4XX = "status_4xx"
LError5XX = "status_5xx"
LServiceName = "servicename"
LApp = "appid"
LServiceName = "service_name"
LApp = "app"
LVersion = "version"
LStartTime = "start_time_seconds"
)
Expand All @@ -54,39 +52,50 @@ var (

//Recorder export the metrics into various of system
type Recorder interface {
RecordStatus(labels []string, labelValues map[string]string, status int)
RecordLatency(labels []string, labelValues map[string]string, latency float64)
RecordStartTime(labels []string, labelValues map[string]string, start time.Time)
RecordStatus(labelValues map[string]string, status int, opts *RecordOptions)
RecordLatency(labelValues map[string]string, latency float64, opts *RecordOptions)
RecordStartTime(labelValues map[string]string, start time.Time, opts *RecordOptions)
}

//Options define recorder options
type Options struct {
LabelNames []string //default label names, if RecordOptions LabelNames is nil
EnableGoRuntimeMetrics bool
}

//RecordOptions is options for record method
type RecordOptions struct {
LabelNames []string // able top custom label names
}

var defaultRecorder Recorder = &promRecorder{}

//RecordStatus record a status
func RecordStatus(inv *invocation.Invocation, statusCode int) {
serviceLabelValues := map[string]string{LServiceName: inv.MicroServiceName, LApp: inv.RouteTags.AppID(), LVersion: inv.RouteTags.Version()}
defaultRecorder.RecordStatus(LabelNames, serviceLabelValues, statusCode)
//RecordStatus record an operation status
func RecordStatus(labelValues map[string]string, statusCode int, opts *RecordOptions) {
defaultRecorder.RecordStatus(labelValues, statusCode, opts)
}

//RecordLatency TODO
func RecordLatency(labelNames []string, labelValues map[string]string, latency float64) {

//RecordLatency record an operation latency
func RecordLatency(labelValues map[string]string, latency float64, opts *RecordOptions) {
defaultRecorder.RecordLatency(labelValues, latency, opts)
}

//RecordStartTime record the app start time
func RecordStartTime(labelNames []string, labelValues map[string]string, start time.Time) {
defaultRecorder.RecordStartTime(labelNames, labelValues, start)
//RecordStartTime record app start time
func RecordStartTime(labelValues map[string]string, start time.Time, opts *RecordOptions) {
defaultRecorder.RecordStartTime(labelValues, start, opts)
}

//Init initiate the recorder
func Init() {
LabelValues := map[string]string{LServiceName: runtime.ServiceName, LApp: config.GlobalDefinition.AppID, LVersion: config.SelfVersion}
defaultRecorder = newPromRecorder(&Options{
func Init() error {
var err error
LabelValues := map[string]string{LServiceName: runtime.ServiceName, LApp: runtime.App, LVersion: runtime.Version}
defaultRecorder, err = NewPromRecorder(&Options{
LabelNames: LabelNames,
EnableGoRuntimeMetrics: mesherConf.GetConfig().Admin.GoRuntimeMetrics,
})
RecordStartTime(LabelNames, LabelValues, time.Now())
if err != nil {
return err
}
RecordStartTime(LabelValues, time.Now(), nil)
return nil
}
98 changes: 26 additions & 72 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,100 +15,54 @@
* limitations under the License.
*/

package metrics
package metrics_test

import (
"net/http"
"strings"
"testing"

"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"

"github.com/go-chassis/go-chassis/core/config/model"
"github.com/go-chassis/go-chassis/pkg/runtime"
mc "github.com/go-mesh/mesher/config"
"github.com/go-mesh/mesher/pkg/metrics"
)

var (
labelNames = []string{"APPID", "VERSION"}
labelValues = map[string]string{"APPID": "sockshop", "VERSION": "0.1"}
)

func TestPrometheusConfig_CounterFromNameAndLabelValues(t *testing.T) {
assert := assert.New(t)
var totalMetricCreated int
DefaultPrometheusExporter.Count("total_request", labelNames, labelValues)
metricFamilies, err := prometheus.DefaultGatherer.Gather()
assert.Nil(err, "error should be nil while collecting metrics from prometheus")
for _, metricFamily := range metricFamilies {
if metricName := metricFamily.GetName(); strings.Contains(metricName, "total_request") {
assert.Equal(metricFamily.GetType(), dto.MetricType_COUNTER)
totalMetricCreated++
}
}
assert.Equal(totalMetricCreated, 1)
func TestInit(t *testing.T) {
mc.SetConfig(&mc.MesherConfig{
Admin: mc.Admin{
GoRuntimeMetrics: false,
},
})
err := metrics.Init()
runtime.ServiceName = "A"
runtime.Version = "v1.1"
runtime.App = "app"
assert.NoError(t, err)
}

func TestPrometheusConfig_GaugeFromNameAndLabelValues(t *testing.T) {
func TestRecordStatus(t *testing.T) {
assert := assert.New(t)
var totalMetricCreated int
var gaugeValue *float64
DefaultPrometheusExporter.Gauge("memory_used", 12, labelNames, labelValues)
metricFamilies, err := prometheus.DefaultGatherer.Gather()
assert.Nil(err, "error should be nil while collecting metrics from prometheus")
for _, metricFamily := range metricFamilies {
if metricName := metricFamily.GetName(); strings.Contains(metricName, "memory_used") {
assert.Equal(metricFamily.GetType(), dto.MetricType_GAUGE)
totalMetricCreated++
gaugeValue = metricFamily.Metric[0].Gauge.Value
}
}
assert.Equal(totalMetricCreated, 1)
assert.Equal(*gaugeValue, float64(12))
}

func TestPrometheusConfig_SummaryFromNameAndLabelValues(t *testing.T) {
assert := assert.New(t)
var totalMetricCreated int
var sampleCount *uint64
DefaultPrometheusExporter.Summary("request_latency", 12, labelNames, labelValues)
metricFamilies, err := prometheus.DefaultGatherer.Gather()
assert.Nil(err, "error should be nil while collecting metrics from prometheus")
for _, metricFamily := range metricFamilies {
if metricName := metricFamily.GetName(); strings.Contains(metricName, "request_latency") {
assert.Equal(metricFamily.GetType(), dto.MetricType_SUMMARY)
totalMetricCreated++
sampleCount = metricFamily.Metric[0].Summary.SampleCount
}
}
assert.Equal(totalMetricCreated, 1)
assert.Equal(*sampleCount, uint64(1))
}

func TestPrepare(t *testing.T) {
assert := assert.New(t)
config.GlobalDefinition = new(model.GlobalCfg)
config.GlobalDefinition.AppID = "sockshop"
config.SelfVersion = "0.1"
var inv = &invocation.Invocation{
MicroServiceName: "service",
}
var errorcount4xx float64
var errorcount5xx float64
RecordStatus(inv, http.StatusOK)
RecordStatus(inv, http.StatusNotFound)
RecordStatus(inv, http.StatusInternalServerError)
lvs := map[string]string{
metrics.LServiceName: "service",
metrics.LVersion: "",
metrics.LApp: "",
}
metrics.RecordStatus(lvs, http.StatusOK, nil)
metrics.RecordStatus(lvs, http.StatusNotFound, nil)
metrics.RecordStatus(lvs, http.StatusInternalServerError, nil)
metricFamilies, err := prometheus.DefaultGatherer.Gather()
assert.Nil(err, "error should be nil while collecting metrics from prometheus")
for _, metricFamily := range metricFamilies {
if name := metricFamily.GetName(); strings.Contains(name, LError5XX) {
if name := metricFamily.GetName(); strings.Contains(name, metrics.LError5XX) {
errorcount4xx += *metricFamily.Metric[0].Counter.Value
}
}
for _, metricFamily := range metricFamilies {
if name := metricFamily.GetName(); strings.Contains(name, LError5XX) {
if name := metricFamily.GetName(); strings.Contains(name, metrics.LError5XX) {
errorcount5xx += *metricFamily.Metric[0].Counter.Value
}
}
Expand Down
Loading

0 comments on commit 345ad39

Please sign in to comment.