Skip to content

Commit

Permalink
feat: Profile support grafana
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaochaoren1 committed Jul 26, 2024
1 parent 4801ff8 commit 27a1db8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/querier/profile/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ type Value struct {
Columns []string `json:"columns"`
Values [][]int `json:"values"`
}

type GrafanaProfileValue struct {
Columns []string `json:"columns"`
Values [][]interface{} `json:"values"`
}
26 changes: 26 additions & 0 deletions server/querier/profile/router/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

func ProfileRouter(e *gin.Engine, cfg *config.QuerierConfig) {
e.POST("/v1/profile/ProfileTracing", profileTracing(cfg))
e.POST("/v1/profile/Grafana", grafanaTracing(cfg))
}

func profileTracing(cfg *config.QuerierConfig) gin.HandlerFunc {
Expand All @@ -54,3 +55,28 @@ func profileTracing(cfg *config.QuerierConfig) gin.HandlerFunc {
router.JsonResponse(c, result, debug, err)
})
}

func grafanaTracing(cfg *config.QuerierConfig) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
var profileTracing model.ProfileTracing

// 参数校验
err := c.ShouldBindBodyWith(&profileTracing, binding.JSON)
if err != nil {
router.BadRequestResponse(c, common.INVALID_POST_DATA, err.Error())
return
}
profileTracing.Context = c.Request.Context()
profileTracing.OrgID = c.Request.Header.Get(common.HEADER_KEY_X_ORG_ID)
if profileTracing.MaxKernelStackDepth == nil {
var maxKernelStackDepth = common.MAX_KERNEL_STACK_DEPTH_DEFAULT
profileTracing.MaxKernelStackDepth = &maxKernelStackDepth
}
result, debug, err := service.Tracing(profileTracing, cfg)
grafanaResult := service.GrafanaTracing(result)
if err == nil && !profileTracing.Debug {
debug = nil
}
router.JsonResponse(c, grafanaResult, debug, err)
})
}
59 changes: 59 additions & 0 deletions server/querier/profile/service/grafana_trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package service

import (
"github.com/deepflowio/deepflow/server/querier/profile/model"
)

func GrafanaTracing(tree model.ProfileTree) (result *model.GrafanaProfileValue) {
result = &model.GrafanaProfileValue{}
result.Columns = []string{"level", "function", "self_value", "total_value"}
nodes := tree.NodeValues.Values

// node_child_ids
newNodes := [][5]interface{}{}
for _, node := range nodes {
// columns: ["function_id", "parent_node_id", "self_value", "total_value", "node_child_ids"]
newNode := [5]interface{}{}
newNode[0] = node[0]
newNode[1] = node[1]
newNode[2] = node[2]
newNode[3] = node[3]
newNode[4] = &[]int{}
newNodes = append(newNodes, newNode)
}

// update node_child_ids
for i, newNode := range newNodes {
if newNode[1] != -1 {
childIDs := newNodes[newNode[1].(int)][4].(*[]int)
*childIDs = append(*childIDs, i)
}
}
convertNode(newNodes[0], 0, result, tree.Functions, newNodes)
return
}

// convert to the data format required by Grafana
func convertNode(node [5]interface{}, level int, result *model.GrafanaProfileValue, functions []string, newNodes [][5]interface{}) {
result.Values = append(result.Values, []interface{}{level, functions[node[0].(int)], node[2].(int), node[3].(int)})
childIDs := node[4].(*[]int)
for _, childID := range *childIDs {
convertNode(newNodes[childID], level+1, result, functions, newNodes)
}
}

0 comments on commit 27a1db8

Please sign in to comment.