当前位置:网站首页>分享一个Chrome控制台数据获取的例子
分享一个Chrome控制台数据获取的例子
2022-08-02 12:34:00 【InfoQ】
问题背景
sdk
SDK
apm
datadog ddtrace
zipkin
skywalking
otel
jaeger
APM
view
问题现象
view
sdk
<script src="https://static.guance.com/browser-sdk/v2/dataflux-rum.js" type="text/javascript"></script>
<script>
window.DATAFLUX_RUM &&
window.DATAFLUX_RUM.init({
applicationId: 'appid_e0da4faf71844ce68ff434db143eccc6',
datakitOrigin: 'https://aliyun-df-rum-dk.guance.com', // 协议(包括://),域名(或IP地址)[和端口号]
env: 'production',
version: '1.0.0',
trackInteractions: true,
traceType: 'ddtrace', // 非必填,默认为ddtrace,目前支持 ddtrace、zipkin、skywalking_v3、jaeger、zipkin_single_header、w3c_traceparent 6种类型
allowedTracingOrigins: ['https://console-api.guance.com', 'https://.*.my-api-domain.com/'], // 非必填,允许注入trace采集器所需header头部的所有请求列表。可以是请求的origin,也可以是是正则
})
</script>
traceType
allowedTracingOrigins
ddtrace
操作流程
/api/v1/board/add
x-datadog
trace_id
SDK
测试思路
https://console-api.guance.com
x-datadog-trace-id
xhr.log
trace_id=7866483901034644545
7866483901034644545
rum.log
diff
xhr.log
rum.log
Chrome devtools protocol
cdp
代码分享
$ tree
.
├── cdpdemo
├── cdpdemo.go
├── go.mod
├── go.sum
├── page.pdf
├── procer
│ ├── diff.go
│ └── req.go
├── rum_trace_id.log
└── xhr_trace_id.log
diff.go
package procer
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func Diff_trace_id() {
rum, err := os.OpenFile("rum_trace_id.log", os.O_RDONLY, 0644)
if err != nil {
fmt.Println("rum_trace_id.log file read error")
os.Exit(1)
}
rum_list := read(rum)
defer rum.Close()
xhr, err := os.OpenFile("xhr_trace_id.log", os.O_RDONLY, 0644)
if err != nil {
fmt.Println("xhr_trace_id.log file read error")
os.Exit(1)
}
xhr_list := read(xhr)
defer xhr.Close()
trace_ids := DiffArray(xhr_list, rum_list)
for _, item := range trace_ids {
fmt.Println(strings.Trim(item, "\n"))
}
}
func read(f *os.File) []string {
list := []string{}
bf := bufio.NewReader(f)
for {
line, err := bf.ReadSlice('\n')
if err != nil && err == io.EOF {
return list
}
list = append(list, string(line))
}
return list
}
func DiffArray(a []string, b []string) []string {
var diffArray []string
temp := map[string]struct{}{}
for _, val := range b {
if _, ok := temp[val]; !ok {
temp[val] = struct{}{}
}
}
for _, val := range a {
if _, ok := temp[val]; !ok {
diffArray = append(diffArray, val)
}
}
return diffArray
}
req.go
package procer
import (
"encoding/json"
"os"
"regexp"
"strings"
"sync"
"github.com/mafredri/cdp/protocol/network"
)
var rum *os.File
var xhr *os.File
func init() {
rum_trace_id, err := os.OpenFile("rum_trace_id.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
rum = rum_trace_id
if err != nil {
panic(err)
}
xhr, err = os.OpenFile("xhr_trace_id.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
}
func Handle(req network.RequestWillBeSentReply, wg sync.WaitGroup) {
defer wg.Done()
head := make(map[string]string)
if strings.Contains(req.Request.URL, "/v1/write/rum?precision") {
reg := regexp.MustCompile(`,?trace_id=(\d+),`)
list := reg.FindAllStringSubmatch(*req.Request.PostData, -1)
for _, item := range list {
if len(item[len(item)-1]) > 0 {
rum.WriteString(item[len(item)-1] + "\n")
}
}
return
}
if req.Request.Method != "OPTIONS" && req.Type != "Font" && req.Type != "Stylesheet" && req.Type != "Script" && req.Type != "Image" {
json.Unmarshal(req.Request.Headers, &head)
if len(head["x-datadog-trace-id"]) > 0 {
xhr.WriteString(head["x-datadog-trace-id"] + "\n")
}
return
}
}
cdpdemo.go
package main
import (
"cdpdemo/procer"
"context"
"flag"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/devtool"
"github.com/mafredri/cdp/rpcc"
)
var address map[string]*rpcc.Conn
var wg sync.WaitGroup
func main() {
targurl := flag.String("url", "https://console.guance.com/", "tagurl")
devtool_address := flag.String("devtool", "http://127.0.0.1:9222", "devtools address")
diff := flag.Bool("diff", false, "diff trace_id")
flag.Parse()
if *diff {
procer.Diff_trace_id()
os.Exit(0)
}
run(*targurl, *devtool_address, &wg)
wg.Wait()
}
func run(url, devtool_address string, wg *sync.WaitGroup) error {
address = make(map[string]*rpcc.Conn)
ctx := context.Background()
// Use the DevTools HTTP/JSON API to manage targets (e.g. pages, webworkers).
devt := devtool.New(devtool_address)
wg.Add(1)
go func() {
defer wg.Done()
for {
time.Sleep(time.Millisecond * 650)
tar, err := devt.List(ctx)
if err != nil {
return
}
for _, t := range tar {
if strings.HasPrefix(t.URL, url) {
rpconn, err := rpcc.Dial(t.WebSocketDebuggerURL)
if err != nil {
fmt.Println(err)
continue
}
if _, ok := address[t.WebSocketDebuggerURL]; !ok {
address[t.WebSocketDebuggerURL] = rpconn
wg.Add(1)
go action(rpconn, ctx, wg)
}
}
}
}
}()
return nil
}
func action(rpconn *rpcc.Conn, ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
cdpclient := cdp.NewClient(rpconn)
cdpclient.Network.Enable(ctx, nil)
net, err := cdpclient.Network.RequestWillBeSent(ctx)
if err != nil {
return
}
defer net.Close()
for {
reqdata, err := net.Recv()
if err != nil {
return
}
wg.Add(1)
go procer.Handle(*reqdata, *wg)
}
}
trace_id
$ go run cdpdemo.go -diff
5669634225797671685
SDK
参考连接
边栏推荐
猜你喜欢
Taurus.MVC V3.0.3 Microservice Open Source Framework Released: Make the evolution of .NET architecture easier in large concurrency.
Distributed current limiting, hand & redisson implementation
干测试这些年,去过阿里也去过小公司,给年轻测试员们一个忠告...
Free Chinese-English Translation Software - Automatic Batch Chinese-English Translation Software Recommended Daquan
FreeRTOS--Priority Experiment
simulink PID自动整定
一款强大的js弹出alert插件
SQL Server2019安装步骤及脱机安装Microsoft机器学习组件下一步不能继续的问题
SQL Server 2019 installation error 0 x80004005 service there is no timely response to the start or control request a detailed solution
消除气泡解救蘑菇h5小游戏源码
随机推荐
Taurus.MVC V3.0.3 微服务开源框架发布:让.NET 架构在大并发的演进过程更简单。
Process finished with exit code 1
unique in numpy & pandas
An example of type3 voltage loop compensator taking Boost as an example
FreeRTOS创建任务--动态创建、静态创建
如何关闭开启硬件加速[通俗易懂]
Openlayers Quick Start Tutorial
A powerful js pop-up alert plugin
Drools(8): WorkBench uses
如何更好评估信用贷风险?看这场评分卡模型直播就可以了
瀑布流式布局怎么实现(什么是瀑布流布局)
According to the field classification Golang map array
svg实现的树木四季变化
LeetCode_377_Combination Sum IV
linux basic command explanation
sql concat()函数
liunx基础命令讲解
SQL Server如何建表
0801~面试题梳理
FreeRTOS--优先级实验