Skip to content

Commit a024d94

Browse files
authored
fix(monitor): add service metric monitor (#22226)
1 parent 7cb030c commit a024d94

File tree

7 files changed

+244
-12
lines changed

7 files changed

+244
-12
lines changed

pkg/apis/monitor/metric.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const (
3232
METRIC_RES_TYPE_ELB = "elb"
3333
METRIC_RES_TYPE_K8S = "k8s"
3434
METRIC_RES_TYPE_CONTAINER = "container"
35+
METRIC_RES_TYPE_WORKER = "worker"
36+
METRIC_RES_TYPE_HTTP_REQUEST = "http_request"
37+
METRIC_RES_TYPE_DB_STATS = "db_stats"
38+
METRIC_RES_TYPE_PROCESS = "process"
3539

3640
//ext is prefix!
3741
METRIC_RES_TYPE_JENKINS = "ext_jenkins"
@@ -52,8 +56,9 @@ const (
5256
METRIC_UNIT_MB = "Mb"
5357
METRIC_UNIT_NULL = "NULL"
5458

55-
METRIC_DATABASE_TELE = "telegraf"
56-
METRIC_DATABASE_METER = "meter_db"
59+
METRIC_DATABASE_TELE = "telegraf"
60+
METRIC_DATABASE_METER = "meter_db"
61+
METRIC_DATABASE_SYSTEM = "system"
5762
)
5863

5964
var (

pkg/cloudmon/misc/system.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ import (
2626
"yunion.io/x/pkg/errors"
2727
"yunion.io/x/pkg/gotypes"
2828
"yunion.io/x/pkg/util/httputils"
29+
"yunion.io/x/pkg/util/version"
2930
"yunion.io/x/pkg/utils"
3031

3132
"yunion.io/x/onecloud/pkg/apis"
33+
compute_api "yunion.io/x/onecloud/pkg/apis/compute"
3234
api "yunion.io/x/onecloud/pkg/apis/identity"
3335
"yunion.io/x/onecloud/pkg/cloudcommon/tsdb"
3436
"yunion.io/x/onecloud/pkg/cloudmon/options"
3537
"yunion.io/x/onecloud/pkg/mcclient"
3638
"yunion.io/x/onecloud/pkg/mcclient/auth"
39+
"yunion.io/x/onecloud/pkg/mcclient/modules/compute"
3740
"yunion.io/x/onecloud/pkg/mcclient/modules/identity"
3841
"yunion.io/x/onecloud/pkg/util/influxdb"
3942
)
@@ -67,7 +70,13 @@ func CollectServiceMetrics(ctx context.Context, userCred mcclient.TokenCredentia
6770
return
6871
}
6972
s := auth.GetAdminSession(ctx, options.Options.CommonOptions.Region)
70-
err := func() error {
73+
urls, err := tsdb.GetDefaultServiceSourceURLs(s, options.Options.SessionEndpointType)
74+
if err != nil {
75+
return
76+
}
77+
78+
tk := auth.AdminCredential().GetTokenString()
79+
err = func() error {
7180
endpoints, err := getEndpoints(ctx, s)
7281
if err != nil {
7382
return errors.Wrapf(err, "getEndpoints")
@@ -83,7 +92,6 @@ func CollectServiceMetrics(ctx context.Context, userCred mcclient.TokenCredentia
8392
continue
8493
}
8594
url := httputils.JoinPath(ep.Url, "version")
86-
tk := auth.AdminCredential().GetTokenString()
8795
hdr := http.Header{}
8896
hdr.Set("X-Auth-Token", tk)
8997
resp, err := httputils.Request(
@@ -106,15 +114,48 @@ func CollectServiceMetrics(ctx context.Context, userCred mcclient.TokenCredentia
106114
}
107115
metrics = append(metrics, part...)
108116
}
109-
urls, err := tsdb.GetDefaultServiceSourceURLs(s, options.Options.SessionEndpointType)
110-
if err != nil {
111-
return errors.Wrap(err, "GetServiceURLs")
112-
}
113117
return influxdb.SendMetrics(urls, SYSTEM_METRIC_DATABASE, metrics, false)
114118
}()
115119
if err != nil {
116120
log.Errorf("collect service metric error: %v", err)
117121
}
122+
params := compute_api.HostListInput{}
123+
limit := 20
124+
params.Limit = &limit
125+
params.Brand = []string{compute_api.CLOUD_PROVIDER_ONECLOUD}
126+
params.Scope = "system"
127+
params.Status = []string{compute_api.HOST_STATUS_RUNNING}
128+
details := false
129+
params.Details = &details
130+
hosts := []compute_api.HostDetails{}
131+
for {
132+
offset := len(hosts)
133+
params.Offset = &offset
134+
resp, err := compute.Hosts.List(s, jsonutils.Marshal(params))
135+
if err != nil {
136+
return
137+
}
138+
part := []compute_api.HostDetails{}
139+
err = jsonutils.Update(&part, resp.Data)
140+
if err != nil {
141+
return
142+
}
143+
hosts = append(hosts, part...)
144+
if len(hosts) >= resp.Total {
145+
break
146+
}
147+
}
148+
metrics := []influxdb.SMetricData{}
149+
for _, host := range hosts {
150+
service := fmt.Sprintf("host-%s", host.Name)
151+
part, err := collectWorkerMetrics(ctx, host.ManagerUri, service, version.GetShortString(), tk)
152+
if err != nil {
153+
log.Errorf("collect host %s metric error: %v", service, err)
154+
continue
155+
}
156+
metrics = append(metrics, part...)
157+
}
158+
influxdb.SendMetrics(urls, SYSTEM_METRIC_DATABASE, metrics, false)
118159
}
119160

120161
func collectStatsMetrics(ctx context.Context, ep api.EndpointDetails, version, token string) ([]influxdb.SMetricData, error) {
@@ -257,8 +298,8 @@ func collectStatsMetrics(ctx context.Context, ep api.EndpointDetails, version, t
257298
return result, nil
258299
}
259300

260-
func collectWorkerMetrics(ctx context.Context, ep api.EndpointDetails, version, token string) ([]influxdb.SMetricData, error) {
261-
statsUrl := httputils.JoinPath(ep.Url, "worker_stats")
301+
func collectWorkerMetrics(ctx context.Context, url, service, version, token string) ([]influxdb.SMetricData, error) {
302+
statsUrl := httputils.JoinPath(url, "worker_stats")
262303
hdr := http.Header{}
263304
hdr.Set("X-Auth-Token", token)
264305
_, ret, err := httputils.JSONRequest(
@@ -301,7 +342,7 @@ func collectWorkerMetrics(ctx context.Context, ep api.EndpointDetails, version,
301342
},
302343
{
303344
Key: "service",
304-
Value: ep.ServiceName,
345+
Value: service,
305346
},
306347
{
307348
Key: "worker_name",
@@ -484,7 +525,7 @@ func collectServiceMetrics(ctx context.Context, ep api.EndpointDetails, version,
484525
errs = append(errs, err)
485526
}
486527
ret = append(ret, stats...)
487-
worker, err := collectWorkerMetrics(ctx, ep, version, token)
528+
worker, err := collectWorkerMetrics(ctx, ep.Url, ep.ServiceName, version, token)
488529
if err != nil {
489530
errs = append(errs, err)
490531
}

pkg/monitor/dbinit/measurements/all.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ var All = []SMeasurement{
3131
system,
3232
vasmi,
3333

34+
worker,
35+
serviceHttpCode,
36+
serviceProcessStats,
37+
dbStats,
38+
3439
vmCpu,
3540
vmMem,
3641
vmDisk,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package measurements
16+
17+
import "yunion.io/x/onecloud/pkg/apis/monitor"
18+
19+
var dbStats = SMeasurement{
20+
Context: []SMonitorContext{
21+
{
22+
"db_stats", "Database Stats",
23+
monitor.METRIC_RES_TYPE_DB_STATS, monitor.METRIC_DATABASE_SYSTEM,
24+
},
25+
},
26+
Metrics: []SMetric{
27+
{
28+
"idle", "Database Idle", monitor.METRIC_UNIT_NULL,
29+
},
30+
{
31+
"in_use", "Database InUse", monitor.METRIC_UNIT_NULL,
32+
},
33+
{
34+
"max_idle_closed", "Database max idle closed", monitor.METRIC_UNIT_NULL,
35+
},
36+
{
37+
"max_idle_time_closed", "Database max idle time closed", monitor.METRIC_UNIT_NULL,
38+
},
39+
{
40+
"max_lifetime_closed", "Database max lifetime closed", monitor.METRIC_UNIT_NULL,
41+
},
42+
{
43+
"max_open_connections", "Database max open connections", monitor.METRIC_UNIT_NULL,
44+
},
45+
{
46+
"open_connections", "Database open connections", monitor.METRIC_UNIT_NULL,
47+
},
48+
{
49+
"wait_count", "Database wait count", monitor.METRIC_UNIT_NULL,
50+
},
51+
{
52+
"wait_duration", "Database wait duration", monitor.METRIC_UNIT_NULL,
53+
},
54+
},
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package measurements
16+
17+
import "yunion.io/x/onecloud/pkg/apis/monitor"
18+
19+
var worker = SMeasurement{
20+
Context: []SMonitorContext{
21+
{
22+
"worker", "Worker queue",
23+
monitor.METRIC_RES_TYPE_WORKER, monitor.METRIC_DATABASE_SYSTEM,
24+
},
25+
},
26+
Metrics: []SMetric{
27+
{
28+
"active_worker_cnt", "Active Worker Count", monitor.METRIC_UNIT_NULL,
29+
},
30+
{
31+
"max_worker_count", "Max Worker Count", monitor.METRIC_UNIT_NULL,
32+
},
33+
{
34+
"detach_worker_cnt", "Detach worker Count", monitor.METRIC_UNIT_NULL,
35+
},
36+
{
37+
"queue_cnt", "Worker Queue Count", monitor.METRIC_UNIT_NULL,
38+
},
39+
},
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package measurements
16+
17+
import "yunion.io/x/onecloud/pkg/apis/monitor"
18+
19+
var serviceHttpCode = SMeasurement{
20+
Context: []SMonitorContext{
21+
{
22+
"http_request", "HTTP Request hit",
23+
monitor.METRIC_RES_TYPE_HTTP_REQUEST, monitor.METRIC_DATABASE_SYSTEM,
24+
},
25+
},
26+
Metrics: []SMetric{
27+
{
28+
"duration.2xx", "http code 2xxx duration", monitor.METRIC_UNIT_NULL,
29+
},
30+
{
31+
"duration.4xx", "http code 4xxx duration", monitor.METRIC_UNIT_NULL,
32+
},
33+
{
34+
"duration.5xx", "http code 5xxx duration", monitor.METRIC_UNIT_NULL,
35+
},
36+
{
37+
"hit.2xx", "http code 2xxx hit", monitor.METRIC_UNIT_NULL,
38+
},
39+
{
40+
"hit.4xx", "http code 4xxx hit", monitor.METRIC_UNIT_NULL,
41+
},
42+
{
43+
"hit.5xx", "http code 5xxx hit", monitor.METRIC_UNIT_NULL,
44+
},
45+
},
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package measurements
16+
17+
import "yunion.io/x/onecloud/pkg/apis/monitor"
18+
19+
var serviceProcessStats = SMeasurement{
20+
Context: []SMonitorContext{
21+
{
22+
"process", "Service process stats",
23+
monitor.METRIC_RES_TYPE_PROCESS, monitor.METRIC_DATABASE_SYSTEM,
24+
},
25+
},
26+
Metrics: []SMetric{
27+
{
28+
"cpu_percent", "CPU percent", monitor.METRIC_UNIT_NULL,
29+
},
30+
{
31+
"mem_percent", "Memory percent", monitor.METRIC_UNIT_NULL,
32+
},
33+
{
34+
"mem_size", "Memory size", monitor.METRIC_UNIT_NULL,
35+
},
36+
{
37+
"goroutine_num", "Goroutine num", monitor.METRIC_UNIT_NULL,
38+
},
39+
},
40+
}

0 commit comments

Comments
 (0)