Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加阿里云 trace url #241

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/apiv1/proxyintegrat/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package proxyintegrat

import "github.com/labstack/echo/v4"

//Group api group
// Group api group
func Group(g *echo.Group) {
g.GET("/uilist", UIList)
g.GET("/traceURL", TraceURL)
g.GET("/proxyMenuList", ProxyMenuList)
g.POST("/proxyMenuCreateOrUpdate", ProxyMenuCreateOrUpdate)
g.POST("/proxyMenuDelete", ProxyMenuDelete)

g.GET("/proxyManageList", ProxyManageList)
g.POST("/proxyManageCreateOrUpdate", ProxyManageCreateOrUpdate)
g.POST("/proxyManageDelete", ProxyManageDelete)

}
22 changes: 22 additions & 0 deletions api/apiv1/proxyintegrat/trace_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package proxyintegrat

import (
"github.com/douyu/juno/internal/pkg/packages/contrib/output"
"github.com/douyu/juno/internal/pkg/service/aliyunlog"
"github.com/douyu/juno/internal/pkg/service/user"
"github.com/labstack/echo/v4"
)

// TraceUrl 阿里云trace地址获取
func TraceURL(c echo.Context) (err error) {
u := user.GetUser(c)
toURL := c.QueryParam("toURL")
data, err := aliyunlog.Instance.CompleteTraceSearchUrl(c.Request().Context(), &aliyunlog.CompleteTraceSearchUrlRequest{
Region: "华北2(北京)",
ToURL: toURL,
}, u)
if err != nil {
return output.JSON(c, output.MsgErr, err.Error())
}
return output.JSON(c, output.MsgOk, "", map[string]interface{}{"url": data})
}
1 change: 1 addition & 0 deletions app/adminengine/router_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,5 @@ func apiAdmin(server *xecho.Server) {
{
loggerGroup.GET("/logstore", core.Handle(loggerplatform.LogStore))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ModalCreateResourceInterface {
export const PanelTypes = [
{ title: 'Grafana', val: 'grafana' },
{ title: 'Pyroscope', val: 'pyroscope' },
{ title: 'AliTrace', val: 'alitrace' },
];

function ModalCreateResource(props: ModalCreateResourceInterface) {
Expand Down
2 changes: 1 addition & 1 deletion assets/src/pages/proxy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const renderPanel = (config) => {
case 'welcome':
return <WelcomeView {...config.config} />;
default:
return <ProxyView {...config.config} />;
return <ProxyView {...config.config} panelType={config.panelType} />;
}
};

Expand Down
62 changes: 44 additions & 18 deletions assets/src/pages/proxy/proxyView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { traceURL } from "../services/proxyintegrat"

export default class Proxy extends React.Component<any, any> {
render() {
const { proxyURL } = this.props;
return (
<div
export default function (props) {
let [dynamicUrl, updateDynamicUrl] = useState('');
const { proxyURL, panelType } = props;
//支持动态嵌入页面
if (panelType == "alitrace") {
useEffect(() => {
traceURL({ toURL: proxyURL }).then((res) => {
if (res.code == 0) {
updateDynamicUrl(res.data && res.data.url);
}
})
}, [])
return dynamicUrl !== '' ? <div
style={{
width: '100%',
display: 'flex',
flex: 1,
}}
>
<iframe
src={dynamicUrl}
style={{
border: 'none',
width: '100%',
display: 'flex',
flex: 1,
}}
>
<iframe
src={proxyURL}
style={{
border: 'none',
width: '100%',
flex: 1,
}}
/>
</div>
);
/>
</div > : <div>请设置代理页面</div>
}
return (
<div
style={{
width: '100%',
display: 'flex',
flex: 1,
}}
>
<iframe
src={proxyURL}
style={{
border: 'none',
width: '100%',
flex: 1,
}}
/>
</div>
);
}
5 changes: 5 additions & 0 deletions assets/src/pages/proxy/services/proxyintegrat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import request from '@/utils/request';
import { stringify } from 'qs';

export async function uilist() {
return request('/api/admin/proxyintegrat/uilist');
}

export async function traceURL(params) {
return request(`/api/admin/proxyintegrat/traceURL?${stringify(params)}`);
}
3 changes: 3 additions & 0 deletions config/resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ permission:
- path: /api/admin/proxyintegrat/uilist
method: GET
name: 功能菜单
- path: /api/admin/proxyintegrat/traceURL
method: GET
name: trace授权地址
- name: 依赖拓扑
path: /analysis/topology
api:
Expand Down
58 changes: 55 additions & 3 deletions internal/pkg/service/aliyunlog/complete_log_search_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
"github.com/douyu/juno/pkg/model/db"
"github.com/douyu/jupiter/pkg/conf"
Expand All @@ -23,13 +25,18 @@ type CompleteLogSearchUrlRequest struct {
StartTime string `query:"startTime"`
EndTime string `query:"endTime"`
}

type CompleteTraceSearchUrlRequest struct {
Region string `query:"region"`
ToURL string `query:"toURL"`
}
type TokenBody struct {
SigninToken string `json:"SigninToken"`
}

//参考:https://help.aliyun.com/document_detail/74971.html

//CompleteLogSearchUrl 获取
// CompleteLogSearchUrl 获取
func (s *Service) CompleteLogSearchUrl(ctx context.Context, req *CompleteLogSearchUrlRequest, u *db.User) (signInUrl string, err error) {
client, ok := s.StsClients[req.Region]
if !ok {
Expand All @@ -43,6 +50,7 @@ func (s *Service) CompleteLogSearchUrl(ctx context.Context, req *CompleteLogSear
//设置参数。关于参数含义和设置方法,请参见《API参考》。
request.RoleArn = conf.GetString("aliyun.roleArn")
request.RoleSessionName = u.Username
request.DurationSeconds = requests.Integer(strconv.Itoa(3600 * 12))

//发起请求,并得到响应。
response, err := client.AssumeRole(request)
Expand Down Expand Up @@ -79,10 +87,54 @@ func (s *Service) CompleteLogSearchUrl(ctx context.Context, req *CompleteLogSear
query.Add("startTime", req.StartTime)
query.Add("endTime", req.EndTime)
}
// query.Add("readOnly", "true")
// query.Add("hiddenBack", "true")
query.Add("readOnly", "true")
query.Add("hiddenBack", "true")
toURL = toURL + "?" + query.Encode()
signInUrl = fmt.Sprintf("https://signin.aliyun.com/federation?Action=Login&LoginUrl=%s&Destination=%s&SigninToken=%s", conf.GetString("server.http.rootUrl"), url.QueryEscape(toURL), tokenItem.SigninToken)
return

}

//参考:https://help.aliyun.com/document_detail/123491.html

// CompleteLogSearchUrl 获取
func (s *Service) CompleteTraceSearchUrl(ctx context.Context, req *CompleteTraceSearchUrlRequest, u *db.User) (signInUrl string, err error) {
client, ok := s.StsClients[req.Region]
if !ok {
err = errors.New("no client")
return
}
//构建请求对象。
request := sts.CreateAssumeRoleRequest()
request.Scheme = "https"

//设置参数。关于参数含义和设置方法,请参见《API参考》。
request.RoleArn = conf.GetString("aliyun.roleArn")
request.RoleSessionName = u.Username
request.DurationSeconds = requests.Integer(strconv.Itoa(3600 * 12))

//发起请求,并得到响应。
response, err := client.AssumeRole(request)
if err != nil {
fmt.Print(err.Error())
}
tokenURl := fmt.Sprintf("https://signin.aliyun.com/federation?Action=GetSigninToken&AccessKeyId=%s&AccessKeySecret=%s&SecurityToken=%s&TicketType=mini",
url.QueryEscape(response.Credentials.AccessKeyId),
url.QueryEscape(response.Credentials.AccessKeySecret),
url.QueryEscape(response.Credentials.SecurityToken))
dataRes, err := http.Get(tokenURl)
if err != nil {
return
}
dataBytes, err := ioutil.ReadAll(dataRes.Body)
if err != nil {
return
}
tokenItem := new(TokenBody)
err = json.Unmarshal(dataBytes, tokenItem)
if err != nil {
return
}
signInUrl = fmt.Sprintf("https://signin.aliyun.com/federation?Action=Login&LoginUrl=%s&Destination=%s&SigninToken=%s", conf.GetString("server.http.rootUrl"), url.QueryEscape(req.ToURL), tokenItem.SigninToken)
return
}
6 changes: 3 additions & 3 deletions internal/pkg/service/loggerplatform/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
LogTypBiz = "biz"
)

//newAppLogD ..
// newAppLogD ..
func newAppLogD() {
var ald appLogDefault
ald.dashboardUrl = cfg.Cfg.AppLog.Default.DashboardUrl
Expand All @@ -60,7 +60,7 @@ func newAppLogD() {
return
}

//LogStore get log store
// LogStore get log store
func (a *appLogDefault) LogStore(env, query, typ, appName, aid string, u *db.User) (string, error) {
project, logStore := a.GetLogStoreName(env, typ, appName)
query = a.genQuery(env, typ, aid, query, appName)
Expand All @@ -81,7 +81,7 @@ func (a *appLogDefault) genQuery(env, typ, aid, query, appName string) string {
return query
}

//getLogStoreName ...
// getLogStoreName ...
func (a *appLogDefault) GetLogStoreName(env, typ, appName string) (string, string) {
var project AppLogDefaultEnvData
//业务日志进行拦截
Expand Down
10 changes: 5 additions & 5 deletions pkg/model/view/loggerplatform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ type (
}
//ReqAliyunLogDefault env, query, typ, appNam
ReqAliyunLogDefault struct {
Env string `json:"env" valid:"required"`
Query string `json:"query" valid:"required"`
Typ string `json:"typ" valid:"required"`
AppName string `json:"app_name" query:"app_name" valid:"required"`
Aid string `json:"aid" query:"aid" valid:"required"`
Env string `query:"env" json:"env" valid:"required"`
Query string `query:"query" json:"query" valid:"required"`
Typ string `query:"typ" json:"typ" valid:"required"`
AppName string `query:"app_name" json:"app_name" valid:"required"`
Aid string `query:"aid" json:"aid" valid:"required"`
}
)