Skip to content

Commit

Permalink
fix: multi arch execution promql-parser (#1372)
Browse files Browse the repository at this point in the history
Co-authored-by: 曲源成 <[email protected]>
  • Loading branch information
quyuancheng and 曲源成 authored Aug 9, 2023
1 parent 7f89688 commit a8ca867
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion console/services/app_actions/properties_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def component_graph_changes(self, component_graphs):
add = []
for graph in component_graphs:
try:
new_promql = promql_service.add_or_update_label(self.service.service_id, graph.get("promql"))
new_promql = promql_service.add_or_update_label(self.service.service_id, graph.get("promql"), self.service.arch)
except AbortRequest as e:
logger.warning("promql: {}, {}".format(graph.get("promql"), e))
continue
Expand Down
16 changes: 8 additions & 8 deletions console/services/app_config/component_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def list_internal_graphs(self):
graphs, _ = self._load_internal_graphs()
return graphs

def create_internal_graphs(self, component_id, graph_name):
def create_internal_graphs(self, component_id, graph_name, component_arch):
_, internal_graphs = self._load_internal_graphs()
if not internal_graphs or not internal_graphs.get(graph_name):
raise ErrInternalGraphsNotFound
Expand All @@ -57,7 +57,7 @@ def create_internal_graphs(self, component_id, graph_name):
pass

try:
promql = promql_service.add_or_update_label(component_id, graph["promql"])
promql = promql_service.add_or_update_label(component_id, graph["promql"], component_arch)
except AbortRequest as e:
logger.warning("promql {}: {}".format(graph["promql"], e))
continue
Expand All @@ -74,8 +74,8 @@ def create_internal_graphs(self, component_id, graph_name):
ComponentGraph.objects.bulk_create(graphs)

@transaction.atomic
def create_component_graph(self, component_id, title, promql):
promql = promql_service.add_or_update_label(component_id, promql)
def create_component_graph(self, component_id, title, promql, component_arch):
promql = promql_service.add_or_update_label(component_id, promql, component_arch)
graph_id = make_uuid()
sequence = self._next_sequence(component_id)
if sequence > 10000:
Expand Down Expand Up @@ -108,18 +108,18 @@ def delete_by_component_id(self, component_id):
return component_graph_repo.delete_by_component_id(component_id)

@transaction.atomic()
def update_component_graph(self, graph, title, promql, sequence):
def update_component_graph(self, graph, title, promql, sequence, arch):
data = {
"title": title,
"promql": promql_service.add_or_update_label(graph.component_id, promql),
"promql": promql_service.add_or_update_label(graph.component_id, promql, arch),
}
if sequence != graph.sequence:
data["sequence"] = sequence
self._sequence_move_back(graph.component_id, sequence, graph.sequence)
component_graph_repo.update(graph.component_id, graph.graph_id, **data)
return component_graph_repo.get(graph.component_id, graph.graph_id).to_dict()

def bulk_create(self, component_id, graphs):
def bulk_create(self, component_id, graphs, arch):
if not graphs:
return
cgs = []
Expand All @@ -131,7 +131,7 @@ def bulk_create(self, component_id, graphs):
pass

try:
promql = promql_service.add_or_update_label(component_id, graph.get("promql"))
promql = promql_service.add_or_update_label(component_id, graph.get("promql"), arch)
except AbortRequest as e:
logger.warning("promql: {}, {}".format(graph.get("promql"), e))
continue
Expand Down
7 changes: 5 additions & 2 deletions console/services/app_config/promql_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

class PromQLService(object):
@staticmethod
def add_or_update_label(component_id, promql):
def add_or_update_label(component_id, promql, component_arch=""):
"""
Add service_id label, or replace illegal service_id label
"""
promql_parser = BASE_DIR + "/bin/" + platform.system().lower() + "/promql-parser"
promql_name = "/promql-parser"
if component_arch == "arm64":
promql_name = "/promql-parser-arm64"
promql_parser = BASE_DIR + "/bin/" + platform.system().lower() + promql_name
c = subprocess.Popen([os.getenv("PROMQL_PARSER", promql_parser), "--component_id", component_id],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand Down
6 changes: 3 additions & 3 deletions console/services/market_app/new_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def create_components(self):
# service monitors
monitors = self._template_to_service_monitors(cpt, component_tmpl.get("component_monitors"))
# graphs
graphs = self._template_to_component_graphs(cpt, component_tmpl.get("component_graphs"))
graphs = self._template_to_component_graphs(cpt, component_tmpl.get("component_graphs"), cpt.arch)
# component k8s attributes
k8s_attrs = self._template_to_k8s_attributes(cpt, component_tmpl.get("component_k8s_attributes"))
service_group_rel = ServiceGroupRelation(
Expand Down Expand Up @@ -433,13 +433,13 @@ def _template_to_service_monitors(self, component, service_monitors):
monitors.append(data)
return monitors

def _template_to_component_graphs(self, component, graphs):
def _template_to_component_graphs(self, component, graphs, arch):
if not graphs:
return []
new_graphs = {}
for graph in graphs:
try:
promql = promql_service.add_or_update_label(component.service_id, graph.get("promql"))
promql = promql_service.add_or_update_label(component.service_id, graph.get("promql"), arch)
except AbortRequest as e:
logger.warning("promql: {}, {}".format(graph.get("promql"), e))
continue
Expand Down
8 changes: 4 additions & 4 deletions console/services/market_app/property_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _get_component_change(self, component: Component, component_tmpl: map):
result["plugin_deps"] = plugin_deps

component_graphs = self._graphs(component.component.component_id, component.graphs,
component_tmpl.get("component_graphs", []))
component_tmpl.get("component_graphs", []), component.component.arch)
if component_graphs:
result["component_graphs"] = component_graphs

Expand Down Expand Up @@ -291,7 +291,7 @@ def _probe(old_probes, new_probes):
return result

@staticmethod
def _graphs(component_id, old_graphs, graphs):
def _graphs(component_id, old_graphs, graphs, arch):
"""
Support adding and updating promql
"""
Expand All @@ -308,8 +308,8 @@ def _graphs(component_id, old_graphs, graphs):
continue

try:
old_promql = promql_service.add_or_update_label(component_id, old_graph.promql)
new_promql = promql_service.add_or_update_label(component_id, graph.get("promql"))
old_promql = promql_service.add_or_update_label(component_id, old_graph.promql, arch)
new_promql = promql_service.add_or_update_label(component_id, graph.get("promql"), arch)
except AbortRequest as e:
logger.warning("promql: {}, {}".format(graph.get("promql"), e))
continue
Expand Down
2 changes: 1 addition & 1 deletion console/services/market_app_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def __save_component_meta(self, tenant, region, tenant_service_group, applicatio
self.__create_component_monitor(tenant, ts, component_monitors)
# component graphs
component_graphs = app.get("component_graphs") if app.get("component_graphs") else []
component_graph_service.bulk_create(ts.service_id, component_graphs)
component_graph_service.bulk_create(ts.service_id, component_graphs, ts.arch)
logger.debug("create component {0} take time {1}".format(ts.service_alias, datetime.datetime.now() - start))
return ts

Expand Down
8 changes: 5 additions & 3 deletions console/views/app_config/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def post(self, request, *args, **kwargs):
serializer = CreateComponentGraphReq(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.data
graph = component_graph_service.create_component_graph(self.service.service_id, data['title'], data['promql'])
graph = component_graph_service.create_component_graph(self.service.service_id, data['title'], data['promql'],
self.service.arch)
result = general_message(200, "success", "创建成功", bean=graph)
return Response(result, status=result["code"])

Expand All @@ -39,7 +40,8 @@ def put(self, request, *args, **kwargs):
serializer = UpdateComponentGraphReq(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.data
graphs = component_graph_service.update_component_graph(self.graph, data["title"], data["promql"], data["sequence"])
graphs = component_graph_service.update_component_graph(self.graph, data["title"], data["promql"], data["sequence"],
self.service.arch)
result = general_message(200, "success", "修改成功", list=graphs)
return Response(result, status=result["code"])

Expand All @@ -52,7 +54,7 @@ def delete(self, request, *args, **kwargs):
class ComponentInternalGraphsView(AppBaseView):
def post(self, request, *args, **kwargs):
graph_name = parse_item(request, "graph_name", required=True)
component_graph_service.create_internal_graphs(self.service.service_id, graph_name)
component_graph_service.create_internal_graphs(self.service.service_id, graph_name, self.service.arch)
result = general_message(200, "success", "导入成功")
return Response(result, status=result["code"])

Expand Down
4 changes: 2 additions & 2 deletions console/views/app_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get(self, request, *args, **kwargs):
try:
query = request.GET.get("query", "")
if "service_id" not in query:
query = promql_service.add_or_update_label(self.service.service_id, query)
query = promql_service.add_or_update_label(self.service.service_id, query, self.service.arch)
sufix = "?" + get_sufix_path(request, query)
res, body = region_api.get_query_data(self.service.service_region, self.tenant.tenant_name, sufix)
result = general_message(200, "success", "查询成功", bean=body["data"])
Expand Down Expand Up @@ -91,7 +91,7 @@ def get(self, request, *args, **kwargs):
query = request.GET.get("query", "")
disable_auto_label = request.GET.get("disable_auto_label", "false")
if "service_id" not in query and disable_auto_label == "false":
query = promql_service.add_or_update_label(self.service.service_id, query)
query = promql_service.add_or_update_label(self.service.service_id, query, self.service.arch)
sufix = "?" + get_sufix_path(request, query)
res, body = region_api.get_query_range_data(self.service.service_region, self.tenant.tenant_name, sufix)
result = general_message(200, "success", "查询成功", bean=body["data"])
Expand Down

0 comments on commit a8ca867

Please sign in to comment.