func (l *LevelDBMetricPersistence) GetMetricForFingerprint(f *model.Fingerprint) (m *model.Metric, err error) { begin := time.Now() defer func() { duration := time.Now().Sub(begin) recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: getMetricForFingerprint, result: success}, map[string]string{operation: getMetricForFingerprint, result: failure}) }() raw, err := l.fingerprintToMetrics.Get(coding.NewProtocolBufferEncoder(model.FingerprintToDTO(f))) if err != nil { return } unmarshaled := &dto.Metric{} err = proto.Unmarshal(raw, unmarshaled) if err != nil { return } m = &model.Metric{} for _, v := range unmarshaled.LabelPair { (*m)[model.LabelName(*v.Name)] = model.LabelValue(*v.Value) } return }
func TestGetFingerprintsForLabelSetUsesAnd(t *testing.T) { temporaryDirectory, _ := ioutil.TempDir("", "test_get_fingerprints_for_label_set_uses_and") defer func() { err := os.RemoveAll(temporaryDirectory) if err != nil { t.Errorf("could not remove temporary directory: %f", err) } }() persistence, _ := NewLevelDBMetricPersistence(temporaryDirectory) defer persistence.Close() metrics := []map[string]string{ {"name": "request_metrics_latency_equal_tallying_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"}, {"name": "requests_metrics_latency_equal_accumulating_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"}, {"name": "requests_metrics_latency_logarithmic_accumulating_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"}, {"name": "requests_metrics_latency_logarithmic_tallying_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"}, {"name": "targets_healthy_scrape_latency_ms", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"}, } for _, metric := range metrics { m := model.Metric{} for k, v := range metric { m[model.LabelName(k)] = model.LabelValue(v) } err := persistence.AppendSample(&model.Sample{ Value: model.SampleValue(0.0), Timestamp: time.Now(), Metric: m, }) if err != nil { t.Errorf("could not create base sample: %s", err) } } labelSet := model.LabelSet{ "name": "targets_healthy_scrape_latency_ms", "percentile": "0.010000", } fingerprints, err := persistence.GetFingerprintsForLabelSet(&labelSet) if err != nil { t.Errorf("could not get labels: %s", err) } if len(fingerprints) != 1 { t.Errorf("did not get a single metric as is expected, got %s", fingerprints) } }
func (l *LevelDBMetricPersistence) GetAllMetrics() ([]model.LabelSet, error) { if getAll, getAllError := l.labelSetToFingerprints.GetAll(); getAllError == nil { result := make([]model.LabelSet, 0) fingerprintCollection := &dto.FingerprintCollection{} fingerprints := make(utility.Set) for _, pair := range getAll { if unmarshalError := proto.Unmarshal(pair.Right, fingerprintCollection); unmarshalError == nil { for _, member := range fingerprintCollection.Member { if !fingerprints.Has(*member.Signature) { fingerprints.Add(*member.Signature) fingerprintEncoded := coding.NewProtocolBufferEncoder(member) if labelPairCollectionRaw, labelPairCollectionRawError := l.fingerprintToMetrics.Get(fingerprintEncoded); labelPairCollectionRawError == nil { labelPairCollectionDTO := &dto.LabelSet{} if labelPairCollectionDTOMarshalError := proto.Unmarshal(labelPairCollectionRaw, labelPairCollectionDTO); labelPairCollectionDTOMarshalError == nil { intermediate := make(model.LabelSet, 0) for _, member := range labelPairCollectionDTO.Member { n := model.LabelName(*member.Name) v := model.LabelValue(*member.Value) intermediate[n] = v } result = append(result, intermediate) } else { return nil, labelPairCollectionDTOMarshalError } } else { return nil, labelPairCollectionRawError } } } } else { return nil, unmarshalError } } return result, nil } else { return nil, getAllError } return nil, errors.New("Unknown error encountered when querying metrics.") }
func (rule *Rule) Eval(timestamp *time.Time) ast.Vector { // Get the raw value of the rule expression. vector := rule.EvalRaw(timestamp) // Override the metric name and labels. for _, sample := range vector { sample.Metric["name"] = model.LabelValue(rule.name) for label, value := range rule.labels { if value == "" { delete(sample.Metric, label) } else { sample.Metric[label] = value } } } return vector }
func (m *targetManager) AddTargetsFromConfig(config *config.Config) { for _, job := range config.Jobs { for _, configTargets := range job.Targets { baseLabels := model.LabelSet{ model.LabelName("job"): model.LabelValue(job.Name), } for label, value := range configTargets.Labels { baseLabels[label] = value } interval := job.ScrapeInterval if interval == 0 { interval = config.Global.ScrapeInterval } for _, endpoint := range configTargets.Endpoints { target := NewTarget(endpoint, time.Second*5, interval, baseLabels) m.Add(target) } } } }
func (l *LevelDBMetricPersistence) GetAllLabelPairs() ([]model.LabelSet, error) { if getAll, getAllError := l.labelSetToFingerprints.GetAll(); getAllError == nil { result := make([]model.LabelSet, 0, len(getAll)) labelPairDTO := &dto.LabelPair{} for _, pair := range getAll { if unmarshalError := proto.Unmarshal(pair.Left, labelPairDTO); unmarshalError == nil { n := model.LabelName(*labelPairDTO.Name) v := model.LabelValue(*labelPairDTO.Value) item := model.LabelSet{n: v} result = append(result, item) } else { return nil, unmarshalError } } return result, nil } else { return nil, getAllError } return nil, errors.New("Unknown error encountered when querying label pairs.") }
func (t *target) Scrape(earliest time.Time, results chan Result) (err error) { result := Result{} defer func() { futureState := t.state switch err { case nil: futureState = ALIVE default: futureState = UNREACHABLE } t.scheduler.Reschedule(earliest, futureState) result.Err = err results <- result }() done := make(chan bool) request := func() { ti := time.Now() resp, err := http.Get(t.Address()) if err != nil { return } defer resp.Body.Close() raw, err := ioutil.ReadAll(resp.Body) if err != nil { return } intermediate := make(map[string]interface{}) err = json.Unmarshal(raw, &intermediate) if err != nil { return } baseLabels := model.LabelSet{"instance": model.LabelValue(t.Address())} for baseK, baseV := range t.BaseLabels { baseLabels[baseK] = baseV } for name, v := range intermediate { asMap, ok := v.(map[string]interface{}) if !ok { continue } switch asMap["type"] { case "counter": m := model.Metric{} m["name"] = model.LabelValue(name) asFloat, ok := asMap["value"].(float64) if !ok { continue } s := model.Sample{ Metric: m, Value: model.SampleValue(asFloat), Timestamp: ti, } for baseK, baseV := range baseLabels { m[baseK] = baseV } result.Samples = append(result.Samples, s) case "histogram": values, ok := asMap["value"].(map[string]interface{}) if !ok { continue } for p, pValue := range values { asString, ok := pValue.(string) if !ok { continue } float, err := strconv.ParseFloat(asString, 64) if err != nil { continue } m := model.Metric{} m["name"] = model.LabelValue(name) m["percentile"] = model.LabelValue(p) s := model.Sample{ Metric: m, Value: model.SampleValue(float), Timestamp: ti, } for baseK, baseV := range baseLabels { m[baseK] = baseV } result.Samples = append(result.Samples, s) } } } done <- true } accumulator := func(d time.Duration) { ms := float64(d) / float64(time.Millisecond) labels := map[string]string{address: t.Address(), outcome: success} if err != nil { labels[outcome] = failure } targetOperationLatencies.Add(labels, ms) targetOperations.Increment(labels) } go metrics.InstrumentCall(request, accumulator) select { case <-done: break case <-time.After(t.Deadline): err = fmt.Errorf("Target %s exceeded %s deadline.", t, t.Deadline) } return }
func yyParse(yylex yyLexer) int { var yyn int var yylval yySymType var yyVAL yySymType yyS := make([]yySymType, yyMaxDepth) Nerrs := 0 /* number of errors */ Errflag := 0 /* error recovery flag */ yystate := 0 yychar := -1 yyp := -1 goto yystack ret0: return 0 ret1: return 1 yystack: /* put a state and value onto the stack */ if yyDebug >= 4 { fmt.Printf("char %v in %v\n", yyTokname(yychar), yyStatname(yystate)) } yyp++ if yyp >= len(yyS) { nyys := make([]yySymType, len(yyS)*2) copy(nyys, yyS) yyS = nyys } yyS[yyp] = yyVAL yyS[yyp].yys = yystate yynewstate: yyn = yyPact[yystate] if yyn <= yyFlag { goto yydefault /* simple state */ } if yychar < 0 { yychar = yylex1(yylex, &yylval) } yyn += yychar if yyn < 0 || yyn >= yyLast { goto yydefault } yyn = yyAct[yyn] if yyChk[yyn] == yychar { /* valid shift */ yychar = -1 yyVAL = yylval yystate = yyn if Errflag > 0 { Errflag-- } goto yystack } yydefault: /* default state action */ yyn = yyDef[yystate] if yyn == -2 { if yychar < 0 { yychar = yylex1(yylex, &yylval) } /* look through exception table */ xi := 0 for { if yyExca[xi+0] == -1 && yyExca[xi+1] == yystate { break } xi += 2 } for xi += 2; ; xi += 2 { yyn = yyExca[xi+0] if yyn < 0 || yyn == yychar { break } } yyn = yyExca[xi+1] if yyn < 0 { goto ret0 } } if yyn == 0 { /* error ... attempt to resume parsing */ switch Errflag { case 0: /* brand new error */ yylex.Error("syntax error") Nerrs++ if yyDebug >= 1 { fmt.Printf("%s", yyStatname(yystate)) fmt.Printf("saw %s\n", yyTokname(yychar)) } fallthrough case 1, 2: /* incompletely recovered error ... try again */ Errflag = 3 /* find a state where "error" is a legal shift action */ for yyp >= 0 { yyn = yyPact[yyS[yyp].yys] + yyErrCode if yyn >= 0 && yyn < yyLast { yystate = yyAct[yyn] /* simulate a shift of "error" */ if yyChk[yystate] == yyErrCode { goto yystack } } /* the current p has no shift on "error", pop stack */ if yyDebug >= 2 { fmt.Printf("error recovery pops state %d\n", yyS[yyp].yys) } yyp-- } /* there is no state on the stack with an error shift ... abort */ goto ret1 case 3: /* no shift yet; clobber input char */ if yyDebug >= 2 { fmt.Printf("error recovery discards %s\n", yyTokname(yychar)) } if yychar == yyEofCode { goto ret1 } yychar = -1 goto yynewstate /* try again in the same state */ } } /* reduction by production yyn */ if yyDebug >= 2 { fmt.Printf("reduce %v in:\n\t%v\n", yyn, yyStatname(yystate)) } yynt := yyn yypt := yyp _ = yypt // guard against "declared and not used" yyp -= yyR2[yyn] yyVAL = yyS[yyp+1] /* consult goto table to find next state */ yyn = yyR1[yyn] yyg := yyPgo[yyn] yyj := yyg + yyS[yyp].yys + 1 if yyj >= yyLast { yystate = yyAct[yyg] } else { yystate = yyAct[yyj] if yyChk[yystate] != -yyn { yystate = yyAct[yyg] } } // dummy call; replaced with literal code switch yynt { case 4: //line parser.y:32 { PopJob() } case 7: //line parser.y:40 { parsedConfig.Global.SetOption(yyS[yypt-2].str, yyS[yypt-0].str) } case 8: //line parser.y:42 { parsedConfig.Global.SetLabels(yyS[yypt-0].labelSet) } case 9: //line parser.y:44 { parsedConfig.Global.AddRuleFiles(yyS[yypt-0].stringSlice) } case 10: //line parser.y:48 { yyVAL.labelSet = yyS[yypt-1].labelSet } case 11: //line parser.y:50 { yyVAL.labelSet = model.LabelSet{} } case 12: //line parser.y:54 { yyVAL.labelSet = yyS[yypt-0].labelSet } case 13: //line parser.y:56 { for k, v := range yyS[yypt-0].labelSet { yyVAL.labelSet[k] = v } } case 14: //line parser.y:60 { yyVAL.labelSet = model.LabelSet{model.LabelName(yyS[yypt-2].str): model.LabelValue(yyS[yypt-0].str)} } case 15: //line parser.y:64 { yyVAL.stringSlice = yyS[yypt-0].stringSlice } case 18: //line parser.y:72 { PushJobOption(yyS[yypt-2].str, yyS[yypt-0].str) } case 19: //line parser.y:74 { PushJobTargets() } case 22: //line parser.y:82 { PushTargetEndpoints(yyS[yypt-0].stringSlice) } case 23: //line parser.y:84 { PushTargetLabels(yyS[yypt-0].labelSet) } case 24: //line parser.y:88 { yyVAL.stringSlice = yyS[yypt-0].stringSlice } case 25: //line parser.y:92 { yyVAL.stringSlice = yyS[yypt-1].stringSlice } case 26: //line parser.y:94 { yyVAL.stringSlice = []string{} } case 27: //line parser.y:98 { yyVAL.stringSlice = []string{yyS[yypt-0].str} } case 28: //line parser.y:100 { yyVAL.stringSlice = append(yyVAL.stringSlice, yyS[yypt-0].str) } } goto yystack /* stack new state and value */ }
func yyParse(yylex yyLexer) int { var yyn int var yylval yySymType var yyVAL yySymType yyS := make([]yySymType, yyMaxDepth) Nerrs := 0 /* number of errors */ Errflag := 0 /* error recovery flag */ yystate := 0 yychar := -1 yyp := -1 goto yystack ret0: return 0 ret1: return 1 yystack: /* put a state and value onto the stack */ if yyDebug >= 4 { fmt.Printf("char %v in %v\n", yyTokname(yychar), yyStatname(yystate)) } yyp++ if yyp >= len(yyS) { nyys := make([]yySymType, len(yyS)*2) copy(nyys, yyS) yyS = nyys } yyS[yyp] = yyVAL yyS[yyp].yys = yystate yynewstate: yyn = yyPact[yystate] if yyn <= yyFlag { goto yydefault /* simple state */ } if yychar < 0 { yychar = yylex1(yylex, &yylval) } yyn += yychar if yyn < 0 || yyn >= yyLast { goto yydefault } yyn = yyAct[yyn] if yyChk[yyn] == yychar { /* valid shift */ yychar = -1 yyVAL = yylval yystate = yyn if Errflag > 0 { Errflag-- } goto yystack } yydefault: /* default state action */ yyn = yyDef[yystate] if yyn == -2 { if yychar < 0 { yychar = yylex1(yylex, &yylval) } /* look through exception table */ xi := 0 for { if yyExca[xi+0] == -1 && yyExca[xi+1] == yystate { break } xi += 2 } for xi += 2; ; xi += 2 { yyn = yyExca[xi+0] if yyn < 0 || yyn == yychar { break } } yyn = yyExca[xi+1] if yyn < 0 { goto ret0 } } if yyn == 0 { /* error ... attempt to resume parsing */ switch Errflag { case 0: /* brand new error */ yylex.Error("syntax error") Nerrs++ if yyDebug >= 1 { fmt.Printf("%s", yyStatname(yystate)) fmt.Printf("saw %s\n", yyTokname(yychar)) } fallthrough case 1, 2: /* incompletely recovered error ... try again */ Errflag = 3 /* find a state where "error" is a legal shift action */ for yyp >= 0 { yyn = yyPact[yyS[yyp].yys] + yyErrCode if yyn >= 0 && yyn < yyLast { yystate = yyAct[yyn] /* simulate a shift of "error" */ if yyChk[yystate] == yyErrCode { goto yystack } } /* the current p has no shift on "error", pop stack */ if yyDebug >= 2 { fmt.Printf("error recovery pops state %d\n", yyS[yyp].yys) } yyp-- } /* there is no state on the stack with an error shift ... abort */ goto ret1 case 3: /* no shift yet; clobber input char */ if yyDebug >= 2 { fmt.Printf("error recovery discards %s\n", yyTokname(yychar)) } if yychar == yyEofCode { goto ret1 } yychar = -1 goto yynewstate /* try again in the same state */ } } /* reduction by production yyn */ if yyDebug >= 2 { fmt.Printf("reduce %v in:\n\t%v\n", yyn, yyStatname(yystate)) } yynt := yyn yypt := yyp _ = yypt // guard against "declared and not used" yyp -= yyR2[yyn] yyVAL = yyS[yyp+1] /* consult goto table to find next state */ yyn = yyR1[yyn] yyg := yyPgo[yyn] yyj := yyg + yyS[yyp].yys + 1 if yyj >= yyLast { yystate = yyAct[yyg] } else { yystate = yyAct[yyj] if yyChk[yystate] != -yyn { yystate = yyAct[yyg] } } // dummy call; replaced with literal code switch yynt { case 5: //line parser.y:52 { yylex.(*RulesLexer).parsedExpr = yyS[yypt-0].ruleNode } case 6: //line parser.y:56 { rule, err := CreateRule(yyS[yypt-3].str, yyS[yypt-2].labelSet, yyS[yypt-0].ruleNode, yyS[yypt-4].boolean) if err != nil { yylex.Error(err.Error()) return 1 } yylex.(*RulesLexer).parsedRules = append(yylex.(*RulesLexer).parsedRules, rule) } case 7: //line parser.y:64 { yyVAL.boolean = false } case 8: //line parser.y:66 { yyVAL.boolean = true } case 9: //line parser.y:70 { yyVAL.labelSet = model.LabelSet{} } case 10: //line parser.y:72 { yyVAL.labelSet = yyS[yypt-1].labelSet } case 11: //line parser.y:74 { yyVAL.labelSet = model.LabelSet{} } case 12: //line parser.y:77 { yyVAL.labelSet = yyS[yypt-0].labelSet } case 13: //line parser.y:79 { for k, v := range yyS[yypt-0].labelSet { yyVAL.labelSet[k] = v } } case 14: //line parser.y:83 { yyVAL.labelSet = model.LabelSet{model.LabelName(yyS[yypt-2].str): model.LabelValue(yyS[yypt-0].str)} } case 15: //line parser.y:88 { yyVAL.ruleNode = yyS[yypt-1].ruleNode } case 16: //line parser.y:90 { yyS[yypt-0].labelSet["name"] = model.LabelValue(yyS[yypt-1].str) yyVAL.ruleNode = ast.NewVectorLiteral(yyS[yypt-0].labelSet) } case 17: //line parser.y:92 { var err error yyVAL.ruleNode, err = NewFunctionCall(yyS[yypt-3].str, yyS[yypt-1].ruleNodeSlice) if err != nil { yylex.Error(err.Error()) return 1 } } case 18: //line parser.y:98 { var err error yyVAL.ruleNode, err = NewFunctionCall(yyS[yypt-2].str, []ast.Node{}) if err != nil { yylex.Error(err.Error()) return 1 } } case 19: //line parser.y:104 { var err error yyVAL.ruleNode, err = NewMatrix(yyS[yypt-3].ruleNode, yyS[yypt-1].str) if err != nil { yylex.Error(err.Error()) return 1 } } case 20: //line parser.y:110 { var err error yyVAL.ruleNode, err = NewVectorAggregation(yyS[yypt-4].str, yyS[yypt-2].ruleNode, yyS[yypt-0].labelNameSlice) if err != nil { yylex.Error(err.Error()) return 1 } } case 21: //line parser.y:118 { var err error yyVAL.ruleNode, err = NewArithExpr(yyS[yypt-1].str, yyS[yypt-2].ruleNode, yyS[yypt-0].ruleNode) if err != nil { yylex.Error(err.Error()) return 1 } } case 22: //line parser.y:124 { var err error yyVAL.ruleNode, err = NewArithExpr(yyS[yypt-1].str, yyS[yypt-2].ruleNode, yyS[yypt-0].ruleNode) if err != nil { yylex.Error(err.Error()) return 1 } } case 23: //line parser.y:130 { var err error yyVAL.ruleNode, err = NewArithExpr(yyS[yypt-1].str, yyS[yypt-2].ruleNode, yyS[yypt-0].ruleNode) if err != nil { yylex.Error(err.Error()) return 1 } } case 24: //line parser.y:136 { yyVAL.ruleNode = ast.NewScalarLiteral(yyS[yypt-0].num) } case 25: //line parser.y:140 { yyVAL.labelNameSlice = []model.LabelName{} } case 26: //line parser.y:142 { yyVAL.labelNameSlice = yyS[yypt-1].labelNameSlice } case 27: //line parser.y:146 { yyVAL.labelNameSlice = []model.LabelName{model.LabelName(yyS[yypt-0].str)} } case 28: //line parser.y:148 { yyVAL.labelNameSlice = append(yyVAL.labelNameSlice, model.LabelName(yyS[yypt-0].str)) } case 29: //line parser.y:152 { yyVAL.ruleNodeSlice = []ast.Node{yyS[yypt-0].ruleNode} } case 30: //line parser.y:154 { yyVAL.ruleNodeSlice = append(yyVAL.ruleNodeSlice, yyS[yypt-0].ruleNode) } case 31: //line parser.y:158 { yyVAL.ruleNode = yyS[yypt-0].ruleNode } case 32: //line parser.y:160 { yyVAL.ruleNode = ast.NewStringLiteral(yyS[yypt-0].str) } } goto yystack /* stack new state and value */ }