-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* collector-agent and testcase * c/cpp changed close #535
- Loading branch information
Showing
10 changed files
with
330 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package agent | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
v1 "github.com/pinpoint-apm/pinpoint-c-agent/collector-agent/pinpoint-grpc-idl/proto/v1" | ||
) | ||
|
||
const bucketVersion = 0 | ||
const histogramSize = 8 | ||
|
||
type uriStatHistogram struct { | ||
Total int64 | ||
Max int64 | ||
TimestampHistogram [histogramSize]int32 | ||
} | ||
|
||
func (ust *uriStatHistogram) Update(span *TSpan) { | ||
elapseTime := span.GetElapsedTime() | ||
ust.Total += int64(elapseTime) | ||
|
||
if int64(elapseTime) > ust.Max { | ||
ust.Max = int64(elapseTime) | ||
} | ||
ust.TimestampHistogram[span.FindHistogramLevel()] += 1 | ||
} | ||
|
||
func (ust *uriStatHistogram) ToUriHistogrm() *v1.PUriHistogram { | ||
pbUriHistogram := &v1.PUriHistogram{ | ||
Total: ust.Total, | ||
Max: ust.Max, | ||
Histogram: ust.TimestampHistogram[:], | ||
} | ||
return pbUriHistogram | ||
} | ||
|
||
type statHisograms struct { | ||
TotalHistogram uriStatHistogram | ||
FailedHistogram uriStatHistogram | ||
} | ||
|
||
func (st *statHisograms) Update(span *TSpan) { | ||
st.TotalHistogram.Update(span) | ||
if span.IsFailed() { | ||
st.FailedHistogram.Update(span) | ||
} | ||
} | ||
|
||
type UrlTemplateReport struct { | ||
uriMap map[string]*statHisograms | ||
BucketVersion int32 | ||
mu sync.Mutex | ||
} | ||
|
||
func (utr *UrlTemplateReport) Interceptor(span *TSpan) bool { | ||
if len(span.UT) > 0 { | ||
// found uri templated | ||
utr.updateUriSnapshot(span) | ||
} | ||
return true | ||
} | ||
|
||
func (utr *UrlTemplateReport) updateUriSnapshot(span *TSpan) { | ||
utr.mu.Lock() | ||
defer utr.mu.Unlock() | ||
ut := span.UT | ||
var st *statHisograms | ||
var ok bool | ||
if st, ok = utr.uriMap[ut]; !ok { | ||
st = &statHisograms{} | ||
utr.uriMap[ut] = st | ||
} | ||
st.Update(span) | ||
} | ||
|
||
func (utr *UrlTemplateReport) MoveUtReprot() *v1.PStatMessage { | ||
utr.mu.Lock() | ||
defer utr.mu.Unlock() | ||
|
||
agentUriStat := &v1.PAgentUriStat{ | ||
BucketVersion: int32(utr.BucketVersion), | ||
} | ||
|
||
for url, st := range utr.uriMap { | ||
eachUriStat := &v1.PEachUriStat{ | ||
Uri: url, | ||
TotalHistogram: st.TotalHistogram.ToUriHistogrm(), | ||
FailedHistogram: st.FailedHistogram.ToUriHistogrm(), | ||
Timestamp: time.Now().UnixMilli(), | ||
} | ||
agentUriStat.EachUriStat = append(agentUriStat.EachUriStat, eachUriStat) | ||
} | ||
//note: create a new one | ||
utr.uriMap = make(map[string]*statHisograms) | ||
pbStat := &v1.PStatMessage{ | ||
Field: &v1.PStatMessage_AgentUriStat{ | ||
AgentUriStat: agentUriStat, | ||
}, | ||
} | ||
|
||
return pbStat | ||
} | ||
|
||
func CreateUrlTemplateReport() *UrlTemplateReport { | ||
ut := &UrlTemplateReport{ | ||
uriMap: make(map[string]*statHisograms), | ||
BucketVersion: bucketVersion, | ||
} | ||
return ut | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package agent | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUrlTemplateReport(t *testing.T) { | ||
spans := []TSpan{ | ||
{ | ||
UT: "/hello", | ||
Uri: "/hello", | ||
ElapsedTime: 32, | ||
}, | ||
{ | ||
UT: "/hello", | ||
Uri: "/hello", | ||
ElapsedTime: 320, | ||
}, | ||
{ | ||
UT: "/hello", | ||
Uri: "/hello", | ||
ElapsedTime: 3200, | ||
}, | ||
{ | ||
UT: "/hello_exp", | ||
Uri: "/hello", | ||
ElapsedTime: 32000, | ||
ExceptionInfo: "exp", | ||
}, | ||
} | ||
|
||
ut := CreateUrlTemplateReport() | ||
for _, span := range spans { | ||
ut.Interceptor(&span) | ||
} | ||
|
||
if len(ut.uriMap) < 2 { | ||
t.Log(len(ut.uriMap)) | ||
} | ||
|
||
pbStatMessage := ut.MoveUtReprot() | ||
t.Log(pbStatMessage) | ||
assert.NotEqual(t, pbStatMessage.GetAgentUriStat(), nil, "GetAgentUriStat") | ||
|
||
pbUriStat := pbStatMessage.GetAgentUriStat() | ||
|
||
assert.Equal(t, pbUriStat.GetBucketVersion(), int32(0), "GetBucketVersion") | ||
|
||
eachUriStat := pbUriStat.GetEachUriStat() | ||
|
||
assert.Equal(t, len(eachUriStat), 2, "len(eachUriStat)") | ||
|
||
assert.NotEqual(t, eachUriStat[0].GetFailedHistogram(), nil, "GetFailedHistogram") | ||
assert.NotEqual(t, eachUriStat[0].GetTotalHistogram(), nil, "GetTotalHistogram") | ||
totalHis := eachUriStat[0].GetTotalHistogram() | ||
assert.Equal(t, len(totalHis.GetHistogram()), histogramSize, "len(totalHis.GetHistogram())") | ||
|
||
assert.Equal(t, totalHis.Max, int64(3200), "totalHis.Max") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.