安装Golang Agent
步骤一:获取授权编号
授权编号是透视宝用户身份识别的License Key,每个用户对应一个唯一的授权编号,在后续配置中需要使用。
步骤二:下载Golang Agent
点击<下载Golang Agent(版本)>按钮或复制Wget链接,下载最新版本的安装包。
步骤三:安装Golang Agent
1、 配置系统环境变量。
如果系统设置了$GOPATH环境变量go env | grep GOPATH,不需要再配置。
如果$GOPATH环境变量不存在,则设置为export GOPATH=/yourGOPATH。
2、解压缩安装包到$GOPATH/src目录。
unzip GoAgent_Linux_2.1.0.zip -d $GOPATH/src
3、进入$GOPATH/src/cloudwise/conf目录下的agent.ini文件,配置License Key,app name等内容。
basic.app_name=golang_gin
basic.license_key=J45Engw88Nc/fR1k7e53nphkNSQbbc8JEfXHo5cek8NKBcPok1f56Q!!!!
server.setting_url=http://data.toushibao.com #实际按具体数据发送地址配置
server.data_url=http://data.toushibao.com
4、安装Golang Agent。
注意:安装前用如下命令备份源码,供恢复使用。
cp -r youproject youproject_bak
1)复制配置文件夹到程序目录。
cp -r $GOPATH/src/cloudwise/conf youproject/
2)注入Golang Agent。
a) 修改用户应用import包位置:
- 用户原组件(martini/gin/mysql/pgsql/redis/http)
// brfore:
import (
/* gin */
"github.com/gin-gonic/gin"
/* martini */
"github.com/go-martini/martini"
/* mysql */
_ "github.com/go-sql-driver/mysql"
/* pgsql */
_ "github.com/lib/pq"
/* redis(redigo) */
"github.com/gomodule/redigo/redis"
/* http */
"net/http"
"database/sql"
)
// after:
import (
"cloudwise"
/* gin */
"cloudwise/_integrations/cloudwise_gin/v1"
"github.com/gin-gonic/gin"
/* martini */
"cloudwise/_integrations/cloudwise_martini/v1"
/* mysql */
_ "cloudwise/_integrations/mysql"
/* pgsql */
_ "cloudwise/_integrations/pq"
/* redis */
"cloudwise/_integrations/redis/redigo/v1"
/* http */
"cloudwise/http"
"database/sql"
"context"
)
3)在用户mian函数中初始化gin。
import (
// before
"github.com/gin-gonic/gin"
// after
"cloudwise/_integrations/cloudwise_gin/v1"
"github.com/gin-gonic/gin"
)
func main() {
// before
router := gin.New() // gin.New()
// after
cloudwise.Init()
router := cloudwise_gin.New() // cloudwise_gin.Default()
}
在用户main函数中初始化martini 。
import (
// before
"github.com/go-martini/martini"
// after
"cloudwise/_integrations/cloudwise_martini/v1"
)
func main() {
// before
m := martini.Classic()
// after
cloudwise.Init()
m := cloudwise_martini.Classic()
}
MySQL的使用
import (
// before
...
_ "github.com/go-sql-driver/mysql"
// after
...
"cloudwise"
_ "cloudwise/_integrations/pq"
)
func mysql(c *gin.Context) {
// before
db, err := sql.Open("mysql", mysqlCon)
......
db.Query( "SELECT count(*) from user_zhx2018")
// after
db, err := sql.Open("cwmysql", mysqlCon)
......
ctx := cloudwise.NewContext(context.Background(), cloudwise.CwTxn)
db.QueryContext(ctx, "SELECT count(*) from user_zhx2018")
}
PostgreySQL的使用
import (
// before
...
_ "github.com/lib/pq"
// after
...
"cloudwise"
_ "cloudwise/_integrations/mysql"
)
func mysql(c *gin.Context) {
// before
db, err := sql.Open("postgres", pgsqlCon)
......
db.QueryRow( "SELECT count(*) FROM student")
// after
db, err := sql.Open("cwpostgres", pgsqlCon)
......
ctx := cloudwise.NewContext(context.Background(), cloudwise.CwTxn)
db.QueryRowContext(ctx, "SELECT count(*) FROM student")
}
Redis应用(redigo库)
import (
// before
...
"github.com/gomodule/redigo/redis"
// after
...
"cloudwise/_integrations/redis/redigo/v1"
)
端到端HTTP
import (
// before
...
"net/http"
// after
...
"cloudwise/http"
)
5、执行 go build main.go 重新编译应用。
6、开启/关闭debug。
#
开启
export AGENT_RUNMODE=dev
#
关闭
export AGENT_RUNMODE=prod
大致两分钟后您就可以在“应用”模块中查看数据。
注意:无人访问过的应用不会出现在应用清单中。