Skip to content

Commit

Permalink
move monitor and change details to proto (#1196)
Browse files Browse the repository at this point in the history
* move alert to core/monitor and change details-apis to proto

* solve conflict in main.go

* solve conflict in main.go

* modify monitor.yaml

* modify monitor.yaml

* modify import in monitor/main.go
  • Loading branch information
Counterflowwind authored Jul 28, 2021
1 parent f7d72c3 commit 7ca66d4
Show file tree
Hide file tree
Showing 46 changed files with 213 additions and 105 deletions.
5 changes: 3 additions & 2 deletions cmd/monitor/monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"github.com/erda-project/erda/modules/extensions/loghub"
"github.com/erda-project/erda/pkg/common"

_ "github.com/erda-project/erda/modules/core/monitor/alert/alert-apis"
_ "github.com/erda-project/erda/modules/core/monitor/alert/details-apis"

// modules
_ "github.com/erda-project/erda/modules/core/monitor/log/query"
_ "github.com/erda-project/erda/modules/core/monitor/metric/index"
Expand All @@ -28,8 +31,6 @@ import (
_ "github.com/erda-project/erda/modules/core/monitor/settings"
_ "github.com/erda-project/erda/modules/extensions/loghub/index/query"
_ "github.com/erda-project/erda/modules/extensions/loghub/metrics/rules"
_ "github.com/erda-project/erda/modules/monitor/alert/alert-apis"
_ "github.com/erda-project/erda/modules/monitor/alert/details-apis"
_ "github.com/erda-project/erda/modules/monitor/apm/report"
_ "github.com/erda-project/erda/modules/monitor/apm/runtime"
_ "github.com/erda-project/erda/modules/monitor/apm/topology"
Expand Down
2 changes: 1 addition & 1 deletion cmd/monitor/streaming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"github.com/erda-project/erda/pkg/common"

// modules
_ "github.com/erda-project/erda/modules/core/monitor/alert/storage/alert-record"
_ "github.com/erda-project/erda/modules/core/monitor/log/storage"
_ "github.com/erda-project/erda/modules/core/monitor/metric/storage"
_ "github.com/erda-project/erda/modules/monitor/alert/storage/alert-record"
_ "github.com/erda-project/erda/modules/monitor/notify/storage/notify-record"
_ "github.com/erda-project/erda/modules/msp/apm/browser"
_ "github.com/erda-project/erda/modules/msp/apm/trace/storage"
Expand Down
5 changes: 3 additions & 2 deletions conf/monitor/monitor/monitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ erda.core.monitor.metric:
- "${CONFIG_PATH}/metricmeta/groups/org.yml"
- "${CONFIG_PATH}/metricmeta/groups/micro_service.yml"
metric_meta_path: "${CONFIG_PATH}/metricmeta/metrics"

erda.core.monitor.alertdetail:

metric-query-example:
_enable: ${METRIC_QUERY_EXAMPLE_ENABLE:false}
metrics-query-compatibility:
Expand Down Expand Up @@ -91,7 +94,6 @@ apm-topology:
class: ${CASSANDRA_KEYSPACE_REPLICATION_CLASS:SimpleStrategy}
factor: ${CASSANDRA_KEYSPACE_REPLICATION_FACTOR:2}
apm-report:
#apm-alert:

erda.core.monitor.alert:
# micro_service_filter_tags: "${MICRO_SERVICE_FILTER_TAGS:metric_name,_metric_name,metric_scope,_metric_scope,metric_scope_id,_metric_scope_id,cluster_name,host_ip,org_id,org_name,project_id,project_name,application_id,application_name,runtime_id,runtime_name,service_id,service_name,component_name,addon_name,workspace,cpu_request,cpu_limit,cpu_origin,mem_request,mem_limit,mem_origin,job_id,addon_id,instance_id,instance_type,edas_app_id,edas_app_name,edas_group_id,pod_name,pod_namespace,pod_ip,terminus_key}"
Expand All @@ -105,7 +107,6 @@ erda.core.monitor.alert:
keyspace:
name: "spot_prod"
gc_grace_seconds: 86400
alert-details-apis:

report-apis:
pipeline:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ require (
github.com/dsnet/compress v0.0.1 // indirect
github.com/elastic/cloud-on-k8s v0.0.0-20210205172912-5ce0eca90c60
github.com/elazarl/goproxy v0.0.0-20200421181703-e76ad31c14f6
github.com/erda-project/erda-infra v0.0.0-20210727055933-be7518b10590
github.com/erda-project/erda-proto-go v0.0.0-20210727101457-c8e2cf5f8687
github.com/erda-project/erda-infra v0.0.0-20210725152943-8d1044e4fc39
github.com/erda-project/erda-proto-go v0.0.0-20210728101536-42e74f28ce7c
github.com/extrame/ole2 v0.0.0-20160812065207-d69429661ad7 // indirect
github.com/extrame/xls v0.0.1
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
Expand Down
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,14 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/erda-project/elastic v0.0.1-ex h1:5ajfxQ5S5YjpzFqY9LzL9hiKWCn6q/JDT4n8sNv7+pU=
github.com/erda-project/elastic v0.0.1-ex/go.mod h1:iAVsas6fcmt9pxtge1+dErMhecv+RLSXlD4rnZRJVW0=
github.com/erda-project/erda-infra v0.0.0-20210706133120-0a742437972c/go.mod h1:TUQYSZ60w9dk7m0q3U3AVg7U74APj/sdEVvRWR3wYv8=
github.com/erda-project/erda-infra v0.0.0-20210727055933-be7518b10590 h1:OPDlqUAh1A+nOLzMqa84XmjPA47ysTf1sYVLXlZv5Vg=
github.com/erda-project/erda-infra v0.0.0-20210727055933-be7518b10590/go.mod h1:L+fFQghY2po2P3H9pzwEOufDLAhL+mRRhPGdZ7vFnAw=
github.com/erda-project/erda-proto-go v0.0.0-20210727101457-c8e2cf5f8687 h1:GdzfpwdNSEdvoPx2MbK4wRwUC66Ce8UdyhB2nNkDyug=
github.com/erda-project/erda-proto-go v0.0.0-20210727101457-c8e2cf5f8687/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/erda-infra v0.0.0-20210725152943-8d1044e4fc39 h1:P03+DWh0qorAc9WUMsjj4P6Gq+jVDyhFUupCfyI3488=
github.com/erda-project/erda-infra v0.0.0-20210725152943-8d1044e4fc39/go.mod h1:L+fFQghY2po2P3H9pzwEOufDLAhL+mRRhPGdZ7vFnAw=
github.com/erda-project/erda-proto-go v0.0.0-20210726090325-2a861ab665ec h1:ge68Kqj5izVExtFznErr/+hqZuGPblEDBO+aDiCpIxA=
github.com/erda-project/erda-proto-go v0.0.0-20210726090325-2a861ab665ec/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/erda-proto-go v0.0.0-20210727031539-cace26f0e407 h1:zr4cUAOHfEOZXX9gVI74HdEkC0PIuTCOfSlH2sxYQpA=
github.com/erda-project/erda-proto-go v0.0.0-20210727031539-cace26f0e407/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/erda-proto-go v0.0.0-20210728101536-42e74f28ce7c h1:2pTXn1Lp7Td/qokddLOcj++45nFBPUZDUiWDahr0aOs=
github.com/erda-project/erda-proto-go v0.0.0-20210728101536-42e74f28ce7c/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/influxql v1.1.0-ex h1:NgP5+S5Qo234IVSIJ3N/egvzCNYJURfMAett3e8a9LE=
github.com/erda-project/influxql v1.1.0-ex/go.mod h1:gHp9y86a/pxhjJ+zMjNXiQAA197Xk9wLxaz+fGG+kWk=
github.com/erda-project/remotedialer v0.2.6-0.20210713103000-da03eb9e4b23 h1:NaKo6voQVqZM6DMBVhcTT4gjd+lr1C3zE17RROspfg0=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/metric/query/metricq"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
block "github.com/erda-project/erda/modules/monitor/dashboard/chart-block"
"github.com/erda-project/erda/modules/pkg/bundle-ex/cmdb"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/monitor/utils"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/monitor/utils"
"github.com/erda-project/erda/pkg/encoding/jsonmap"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/pkg/encoding/jsonmap"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/pkg/bundle-ex/cmdb"
"github.com/erda-project/erda/pkg/encoding/jsonmap"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/monitor/utils"
"github.com/erda-project/erda/pkg/encoding/jsonmap"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/monitor/utils"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"time"

"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/monitor/utils"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
metricpb "github.com/erda-project/erda-proto-go/core/monitor/metric/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/monitor/utils"
"github.com/erda-project/erda/pkg/common/apis"
"github.com/erda-project/erda/pkg/common/errors"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/adapt"
block "github.com/erda-project/erda/modules/monitor/dashboard/chart-block"
api "github.com/erda-project/erda/pkg/common/httpapi"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"github.com/erda-project/erda-infra/providers/mysql"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db"
"github.com/erda-project/erda/modules/core/monitor/metric/query/metricq"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/cql"
"github.com/erda-project/erda/modules/monitor/alert/alert-apis/db"
block "github.com/erda-project/erda/modules/monitor/dashboard/chart-block"
"github.com/erda-project/erda/modules/pkg/bundle-ex/cmdb"
"github.com/erda-project/erda/pkg/common/apis"
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions modules/core/monitor/alert/details-apis/alertdetail.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package details_apis

import (
"context"
"encoding/json"

"github.com/erda-project/erda-proto-go/core/monitor/alertdetail/pb"
"github.com/erda-project/erda/pkg/common/errors"
)

type alertDetailService struct {
p *provider
}

func (a *alertDetailService) QuerySystemPodMetrics(ctx context.Context, request *pb.QuerySystemPodMetricsRequest) (*pb.QuerySystemPodMetricsResponse, error) {
start := request.Timestamp - 30*60*1000
end := request.Timestamp + 30*60*1000
pod, err := a.p.getPodInfo(request.ClusterName, request.Name, start, end)
if err != nil {
return nil, errors.NewInternalServerError(err)
}
data, err := json.Marshal(pod)
if err != nil {
return nil, errors.NewInternalServerError(err)
}
result := &pb.QuerySystemPodMetricsResponse{
Data: &pb.PodInfo{
Summary: nil,
Instances: make([]*pb.PodInfoInstanse, 0),
},
}
err = json.Unmarshal(data, result.Data)
if err != nil {
return nil, errors.NewInternalServerError(err)
}
return result, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
package details_apis

import (
"context"
"fmt"
"strconv"

"github.com/erda-project/erda-infra/pkg/transport/http"
"github.com/erda-project/erda-infra/providers/httpserver"
api "github.com/erda-project/erda/pkg/common/httpapi"
"github.com/erda-project/erda/pkg/common/permission"
)

func (p *provider) getOrgIDByClusters(ctx httpserver.Context) (string, error) {
Expand Down Expand Up @@ -50,3 +53,39 @@ func (p *provider) getOrgIDByClusters(ctx httpserver.Context) (string, error) {
}
return idStr, nil
}

func (p *provider) OrgIDByCluster(key string) permission.ValueGetter {
return func(ctx context.Context, req interface{}) (string, error) {
orgIdValue := permission.OrgIDValue()
orgIdStr, err := orgIdValue(ctx, req)
orgID, err := strconv.ParseUint(orgIdStr, 10, 64)
if err != nil {
return "", fmt.Errorf("Org-ID is not number")
}
request := http.ContextRequest(ctx)
cluster := request.URL.Query().Get(key)
if len(cluster) <= 0 {
return "", fmt.Errorf("cluster must not be empty")
}
err = p.checkOrgIDsByCluster(orgID, cluster)
if err != nil {
return "", err
}
return orgIdStr, nil
}
}

func (p *provider) checkOrgIDsByCluster(orgID uint64, clusterName string) error {
resp, err := p.cmdb.QueryAllOrgClusterRelation()
if err != nil {
return err
}
for _, item := range resp {
if item.ClusterName == clusterName {
if orgID == item.OrgID {
return nil
}
}
}
return fmt.Errorf("not found cluster '%s'", clusterName)
}
88 changes: 88 additions & 0 deletions modules/core/monitor/alert/details-apis/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package details_apis

import (
"time"

"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/pkg/transport"
"github.com/erda-project/erda-infra/providers/httpserver"
"github.com/erda-project/erda-infra/providers/httpserver/interceptors"
"github.com/erda-project/erda-proto-go/core/monitor/alertdetail/pb"
"github.com/erda-project/erda/modules/core/monitor/metric/query/metricq"
"github.com/erda-project/erda/modules/pkg/bundle-ex/cmdb"
"github.com/erda-project/erda/pkg/common/apis"
perm "github.com/erda-project/erda/pkg/common/permission"
"github.com/erda-project/erda/pkg/http/httpclient"
)

type config struct {
}

type provider struct {
L logs.Logger
metricq metricq.Queryer
//metricq metricpb.MetricServiceServer `autowired:"erda.core.monitor.metric.MetricService"`
cmdb *cmdb.Cmdb

Register transport.Register `autowired:"service-register"`
Perm perm.Interface `autowired:"permission"`
alertDetailService *alertDetailService
}

func (p *provider) Init(ctx servicehub.Context) error {
hc := httpclient.New(httpclient.WithTimeout(time.Second, time.Second*60))
p.cmdb = cmdb.New(cmdb.WithHTTPClient(hc))
p.metricq = ctx.Service("metrics-query").(metricq.Queryer)
p.alertDetailService = &alertDetailService{
p: p,
}

if p.Register != nil {
type AlertDetailService = pb.AlertDetailServiceServer
pb.RegisterAlertDetailServiceImp(p.Register, p.alertDetailService, apis.Options(), p.Perm.Check(
perm.Method(AlertDetailService.QuerySystemPodMetrics, perm.ScopeOrg, "monitor_org_center", perm.ActionGet, p.OrgIDByCluster("clusterName")),
))
}
routes := ctx.Service("http-server",
//telemetry.HttpMetric(),
interceptors.Recover(p.L)).(httpserver.Router)
return p.intRoutes(routes)
}

func (p *provider) Provide(ctx servicehub.DependencyContext, args ...interface{}) interface{} {
switch {
case ctx.Service() == "erda.core.monitor.alertdetail" || ctx.Type() == pb.AlertDetailServiceServerType() || ctx.Type() == pb.AlertDetailServiceHandlerType():
return p.alertDetailService
}
return p
}

func init() {
servicehub.Register("erda.core.monitor.alertdetail", &servicehub.Spec{
Services: pb.ServiceNames(),
Types: pb.Types(),
Dependencies: []string{"metrics-query"},
OptionalDependencies: []string{"service-register"},
Description: "",
ConfigFunc: func() interface{} {
return &config{}
},
Creator: func() servicehub.Provider {
return &provider{}
},
})
}
Loading

0 comments on commit 7ca66d4

Please sign in to comment.