Debian Codegraph @

tutorials/whatsup/internal/playground_test.go

raw
1 // Copyright 2023 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //go:build interactive
15 // +build interactive
16
17 package internal
18
19 import (
20 "fmt"
21 "os"
22 "strings"
23 "testing"
24 "time"
25
26 "github.com/efficientgo/core/testutil"
27 "github.com/efficientgo/e2e"
28 e2edb "github.com/efficientgo/e2e/db"
29 e2einteractive "github.com/efficientgo/e2e/interactive"
30 e2emon "github.com/efficientgo/e2e/monitoring"
31 "github.com/efficientgo/e2e/monitoring/promconfig"
32 sdconfig "github.com/efficientgo/e2e/monitoring/promconfig/discovery/config"
33 "github.com/efficientgo/e2e/monitoring/promconfig/discovery/targetgroup"
34 "github.com/prometheus/common/model"
35 "gopkg.in/yaml.v2"
36 )
37
38 func TestPlayground(t *testing.T) {
39 // NOTE: Only one run at the time will work due to static ports used.
40
41 e, err := e2e.New(e2e.WithVerbose())
42 testutil.Ok(t, err)
43 t.Cleanup(e.Close)
44
45 // Setup in-memory Jaeger as our tracing backend.
46 jaeger := e2emon.AsInstrumented(e.Runnable("tracing").
47 WithPorts(
48 map[string]int{
49 "http.front": 16686,
50 "grpc.otlp": 4317,
51 "http.metrics": 14269,
52 }).
53 Init(e2e.StartOptions{
54 Image: "jaegertracing/all-in-one:1.35",
55 EnvVars: map[string]string{"COLLECTOR_OTLP_ENABLED": "true"},
56 }), "http.metrics")
57 testutil.Ok(t, e2e.StartAndWaitReady(jaeger))
58
59 prom := e2edb.NewPrometheus(e, "prometheus", e2edb.WithImage("prom/prometheus:v2.43.0-stringlabels"))
60 testutil.Ok(t, e2e.StartAndWaitReady(prom))
61
62 // Write config file for whatsup app.
63 c := Config{
64 PrometheusAddr: "http://" + prom.Endpoint("http"),
65 TraceEndpoint: jaeger.Endpoint("grpc.otlp"),
66 TraceSamplingRatio: 1,
67 }
68 b, err := yaml.Marshal(c)
69 testutil.Ok(t, err)
70 testutil.Ok(t, os.WriteFile("../whatsup.yaml", b, os.ModePerm))
71
72 testutil.Ok(t, prom.SetConfig(prometheusConfig(map[string]string{
73 "prometheus": prom.InternalEndpoint("http"),
74 "jaeger": jaeger.InternalEndpoint("http.metrics"),
75 "whatsup": whatsupAddr(fmt.Sprintf("host.docker.internal:%v", WhatsupPort)),
76 })))
77 // Due to VM based docker setups (e.g. MacOS), file sharing can be slower - do more sighups just in case (noops if all good)
78 prom.Exec(e2e.NewCommand("kill", "-SIGHUP", "1"))
79 prom.Exec(e2e.NewCommand("kill", "-SIGHUP", "1"))
80
81 // Best effort.
82 fmt.Println(e2einteractive.OpenInBrowser(convertToExternal("http://" + jaeger.Endpoint("http.front"))))
83 fmt.Println(e2einteractive.OpenInBrowser(convertToExternal("http://" + prom.Endpoint("http"))))
84 testutil.Ok(t, e2einteractive.RunUntilEndpointHitWithPort(19920))
85 }
86
87 func convertToExternal(endpoint string) string {
88 a := os.Getenv("HOSTADDR")
89 if a == "" {
90 return endpoint
91 }
92 // YOLO, fix and test.
93 return fmt.Sprintf("%v:%v", a, strings.Split(endpoint, ":")[2])
94 }
95
96 func prometheusConfig(jobToScrapeTargetAddress map[string]string) promconfig.Config {
97 h, _ := os.Hostname()
98 cfg := promconfig.Config{
99 GlobalConfig: promconfig.GlobalConfig{
100 ExternalLabels: map[model.LabelName]model.LabelValue{"prometheus": model.LabelValue(h)},
101 ScrapeInterval: model.Duration(15 * time.Second),
102 },
103 }
104
105 for job, s := range jobToScrapeTargetAddress {
106 scfg := &promconfig.ScrapeConfig{
107 JobName: job,
108 ServiceDiscoveryConfig: sdconfig.ServiceDiscoveryConfig{},
109 }
110
111 g := &targetgroup.Group{
112 Targets: []model.LabelSet{map[model.LabelName]model.LabelValue{
113 model.AddressLabel: model.LabelValue(s),
114 }},
115 }
116 scfg.ServiceDiscoveryConfig.StaticConfigs = append(scfg.ServiceDiscoveryConfig.StaticConfigs, g)
117 cfg.ScrapeConfigs = append(cfg.ScrapeConfigs, scfg)
118 }
119 return cfg
120 }