Repository: VictoriaMetrics/VictoriaMetrics
Pull request #1389 (2021-06-17 12:03:12)

fixes DO service discovery labels
adds test for digitalocean sd

must fix https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1367

codecov[bot]: # [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) Report
> Merging [#1389](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) (0423b1f) into [master](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/commit/dcbc22552f0dcfa3075982eabeda44b0a28fd428?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) (dcbc225) will **increase** coverage by `0.12%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389/graphs/tree.svg?width=650&height=150&src=pr&token=YND8dYVcXn&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics)](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics)

```diff
@@            Coverage Diff             @@
##           master    #1389      +/-   ##
==========================================
+ Coverage   60.49%   60.61%   +0.12%     
==========================================
  Files         224      224              
  Lines       26488    26488              
==========================================
+ Hits        16025    16057      +32     
+ Misses       8973     8941      -32     
  Partials     1490     1490              
```


| [Impacted Files](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) | Coverage Δ | |
|---|---|---|
| [.../promscrape/discovery/digitalocean/digitalocean.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics#diff-bGliL3Byb21zY3JhcGUvZGlzY292ZXJ5L2RpZ2l0YWxvY2Vhbi9kaWdpdGFsb2NlYW4uZ28=) | `73.07% <100.00%> (+65.38%)` | :arrow_up: |
| [app/vmalert/remotewrite/remotewrite.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics#diff-YXBwL3ZtYWxlcnQvcmVtb3Rld3JpdGUvcmVtb3Rld3JpdGUuZ28=) | `58.87% <0.00%> (-3.74%)` | :arrow_down: |
| [app/vmselect/promql/aggr\_incremental.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics#diff-YXBwL3Ztc2VsZWN0L3Byb21xbC9hZ2dyX2luY3JlbWVudGFsLmdv) | `86.43% <0.00%> (ø)` | |
| [lib/mergeset/table.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics#diff-bGliL21lcmdlc2V0L3RhYmxlLmdv) | `66.32% <0.00%> (+0.29%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics). Last update [dcbc225...0423b1f](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1389?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics).


Patch:
diff --git a/lib/promscrape/discovery/digitalocean/digitalocean.go b/lib/promscrape/discovery/digitalocean/digitalocean.go
index e498f201a..3c68c2b72 100644
--- a/lib/promscrape/discovery/digitalocean/digitalocean.go
+++ b/lib/promscrape/discovery/digitalocean/digitalocean.go
@@ -136,10 +136,10 @@ func addDropletLabels(droplets []droplet, defaultPort int) []map[string]string {
 		}
 		if len(droplet.Features) > 0 {
 			features := fmt.Sprintf(",%s,", strings.Join(droplet.Features, ","))
-			m["__meta_digitalocean_vpc"] = features
+			m["__meta_digitalocean_features"] = features
 		}
 		if len(droplet.Tags) > 0 {
-			tags := fmt.Sprintf(",%s,", strings.Join(droplet.Features, ","))
+			tags := fmt.Sprintf(",%s,", strings.Join(droplet.Tags, ","))
 			m["__meta_digitalocean_tags"] = tags
 		}
 		ms = append(ms, m)


Test patch:
diff --git a/lib/promscrape/discovery/digitalocean/digitalocean_test.go b/lib/promscrape/discovery/digitalocean/digitalocean_test.go
new file mode 100644
index 000000000..ecb177fbf
--- /dev/null
+++ b/lib/promscrape/discovery/digitalocean/digitalocean_test.go
@@ -0,0 +1,98 @@
+package digitalocean
+
+import (
+	"reflect"
+	"testing"
+
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
+)
+
+func Test_addDropletLabels(t *testing.T) {
+	type args struct {
+		droplets    []droplet
+		defaultPort int
+	}
+	tests := []struct {
+		name string
+		args args
+		want [][]prompbmarshal.Label
+	}{
+		{
+			name: "base labels add test",
+			args: args{
+				droplets: []droplet{
+					{
+						ID:     15,
+						Tags:   []string{"private", "test"},
+						Status: "active",
+						Name:   "ubuntu-1",
+						Region: struct {
+							Slug string `json:"slug"`
+						}(struct{ Slug string }{Slug: "do"}),
+						Features: []string{"feature-1", "feature-2"},
+						SizeSlug: "base-1",
+						VpcUUID:  "vpc-1",
+						Image: struct {
+							Name string `json:"name"`
+							Slug string `json:"slug"`
+						}(struct {
+							Name string
+							Slug string
+						}{Name: "ubuntu", Slug: "18"}),
+						Networks: networks{
+							V4: []network{
+								{
+									Type:      "public",
+									IPAddress: "100.100.100.100",
+								},
+								{
+									Type:      "private",
+									IPAddress: "10.10.10.10",
+								},
+							},
+							V6: []network{
+								{
+									Type:      "public",
+									IPAddress: "::1",
+								},
+							},
+						},
+					},
+				},
+				defaultPort: 9100,
+			},
+			want: [][]prompbmarshal.Label{
+				discoveryutils.GetSortedLabels(map[string]string{
+					"__address__":                      "100.100.100.100:9100",
+					"__meta_digitalocean_droplet_id":   "15",
+					"__meta_digitalocean_droplet_name": "ubuntu-1",
+					"__meta_digitalocean_features":     ",feature-1,feature-2,",
+					"__meta_digitalocean_image":        "18",
+					"__meta_digitalocean_image_name":   "ubuntu",
+					"__meta_digitalocean_private_ipv4": "10.10.10.10",
+					"__meta_digitalocean_public_ipv4":  "100.100.100.100",
+					"__meta_digitalocean_public_ipv6":  "::1",
+					"__meta_digitalocean_region":       "do",
+					"__meta_digitalocean_size":         "base-1",
+					"__meta_digitalocean_status":       "active",
+					"__meta_digitalocean_tags":         ",private,test,",
+					"__meta_digitalocean_vpc":          "vpc-1",
+				}),
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got := addDropletLabels(tt.args.droplets, tt.args.defaultPort)
+			var sortedLabelss [][]prompbmarshal.Label
+			for _, labels := range got {
+				sortedLabelss = append(sortedLabelss, discoveryutils.GetSortedLabels(labels))
+			}
+			if !reflect.DeepEqual(sortedLabelss, tt.want) {
+				t.Errorf("addTasksLabels() \ngot  \n%v\n, \nwant \n%v\n", sortedLabelss, tt.want)
+			}
+
+		})
+	}
+}