Exemple #1
0
// Test that most specific template is chosen
func TestApplyTemplateSpecific(t *testing.T) {
	o := graphite.Options{
		Separator: "_",
		Templates: []string{
			"current.* measurement.measurement",
			"current.*.* measurement.measurement.service",
		},
	}
	p, err := graphite.NewParserWithOptions(o)
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	measurement, tags, _, _ := p.ApplyTemplate("current.users.facebook")
	if measurement != "current_users" {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s",
			measurement, "current_users")
	}
	service, ok := tags["service"]
	if !ok {
		t.Error("Expected for template to apply a 'service' tag, but not found")
	}
	if service != "facebook" {
		t.Errorf("Expected service='facebook' tag, got service='%s'", service)
	}
}
Exemple #2
0
// parseName parses the given bucket name with the list of bucket maps in the
// config file. If there is a match, it will parse the name of the metric and
// map of tags.
// Return values are (<name>, <tags>)
func (s *Statsd) parseName(bucket string) (string, map[string]string) {
	tags := make(map[string]string)

	bucketparts := strings.Split(bucket, ",")
	// Parse out any tags in the bucket
	if len(bucketparts) > 1 {
		for _, btag := range bucketparts[1:] {
			k, v := parseKeyValue(btag)
			if k != "" {
				tags[k] = v
			}
		}
	}

	o := graphite.Options{
		Separator:   "_",
		Templates:   s.Templates,
		DefaultTags: tags,
	}

	name := bucketparts[0]
	p, err := graphite.NewParserWithOptions(o)
	if err == nil {
		name, tags, _, _ = p.ApplyTemplate(name)
	}
	if s.ConvertNames {
		name = strings.Replace(name, ".", "_", -1)
		name = strings.Replace(name, "-", "__", -1)
	}

	return name, tags
}
Exemple #3
0
func TestApplyTemplateFieldError(t *testing.T) {
	o := graphite.Options{
		Separator: "_",
		Templates: []string{"current.* measurement.field.field"},
	}
	p, err := graphite.NewParserWithOptions(o)
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	_, _, _, err = p.ApplyTemplate("current.users.logged_in")
	if err == nil {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s", err,
			"'field' can only be used once in each template: current.users.logged_in")
	}
}
Exemple #4
0
// Test basic functionality of ApplyTemplate
func TestApplyTemplateNoMatch(t *testing.T) {
	o := graphite.Options{
		Separator: "_",
		Templates: []string{"foo.bar measurement.measurement"},
	}
	p, err := graphite.NewParserWithOptions(o)
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	measurement, _, _, _ := p.ApplyTemplate("current.users")
	if measurement != "current.users" {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s",
			measurement, "current.users")
	}
}
Exemple #5
0
// Test that most specific template is N/A
func TestApplyTemplateSpecificIsNA(t *testing.T) {
	o := graphite.Options{
		Separator: "_",
		Templates: []string{
			"current.* measurement.service",
			"current.*.*.test measurement.measurement.service",
		},
	}
	p, err := graphite.NewParserWithOptions(o)
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	measurement, _, _, _ := p.ApplyTemplate("current.users.facebook")
	if measurement != "current" {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s",
			measurement, "current")
	}
}
Exemple #6
0
func TestApplyTemplateField(t *testing.T) {
	o := graphite.Options{
		Separator: "_",
		Templates: []string{"current.* measurement.measurement.field"},
	}
	p, err := graphite.NewParserWithOptions(o)
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	measurement, _, field, err := p.ApplyTemplate("current.users.logged_in")

	if measurement != "current_users" {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s",
			measurement, "current_users")
	}

	if field != "logged_in" {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s",
			field, "logged_in")
	}
}
Exemple #7
0
func TestFilterMatchMultipleMeasurementSeparator(t *testing.T) {
	p, err := graphite.NewParserWithOptions(graphite.Options{
		Templates: []string{"servers.localhost .host.measurement.measurement*"},
		Separator: "_",
	})
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	exp := models.MustNewPoint("cpu_cpu_load_10",
		models.NewTags(map[string]string{"host": "localhost"}),
		models.Fields{"value": float64(11)},
		time.Unix(1435077219, 0))

	pt, err := p.Parse("servers.localhost.cpu.cpu_load.10 11 1435077219")
	if err != nil {
		t.Fatalf("parse error: %v", err)
	}

	if exp.String() != pt.String() {
		t.Errorf("parse mismatch: got %v, exp %v", pt.String(), exp.String())
	}
}
Exemple #8
0
func TestApplyTemplateTags(t *testing.T) {
	o := graphite.Options{
		Separator: "_",
		Templates: []string{"current.* measurement.measurement region=us-west"},
	}
	p, err := graphite.NewParserWithOptions(o)
	if err != nil {
		t.Fatalf("unexpected error creating parser, got %v", err)
	}

	measurement, tags, _, _ := p.ApplyTemplate("current.users")
	if measurement != "current_users" {
		t.Errorf("Parser.ApplyTemplate unexpected result. got %s, exp %s",
			measurement, "current_users")
	}

	region, ok := tags["region"]
	if !ok {
		t.Error("Expected for template to apply a 'region' tag, but not found")
	}
	if region != "us-west" {
		t.Errorf("Expected region='us-west' tag, got region='%s'", region)
	}
}