This repository was archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xbkaishui
authored
Sep 13, 2020
1 parent
fdae247
commit 5ff73ea
Showing
7 changed files
with
170 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package go2sky | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
type Sampler interface { | ||
IsSampled(operation string) (sampled bool) | ||
} | ||
|
||
type ConstSampler struct { | ||
decision bool | ||
} | ||
|
||
// NewConstSampler creates a ConstSampler. | ||
func NewConstSampler(sample bool) *ConstSampler { | ||
s := &ConstSampler{ | ||
decision: sample, | ||
} | ||
return s | ||
} | ||
|
||
// IsSampled implements IsSampled() of Sampler. | ||
func (s *ConstSampler) IsSampled(operation string) bool { | ||
return s.decision | ||
} | ||
|
||
type RandomSampler struct { | ||
samplingRate float64 | ||
rand *rand.Rand | ||
threshold int | ||
} | ||
|
||
// IsSampled implements IsSampled() of Sampler. | ||
func (s *RandomSampler) IsSampled(operation string) bool { | ||
return s.threshold >= s.rand.Intn(100) | ||
} | ||
|
||
func (s *RandomSampler) init() { | ||
s.rand = rand.New(rand.NewSource(time.Now().Unix())) | ||
s.threshold = int(s.samplingRate * 100) | ||
} | ||
|
||
func NewRandomSampler(samplingRate float64) *RandomSampler { | ||
s := &RandomSampler{ | ||
samplingRate: samplingRate, | ||
} | ||
s.init() | ||
return s | ||
} |
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,47 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package go2sky | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestConstSampler_IsSampled(t *testing.T) { | ||
sampler := NewConstSampler(true) | ||
operationName := "op" | ||
sampled := sampler.IsSampled(operationName) | ||
if sampled != true { | ||
t.Errorf("const sampler should be sampled") | ||
} | ||
samplerNegative := NewConstSampler(false) | ||
sampledNegative := samplerNegative.IsSampled(operationName) | ||
if sampledNegative != false { | ||
t.Errorf("const sampler should not be sampled") | ||
} | ||
} | ||
|
||
func TestRandomSampler_IsSampled(t *testing.T) { | ||
randomSampler := NewRandomSampler(0.5) | ||
//just for test case | ||
randomSampler.threshold = 100 | ||
operationName := "op" | ||
sampled := randomSampler.IsSampled(operationName) | ||
if sampled != true { | ||
t.Errorf("const sampler should be sampled") | ||
} | ||
} |
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
5ff73ea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.5版本不支持采样,量大时报“reach max send buffer”,超过了maxSendQueueSize值,不知道啥时发布支持采样的版本。最好智能采样,小压力时全量上报,大压力时自动调整采样率。业务的一次交易可能产生几100万的上报,这个时候只能采样压缩数据。
5ff73ea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eyjian Hi 我可以现在近期发布个 0.6.0的版本
5ff73ea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
及用采样特性,这个月能发布0.6.0吗?