func (s *MySuite) TestVariableInsideRelativeRange(c *C) { v := NewFromString("/test") // From 40 to 50 seconds ago v.MinTimestamp = -50000 v.MaxTimestamp = -40000 c.Check(v.TimestampInsideRange(openinstrument.NowMs()-60000), Equals, false) c.Check(v.TimestampInsideRange(openinstrument.NowMs()-45000), Equals, true) c.Check(v.TimestampInsideRange(openinstrument.NowMs()-20000), Equals, false) }
// UpdateThisState makes a change to the state of the current server and broadcasts it to the cluster. func (s *LocalConfigStore) UpdateThisState(ctx context.Context, state oproto.ClusterMember_State) error { m, err := s.GetServer(ctx, s.taskName) if err != nil { m = oproto.ClusterMember{ Name: s.taskName, LastUpdated: openinstrument.NowMs(), } } m.State = state m.LastUpdated = openinstrument.NowMs() return s.UpdateServer(ctx, m) }
func (v *Variable) TimestampInsideRange(timestamp uint64) bool { minTimestamp := v.MinTimestamp if minTimestamp < 0 { minTimestamp = int64(openinstrument.NowMs()) + minTimestamp } maxTimestamp := v.MaxTimestamp if maxTimestamp < 0 { maxTimestamp = int64(openinstrument.NowMs()) + maxTimestamp } if maxTimestamp != 0 && int64(timestamp) > maxTimestamp { // Too new return false } if int64(timestamp) < minTimestamp { // Too old return false } return true }
func runAddLoadtest(ctx context.Context, conn client.Client) { log.Println("Running ADD load test") in, out, err := conn.Add(ctx) if err != nil { log.Printf("Error starting Add RPC: %s", err) return } defer close(in) sem := make(semaphore, 1000) var sent, received int go func() { tick := time.Tick(1 * time.Second) for { select { case <-out: received++ sem.V(1) case <-tick: log.Printf("Sent %d, received %d in the last second, latency %0.02fms", sent, received, 1.0/float64(received)*1000.0) received = 0 sent = 0 } } }() for { select { case <-ctx.Done(): return default: } sem.P(1) v := variable.NewFromString("/test/var1{host=rage}").AsProto() request := &oproto.AddRequest{ Stream: []*oproto.ValueStream{ { Variable: v, Value: []*oproto.Value{ { Timestamp: openinstrument.NowMs(), DoubleValue: 1.0, }, }, }, }, } in <- request sent++ } log.Println("Complete") }
func runSlowAddLoadtest(ctx context.Context) { log.Println("Running ADD load test") var sent, received int go func() { tick := time.Tick(1 * time.Second) for { select { case <-tick: log.Printf("Sent %d, received %d in the last second, latency %0.02fms", sent, received, 1.0/float64(received)*1000.0) received = 0 sent = 0 } } }() for { conn, err := client.NewRpcClient(ctx, *connectAddress) if err != nil { log.Fatalf("Error connecting to %s: %s", *connectAddress, err) } in, out, err := conn.Add(ctx) if err != nil { log.Printf("Error starting Add RPC: %s", err) return } v := variable.NewFromString("/test/var1{host=rage}").AsProto() request := &oproto.AddRequest{ Stream: []*oproto.ValueStream{ { Variable: v, Value: []*oproto.Value{ { Timestamp: openinstrument.NowMs(), DoubleValue: 1.0, }, }, }, }, } in <- request close(in) sent++ <-out received++ conn.Close() } log.Println("Complete") }
func (s *MySuite) TestAgeKeepPolicy(c *C) { return // TODO(dparrish): Re-enable this when it works policyTxt := ` interval: 1 policy { comment: "Throw everything away" variable { name: "/test/foo*" min_timestamp: -91 max_timestamp: -75 } policy: KEEP } # Implicit 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{}, } now := openinstrument.NowMs() for i := 1; i <= 10; i++ { input.Value = append(input.Value, &oproto.Value{ Timestamp: now - uint64(98-3*i), EndTimestamp: now - uint64(100-3*i), DoubleValue: 1.1, }) } output := policy.Apply(input) count := 0 for _, value := range output.Value { age := now - value.Timestamp if age < 75 || age > 91 { log.Printf("Got value outside expected age (%d)", age) c.Fail() continue } count++ } c.Check(count, Equals, 5) }
func findFirstMatchingPolicy(value *oproto.Value, policies []*oproto.RetentionPolicyItem) *oproto.RetentionPolicyItem { now := openinstrument.NowMs() valueStartAge := now - value.Timestamp if value.EndTimestamp == 0 && value.Timestamp != 0 { value.EndTimestamp = value.Timestamp } valueEndAge := now - value.EndTimestamp for _, item := range policies { if len(item.Variable) == 0 { // No variables supplied, this matches everything return item } for _, i := range item.Variable { // Look for policies that match the variable age v := variable.NewFromProto(i) if v.TimestampInsideRange(valueStartAge) || v.TimestampInsideRange(valueEndAge) { return item } } } return nil }
func (s *MySuite) TestValueStreamWriterMemoryLeak(c *C) { filename := filepath.Join(c.MkDir(), "protofile_testvar.dat") vs := &oproto.ValueStream{ Variable: &oproto.StreamVariable{Name: "/test/bar"}, Value: []*oproto.Value{}, } for i := 0; i < 10; i++ { vs.Value = append(vs.Value, &oproto.Value{Timestamp: uint64(openinstrument.NowMs()), DoubleValue: 1.1}) } // Write a temporary file containing two value streams file, err := Write(filename) c.Assert(err, IsNil) defer file.Close() writer, done := file.ValueStreamWriter(10) wroteStreams := 0 for j := 0; j < 1000; j++ { writer <- vs wroteStreams++ } close(writer) <-done file, err = Read(filename) c.Assert(err, IsNil) defer file.Close() reader := file.ValueStreamReader(context.Background(), 10) readStreams := 0 for range reader { readStreams++ } c.Assert(readStreams, Equals, wroteStreams) }
func main() { log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) flag.Parse() ctx := context.Background() conn, err := client.NewRpcClient(ctx, *connectAddress) if err != nil { log.Fatalf("Error connecting to %s: %s", *connectAddress, err) } defer conn.Close() in, out, err := conn.Add(ctx) if err != nil { log.Fatal(err) } snmp := gosnmp.Default for _, host := range []string{"192.168.1.1"} { snmp.Target = host snmp.Community = "public" if err := snmp.Connect(); err != nil { log.Fatal(err) } defer snmp.Conn.Close() // VDSL-LINE-MIB::vdslPhysCurrLineRate.1 = Gauge32: 20265 kbps // VDSL-LINE-MIB::vdslPhysCurrLineRate.2 = Gauge32: 1315 kbps oids := []string{"1.3.6.1.2.1.10.97.1.1.2.1.10.1", "1.3.6.1.2.1.10.97.1.1.2.1.10.2"} result, err := snmp.Get(oids) if err != nil { log.Fatal(err) } request := &oproto.AddRequest{ Stream: []*oproto.ValueStream{}, } for _, snmpVar := range result.Variables { switch snmpVar.Name { case ".1.3.6.1.2.1.10.97.1.1.2.1.10.1": request.Stream = append(request.Stream, &oproto.ValueStream{ Variable: variable.NewFromString(fmt.Sprintf("/network/adsl/downstream_rate{host=%s,unit=bps}", host)).AsProto(), Value: []*oproto.Value{ { DoubleValue: float64(snmpVar.Value.(uint) * 1000), Timestamp: openinstrument.NowMs(), }, }, }) case ".1.3.6.1.2.1.10.97.1.1.2.1.10.2": request.Stream = append(request.Stream, &oproto.ValueStream{ Variable: variable.NewFromString(fmt.Sprintf("/network/adsl/upstream_rate{host=%s,unit=bps}", host)).AsProto(), Value: []*oproto.Value{ { DoubleValue: float64(snmpVar.Value.(uint) * 1000), Timestamp: openinstrument.NowMs(), }, }, }) } } in <- request } close(in) for response := range out { if !response.Success { fmt.Println(openinstrument.ProtoText(response)) } } }
func main() { log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) flag.Parse() ctx := context.Background() conn, err := client.NewRpcClient(ctx, *connectAddress) if err != nil { log.Fatalf("Error connecting to %s: %s", *connectAddress, err) } defer conn.Close() re, err := regexp.Compile("^([^\\s]+) (.+)$") if err != nil { log.Fatalf("Error compiling regex: %s", err) } timestamp := openinstrument.NowMs() // Read from stdin c := make(chan *oproto.ValueStream) go func() { defer close(c) scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { matches := re.FindStringSubmatch(scanner.Text()) if len(matches) != 3 { log.Printf("Invalid input line: %s", scanner.Text()) continue } v := variable.NewFromString(matches[1]) value := &oproto.Value{Timestamp: timestamp} f, err := strconv.ParseFloat(matches[2], 64) if err != nil { value.StringValue = matches[2] } else { value.DoubleValue = f } c <- &oproto.ValueStream{ Variable: v.AsProto(), Value: []*oproto.Value{value}, } } }() if *readTimeout != 0 { ctx, _ = context.WithTimeout(ctx, *readTimeout) } in, out, err := conn.Add(ctx) if err != nil { log.Fatal(err) } func() { defer close(in) interval := time.Tick(1 * time.Second) request := &oproto.AddRequest{} for { select { case stream := <-c: if stream == nil { in <- request return } request.Stream = append(request.Stream, stream) case <-interval: in <- request request = &oproto.AddRequest{} timestamp = openinstrument.NowMs() case response := <-out: if response == nil { return } log.Printf("%s", response) } } in <- request log.Printf("done") }() for response := range out { log.Printf("%s", response) } }