本文整理匯總了Golang中github.com/dparrish/openinstrument/variable.NewFromString函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewFromString函數的具體用法?Golang NewFromString怎麽用?Golang NewFromString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewFromString函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestMerge
func (s *MySuite) TestMerge(c *C) {
input := []*oproto.ValueStream{
&oproto.ValueStream{
Variable: variable.NewFromString("/test{host=a}").AsProto(),
Value: []*oproto.Value{
&oproto.Value{Timestamp: 1, DoubleValue: 1.0},
&oproto.Value{Timestamp: 4, DoubleValue: 4.0},
},
},
&oproto.ValueStream{
Variable: variable.NewFromString("/test{host=b}").AsProto(),
Value: []*oproto.Value{
&oproto.Value{Timestamp: 2, DoubleValue: 2.0},
&oproto.Value{Timestamp: 5, DoubleValue: 5.0},
},
},
&oproto.ValueStream{
Variable: variable.NewFromString("/test{host=c}").AsProto(),
Value: []*oproto.Value{
&oproto.Value{Timestamp: 3, DoubleValue: 3.0},
&oproto.Value{Timestamp: 6, DoubleValue: 6.0},
},
},
}
outCount, err := checkValueOrder(Merge(input))
if err != nil {
c.Error(err)
}
c.Check(outCount, Equals, 6)
}
示例2: TestMeanByTwoLabels
func (s *MySuite) TestMeanByTwoLabels(c *C) {
input := []*oproto.ValueStream{
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=a,job=foo,other=w}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=a,job=bar,other=x}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=b,job=foo,other=y}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=b,job=bar,other=z}")),
}
output := aggregations.Mean([]string{"host", "job"}, input)
c.Assert(output, Not(IsNil))
c.Assert(len(output), Equals, 4)
// Check that there are 4 output streams with the correct number of output labels
checkHostsAndJobs(c, output, 2, 2, 2, 2)
}
示例3: ListVariables
func ListVariables(ctx context.Context, ds *datastore.Datastore, w http.ResponseWriter, req *http.Request) {
prefix := req.FormValue("p")
if prefix == "" {
prefix = "/*"
}
if prefix[len(prefix)-1] != '*' {
prefix = prefix + "*"
}
v := variable.NewFromString(prefix)
vars := make(map[string]*oproto.StreamVariable)
for stream := range ds.Reader(ctx, v) {
vars[stream.Variable.Name] = stream.Variable
}
if len(vars) == 0 {
w.WriteHeader(404)
return
}
w.WriteHeader(200)
keys := make([]string, len(vars))
i := 0
for k := range vars {
keys[i] = k
i++
}
sort.Strings(keys)
out, _ := json.Marshal(keys)
w.Header().Set("Content-Type", "text/json")
w.Write(out)
}
示例4: TestRatio
func (s *MySuite) TestRatio(c *C) {
ctx, cancel := context.WithCancel(context.Background())
e := NewExporter(ctx, s.Client, 10*time.Second)
r := NewRatio(e, variable.NewFromString("/test/ratio"))
for i := 0; i < 10; i++ {
r.Success()
}
for i := 0; i < 5; i++ {
r.Failure()
}
// Force export
streams := e.GetStreams()
c.Assert(len(streams), Equals, 3)
for _, stream := range streams {
switch variable.ProtoToString(stream.Variable) {
case "/test/ratio-success":
c.Check(stream.Value[0].DoubleValue, Equals, 10.0)
case "/test/ratio-failure":
c.Check(stream.Value[0].DoubleValue, Equals, 5.0)
case "/test/ratio-total":
c.Check(stream.Value[0].DoubleValue, Equals, 15.0)
default:
fmt.Printf("Invalid variable %s", variable.ProtoToString(stream.Variable))
c.Fail()
}
}
cancel()
<-ctx.Done()
}
示例5: TestDefaultDropPolicy
func (s *MySuite) TestDefaultDropPolicy(c *C) {
policyTxt := `
interval: 3600
policy {
comment: "Throw everything away"
policy: DROP
}
`
policyProto := &oproto.RetentionPolicy{}
c.Assert(proto.UnmarshalText(policyTxt, policyProto), IsNil)
policy := New(policyProto)
input := &oproto.ValueStream{
Variable: variable.NewFromString("/test/foo/bar").AsProto(),
Value: []*oproto.Value{},
}
for i := 0; i < 10; i++ {
input.Value = append(input.Value, &oproto.Value{Timestamp: uint64(i), DoubleValue: 1.1})
}
output := policy.Apply(input)
for _, value := range output.Value {
log.Printf("Got output when none was expected: %s", value)
c.Fail()
}
}
示例6: TestTimer
func (s *MySuite) TestTimer(c *C) {
ctx, cancel := context.WithCancel(context.Background())
e := NewExporter(ctx, s.Client, 10*time.Second)
t := NewTimer(e, variable.NewFromString("/test/timer"))
t.Start()
time.Sleep(10 * time.Millisecond)
t.Stop()
t.Start()
time.Sleep(10 * time.Millisecond)
t.Stop()
// Force export
streams := e.GetStreams()
c.Assert(len(streams), Equals, 2)
for _, stream := range streams {
switch variable.ProtoToString(stream.Variable) {
case "/test/timer-total-count":
c.Check((stream.Value[0].DoubleValue >= 20.0 && stream.Value[0].DoubleValue <= 25.0), Equals, true)
case "/test/timer-overall-sum":
c.Check(stream.Value[0].DoubleValue, Equals, 2.0)
default:
fmt.Printf("Invalid variable %s", variable.ProtoToString(stream.Variable))
c.Fail()
}
}
cancel()
<-ctx.Done()
}
示例7: TestAverage
func (s *MySuite) TestAverage(c *C) {
ctx, cancel := context.WithCancel(context.Background())
e := NewExporter(ctx, s.Client, 10*time.Second)
a := NewAverage(e, variable.NewFromString("/test/average"))
a.Update(95, 1)
a.Update(100, 1)
a.Update(105, 1)
// Force export
streams := e.GetStreams()
c.Assert(len(streams), Equals, 2)
for _, stream := range streams {
switch variable.ProtoToString(stream.Variable) {
case "/test/average-total-count":
c.Check(stream.Value[0].DoubleValue, Equals, 300.0)
case "/test/average-overall-sum":
c.Check(stream.Value[0].DoubleValue, Equals, 3.0)
default:
fmt.Printf("Invalid variable %s", variable.ProtoToString(stream.Variable))
c.Fail()
}
}
cancel()
<-ctx.Done()
}
示例8: main
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
flag.Parse()
conn, err := grpc.Dial(*connectAddress, grpc.WithInsecure())
if err != nil {
log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
}
defer conn.Close()
request := &oproto.LookupBlockRequest{}
if *varName != "" {
request.Variable = variable.NewFromString(*varName).AsProto()
} else if *ID != "" {
request.BlockId = *ID
} else if len(os.Args) > 1 {
request.BlockId = os.Args[1]
} else {
log.Fatal("Specify either --variable or --id")
}
stub := oproto.NewStoreClient(conn)
response, err := stub.LookupBlock(context.Background(), request)
if err != nil {
log.Fatal(err)
}
log.Println(openinstrument.ProtoText(response))
}
示例9: TestMergeBy
func (s *MySuite) TestMergeBy(c *C) {
input := []*oproto.ValueStream{
&oproto.ValueStream{
Variable: variable.NewFromString("/test{host=a,other=x}").AsProto(),
Value: []*oproto.Value{
&oproto.Value{Timestamp: 1, DoubleValue: 1.0},
&oproto.Value{Timestamp: 4, DoubleValue: 4.0},
},
},
&oproto.ValueStream{
Variable: variable.NewFromString("/test{host=b,other=y}").AsProto(),
Value: []*oproto.Value{
&oproto.Value{Timestamp: 2, DoubleValue: 2.0},
&oproto.Value{Timestamp: 5, DoubleValue: 5.0},
},
},
&oproto.ValueStream{
Variable: variable.NewFromString("/test{host=a,other=z}").AsProto(),
Value: []*oproto.Value{
&oproto.Value{Timestamp: 3, DoubleValue: 3.0},
&oproto.Value{Timestamp: 6, DoubleValue: 6.0},
},
},
}
numSets := 0
for streams := range MergeBy(input, "host") {
stv := variable.NewFromProto(streams[0].Variable)
if stv.Match(variable.NewFromString("/test{host=a}")) {
c.Assert(len(streams), Equals, 2)
outCount, err := checkValueOrder(Merge(streams))
if err != nil {
c.Error(err)
}
c.Check(outCount, Equals, 4)
} else {
c.Check(len(streams), Equals, 1)
outCount, err := checkValueOrder(Merge(streams))
if err != nil {
c.Error(err)
}
c.Check(outCount, Equals, 2)
}
numSets++
}
c.Check(numSets, Equals, 2)
}
示例10: TestSignedRate
func (s *MySuite) TestSignedRate(c *C) {
for input := range s.store.Reader(context.Background(), variable.NewFromString("/test")) {
output := mutations.SignedRate(input)
for _, v := range output.Value {
checkValue(c, v, v.Timestamp, float64(1)/float64(3))
}
}
}
示例11: TestInterpolate
func (s *MySuite) TestInterpolate(c *C) {
for input := range s.store.Reader(context.Background(), variable.NewFromString("/test/offset")) {
output := mutations.Interpolate(300, input)
checkValue(c, output.Value[0], 0, float64(20))
checkValue(c, output.Value[1], 300, float64(121.81818181818181))
checkValue(c, output.Value[2], 600, float64(191.86046511627907))
checkValue(c, output.Value[3], 900, float64(258.37209302325584))
}
}
示例12: TestMeanBy
func (s *MySuite) TestMeanBy(c *C) {
input := []*oproto.ValueStream{
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=a,job=foo,other=w}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=a,job=bar,other=x}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=b,job=foo,other=y}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=b,job=bar,other=z}")),
}
output := aggregations.Mean([]string{"host"}, input)
c.Assert(output, Not(IsNil))
c.Assert(len(output), Equals, 2)
// Check that there are two output streams, as there are two distinct hosts
checkHostsAndJobs(c, output, 1, 1, 0, 0)
for _, stream := range output {
c.Assert(len(stream.Value), Equals, 11)
if stream.Variable.Label["host"] == "a" {
checkValue(c, stream.Value[0], 60*0, float64((20*1+20*1)/2))
checkValue(c, stream.Value[1], 60*1, float64((20*2+20*2)/2))
checkValue(c, stream.Value[2], 60*2, float64((20*3+20*3)/2))
checkValue(c, stream.Value[3], 60*3, float64((20*4+20*4)/2))
checkValue(c, stream.Value[4], 60*4, float64((20*5+20*5)/2))
checkValue(c, stream.Value[5], 60*5, float64((20*6+20*6)/2))
checkValue(c, stream.Value[6], 60*6, float64((20*7+20*7)/2))
checkValue(c, stream.Value[7], 60*7, float64((20*8+20*8)/2))
checkValue(c, stream.Value[8], 60*8, float64((20*9+20*9)/2))
checkValue(c, stream.Value[9], 60*9, float64((20*10+20*10)/2))
checkValue(c, stream.Value[10], 60*10, float64((20*11+20*11)/2))
} else if stream.Variable.Label["host"] == "b" {
checkValue(c, stream.Value[0], 60*0, float64((40*1+40*1)/2))
checkValue(c, stream.Value[1], 60*1, float64((40*2+40*2)/2))
checkValue(c, stream.Value[2], 60*2, float64((40*3+40*3)/2))
checkValue(c, stream.Value[3], 60*3, float64((40*4+40*4)/2))
checkValue(c, stream.Value[4], 60*4, float64((40*5+40*5)/2))
checkValue(c, stream.Value[5], 60*5, float64((40*6+40*6)/2))
checkValue(c, stream.Value[6], 60*6, float64((40*7+40*7)/2))
checkValue(c, stream.Value[7], 60*7, float64((40*8+40*8)/2))
checkValue(c, stream.Value[8], 60*8, float64((40*9+40*9)/2))
checkValue(c, stream.Value[9], 60*9, float64((40*10+40*10)/2))
checkValue(c, stream.Value[10], 60*10, float64((40*11+40*11)/2))
} else {
c.Fail()
}
}
}
示例13: TestMin
func (s *MySuite) TestMin(c *C) {
for input := range s.store.Reader(context.Background(), variable.NewFromString("/test")) {
output := mutations.Min(120, input)
checkValue(c, output.Value[0], 0, 20*1)
checkValue(c, output.Value[1], 120, 20*3)
checkValue(c, output.Value[2], 240, 20*5)
checkValue(c, output.Value[3], 360, 20*7)
checkValue(c, output.Value[4], 480, 20*9)
}
}
示例14: TestMax
func (s *MySuite) TestMax(c *C) {
for input := range s.store.Reader(context.Background(), variable.NewFromString("/test")) {
output := mutations.Max(120, input)
checkValue(c, output.Value[0], 60*2, 20*3)
checkValue(c, output.Value[1], 60*4, 20*5)
checkValue(c, output.Value[2], 60*6, 20*7)
checkValue(c, output.Value[3], 60*8, 20*9)
checkValue(c, output.Value[4], 60*10, 20*11)
}
}
示例15: TestMeanByJob
func (s *MySuite) TestMeanByJob(c *C) {
input := []*oproto.ValueStream{
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=a,job=foo,other=w}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=a,job=bar,other=x}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=b,job=foo,other=y}")),
<-s.store.Reader(context.Background(), variable.NewFromString("/test{host=b,job=bar,other=z}")),
}
output := aggregations.Mean([]string{"job"}, input)
c.Assert(output, Not(IsNil))
c.Assert(len(output), Equals, 2)
// Check that there are two output streams, as there are two distinct hosts
checkHostsAndJobs(c, output, 0, 0, 1, 1)
// First stream is job=foo
stream := output[0]
c.Assert(len(stream.Value), Equals, 11)
checkValue(c, stream.Value[0], 60*0, float64((20*1+40*1)/2))
checkValue(c, stream.Value[1], 60*1, float64((20*2+40*2)/2))
checkValue(c, stream.Value[2], 60*2, float64((20*3+40*3)/2))
checkValue(c, stream.Value[3], 60*3, float64((20*4+40*4)/2))
checkValue(c, stream.Value[4], 60*4, float64((20*5+40*5)/2))
checkValue(c, stream.Value[5], 60*5, float64((20*6+40*6)/2))
checkValue(c, stream.Value[6], 60*6, float64((20*7+40*7)/2))
checkValue(c, stream.Value[7], 60*7, float64((20*8+40*8)/2))
checkValue(c, stream.Value[8], 60*8, float64((20*9+40*9)/2))
checkValue(c, stream.Value[9], 60*9, float64((20*10+40*10)/2))
checkValue(c, stream.Value[10], 60*10, float64((20*11+40*11)/2))
// Second stream is job=bar
stream = output[1]
c.Assert(len(stream.Value), Equals, 11)
checkValue(c, stream.Value[0], 60*0, float64((20*1+40*1)/2))
checkValue(c, stream.Value[1], 60*1, float64((20*2+40*2)/2))
checkValue(c, stream.Value[2], 60*2, float64((20*3+40*3)/2))
checkValue(c, stream.Value[3], 60*3, float64((20*4+40*4)/2))
checkValue(c, stream.Value[4], 60*4, float64((20*5+40*5)/2))
checkValue(c, stream.Value[5], 60*5, float64((20*6+40*6)/2))
checkValue(c, stream.Value[6], 60*6, float64((20*7+40*7)/2))
checkValue(c, stream.Value[7], 60*7, float64((20*8+40*8)/2))
checkValue(c, stream.Value[8], 60*8, float64((20*9+40*9)/2))
checkValue(c, stream.Value[9], 60*9, float64((20*10+40*10)/2))
checkValue(c, stream.Value[10], 60*10, float64((20*11+40*11)/2))
}