Skip to content

Commit 55f567b

Browse files
author
qinwenshi
committed
Release 0.1.2
1 parent 8cb2099 commit 55f567b

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
# --------
55
# IntelliJ
66
*.iml
7-
.idea/*
7+
.idea/*
8+
go.mod
9+
go.sum

beans/ab_config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ type ABTestConfig struct {
2929
*/
3030
EnableEventCache bool
3131

32+
/**
33+
开启请求耗时记录
34+
*/
35+
EnableRecordRequestCostTime bool
36+
3237
/*
3338
API 地址
3439
*/

experiment_helper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func loadExperimentFromNetwork(sensors *SensorsABTest, distinctId string, isLogi
2222
if requestParam.TimeoutMilliseconds <= 0 {
2323
requestParam.TimeoutMilliseconds = 3 * 1000
2424
}
25-
response, err := requestExperimentOnNetwork(sensors.config.APIUrl, distinctId, isLoginId, requestParam)
25+
response, err := requestExperimentOnNetwork(sensors.config.APIUrl, distinctId, isLoginId, requestParam, sensors.config.EnableRecordRequestCostTime)
2626
if err != nil {
2727
return err, beans.Experiment{
2828
Result: requestParam.DefaultValue,
@@ -88,7 +88,7 @@ func loadExperimentFromCache(sensors *SensorsABTest, distinctId string, isLoginI
8888
var outExperiments []beans.InnerExperiment
8989
if isRequestNetwork {
9090
// 从网络请求试验
91-
response, err := requestExperimentOnNetwork(sensors.config.APIUrl, distinctId, isLoginId, requestParam)
91+
response, err := requestExperimentOnNetwork(sensors.config.APIUrl, distinctId, isLoginId, requestParam, sensors.config.EnableRecordRequestCostTime)
9292
if err != nil {
9393
return err, beans.Experiment{
9494
Result: requestParam.DefaultValue,
@@ -134,11 +134,11 @@ func loadExperimentFromCache(sensors *SensorsABTest, distinctId string, isLoginI
134134
}
135135

136136
// 从网络加载试验
137-
func requestExperimentOnNetwork(apiUrl string, distinctId string, isLoginId bool, requestParam beans.RequestParam) (utils.Response, error) {
137+
func requestExperimentOnNetwork(apiUrl string, distinctId string, isLoginId bool, requestParam beans.RequestParam, enableRecordRequestCostTime bool) (utils.Response, error) {
138138
if requestParam.TimeoutMilliseconds <= 0 {
139139
requestParam.TimeoutMilliseconds = 3 * 1000
140140
}
141-
return utils.RequestExperiment(apiUrl, buildRequestParam(distinctId, isLoginId, requestParam), time.Duration(requestParam.TimeoutMilliseconds)*time.Millisecond)
141+
return utils.RequestExperiment(apiUrl, buildRequestParam(distinctId, isLoginId, requestParam), time.Duration(requestParam.TimeoutMilliseconds)*time.Millisecond, enableRecordRequestCostTime)
142142
}
143143

144144
func trackABTestEventOuter(distinctId string, isLoginId bool, experiment beans.Experiment, sensors *SensorsABTest, properties map[string]interface{}, customIDs map[string]interface{}) {

sensors_abtesting.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
SDK_VERSION = "0.1.1"
11+
SDK_VERSION = "0.1.2"
1212
LIB_NAME = "Golang"
1313
)
1414

@@ -155,6 +155,7 @@ func initConfig(abConfig beans.ABTestConfig) (error, beans.ABTestConfig) {
155155

156156
config.SensorsAnalytics = abConfig.SensorsAnalytics
157157
config.EnableEventCache = abConfig.EnableEventCache
158+
config.EnableRecordRequestCostTime = abConfig.EnableRecordRequestCostTime
158159
config.APIUrl = abConfig.APIUrl
159160
initCache(config)
160161
return nil, config

utils/network.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"github.com/sensorsdata/abtesting-sdk-go/beans"
89
"io/ioutil"
910
"net/http"
11+
"strconv"
1012
"strings"
1113
"time"
1214
)
1315

14-
func RequestExperiment(url string, requestPrams map[string]interface{}, to time.Duration) (Response, error) {
16+
func RequestExperiment(url string, requestPrams map[string]interface{}, to time.Duration, enableRecordRequestCostTime bool) (Response, error) {
1517
var resp *http.Response
1618

1719
data, _ := json.Marshal(requestPrams)
1820

1921
req, _ := http.NewRequest("POST", url, bytes.NewReader(data))
2022

23+
abRequestStartTime := time.Now().UnixNano() / int64(time.Millisecond)
24+
req.Header.Add("X-AB-Request-Start-Time", fmt.Sprintf("%v", abRequestStartTime))
2125
req.Header.Add("Content-Type", "application/json")
2226

2327
client := &http.Client{Timeout: to}
2428
resp, err := client.Do(req)
2529

30+
if enableRecordRequestCostTime {
31+
abRequestEndTime := time.Now().UnixNano() / int64(time.Millisecond)
32+
recordAbRequestCostTime(resp, abRequestStartTime, abRequestEndTime)
33+
}
34+
2635
if err != nil {
2736
return Response{}, err
2837
}
@@ -49,6 +58,36 @@ func RequestExperiment(url string, requestPrams map[string]interface{}, to time.
4958
}
5059
}
5160

61+
func recordAbRequestCostTime(response *http.Response, abRequestStartTime int64, abRequestEndTime int64) {
62+
abRequestId := getAbRequestIdFromResponse(response)
63+
abRequestProcessTime := getAbRequestProcessTimeFromResponse(response)
64+
abRequestTotalTime := strconv.FormatInt(abRequestEndTime-abRequestStartTime, 10)
65+
fmt.Println("record ab request time consumption. requestId: ", abRequestId, ", requestTotalTime:", abRequestTotalTime, "ms, abRequestProcessTime:", abRequestProcessTime, "ms")
66+
}
67+
68+
func getAbRequestIdFromResponse(response *http.Response) (abRequestId string) {
69+
if response != nil && response.Header != nil {
70+
abRequestId = response.Header.Get("X-AB-Request-Id")
71+
if abRequestId == "" {
72+
abRequestId = response.Header.Get("X-Request-Id")
73+
}
74+
if abRequestId != "" {
75+
return abRequestId
76+
}
77+
}
78+
return "unknown (not found)"
79+
}
80+
81+
func getAbRequestProcessTimeFromResponse(response *http.Response) (abRequestProcessTime string) {
82+
if response != nil && response.Header != nil {
83+
abRequestProcessTime = response.Header.Get("X-AB-Request-Process-Time")
84+
if abRequestProcessTime != "" {
85+
return abRequestProcessTime
86+
}
87+
}
88+
return "unknown (not found)"
89+
}
90+
5291
func defaultTrackConfig(response *Response, resMaps map[string]interface{}) {
5392
if !response.TrackConfig.TriggerSwitch {
5493
return

0 commit comments

Comments
 (0)