Skip to content

Commit

Permalink
Added support for on-prem synthetic monitoring (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhogayatakb authored Oct 17, 2024
1 parent bc1cc75 commit 92ba90b
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 16 deletions.
58 changes: 58 additions & 0 deletions cmd/host-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"

"github.com/middleware-labs/mw-agent/pkg/agent"
"github.com/middleware-labs/synthetics-agent/pkg/worker"
"github.com/prometheus/common/version"
"gopkg.in/natefinch/lumberjack.v2"

Expand Down Expand Up @@ -105,6 +106,14 @@ func getFlags(execPath string, cfg *agent.HostConfig) []cli.Flag {
Value: "",
Hidden: true,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "api-url-for-synthetic-monitoring",
EnvVars: []string{"MW_API_URL_FOR_SYNTHETIC_MONITORING"},
Destination: &cfg.APIURLForSyntheticMonitoring,
DefaultText: "wss://app.middleware.io/plsrws/v2",
Value: "wss://app.middleware.io/plsrws/v2",
Hidden: true,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "host-tags",
Usage: "Tags for this host.",
Expand Down Expand Up @@ -161,6 +170,14 @@ func getFlags(execPath string, cfg *agent.HostConfig) []cli.Flag {
DefaultText: "true",
Value: true, // log collection is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.synthetic-monitoring",
Usage: "Flag to enable or disable synthetic monitoring.",
EnvVars: []string{"MW_AGENT_FEATURES_SYNTHETIC_MONITORING"},
Destination: &cfg.AgentFeatures.SyntheticMonitoring,
DefaultText: "false",
Value: false, // log collection is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "mw-agent-self-profiling",
Usage: "For Profiling MW Agent itself.",
Expand Down Expand Up @@ -342,6 +359,19 @@ func main() {
zap.String("api-url-for-config-check", cfg.APIURLForConfigCheck))
}

if cfg.APIURLForSyntheticMonitoring == "" {
cfg.APIURLForSyntheticMonitoring, err = agent.GetAPIURLForSyntheticMonitoring(cfg.Target)
// could not derive api url for synthetic monitoring from target
if err != nil {
logger.Info("could not derive api url for synthetic monitoring from target",
zap.String("target", cfg.Target))
return err
}

logger.Info("derived api url for synthetic monitoring",
zap.String("api-url-for-synthetic-monitoring", cfg.APIURLForSyntheticMonitoring))
}

hostAgent := agent.NewHostAgent(
cfg,
agent.WithHostAgentLogger(logger),
Expand Down Expand Up @@ -437,6 +467,34 @@ func main() {
ConfigProviderSettings: configProviderSetting,
}

if cfg.AgentFeatures.SyntheticMonitoring {
config := worker.Config{
Mode: worker.ModeAgent,
Token: cfg.APIKey,
Hostname: hostname,
PulsarHost: cfg.APIURLForSyntheticMonitoring,
}

logger.Info("starting synthetic worker: ")
syntheticWorker, err := worker.New(&config)
if err != nil {
logger.Error("Failed to create worker")
}

go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("Turning off the synthetic monitoring...")
return
default:
syntheticWorker.Run()
}
}
}(ctx)

}

collector, _ := otelcol.NewCollector(settings)
svcConfig := &service.Config{
Name: "MiddlewareHostAgent",
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ require (
)

require (
github.com/middleware-labs/synthetics-agent v1.0.27
go.opentelemetry.io/collector/confmap/converter/expandconverter v0.102.2-0.20240606174409-6888f8f7a45f
go.opentelemetry.io/collector/confmap/provider/envprovider v0.102.2-0.20240606174409-6888f8f7a45f
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.102.2-0.20240606174409-6888f8f7a45f
Expand All @@ -113,13 +114,16 @@ require (
github.com/IBM/sarama v1.43.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Showmax/go-fqdn v1.0.0 // indirect
github.com/adakailabs/go-traceroute v0.0.0-20210727014431-97524352ab91 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/apache/thrift v0.20.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.53.11 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down Expand Up @@ -201,6 +205,7 @@ require (
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jhump/protoreflect v1.17.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
Expand All @@ -215,6 +220,9 @@ require (
github.com/leodido/ragel-machinery v0.0.0-20190525184631-5f46317e436b // indirect
github.com/leoluk/perflib_exporter v0.2.1 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/likexian/gokit v0.25.15 // indirect
github.com/likexian/whois v1.15.5 // indirect
github.com/likexian/whois-parser v1.24.20 // indirect
github.com/linode/linodego v1.33.0 // indirect
github.com/lufia/plan9stats v0.0.0-20220913051719-115f729f3c8c // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down Expand Up @@ -265,6 +273,7 @@ require (
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect
github.com/prometheus-community/pro-bing v0.1.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
Expand Down
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM=
github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
github.com/adakailabs/go-traceroute v0.0.0-20210727014431-97524352ab91 h1:9d55wDeAS8L+TIlP1lDG4YcslmHv/J8j5semIi52ZBs=
github.com/adakailabs/go-traceroute v0.0.0-20210727014431-97524352ab91/go.mod h1:vo+oaEco9ORaCSCcSpITTRIkJ4n/CNWJ1YxjnY+HpcU=
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.1.1 h1:hrjKESvSqGHzRb4yW1ciisFJ4p3MGYih6icjJvbsmV8=
Expand Down Expand Up @@ -131,6 +133,10 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw=
github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -492,6 +498,8 @@ github.com/jcmturner/gokrb5/v8 v8.4.4 h1:x1Sv4HaTpepFkXbt2IkL29DXRf8sOfZXo8eRKh6
github.com/jcmturner/gokrb5/v8 v8.4.4/go.mod h1:1btQEpgT6k+unzCwX1KdWMEwPPkkgBtP+F6aCACiMrs=
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94=
github.com/jhump/protoreflect v1.17.0/go.mod h1:h9+vUUL38jiBzck8ck+6G/aeMX8Z4QUY/NiJPwPNi+8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down Expand Up @@ -549,6 +557,12 @@ github.com/leoluk/perflib_exporter v0.2.1 h1:/3/ut1k/jFt5p4ypjLZKDHDqlXAK6ERZPVW
github.com/leoluk/perflib_exporter v0.2.1/go.mod h1:MinSWm88jguXFFrGsP56PtleUb4Qtm4tNRH/wXNXRTI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/likexian/gokit v0.25.15 h1:QjospM1eXhdMMHwZRpMKKAHY/Wig9wgcREmLtf9NslY=
github.com/likexian/gokit v0.25.15/go.mod h1:S2QisdsxLEHWeD/XI0QMVeggp+jbxYqUxMvSBil7MRg=
github.com/likexian/whois v1.15.5 h1:gpPxyCTJtLtJDmakHCo//0ZjK/ocI01GCAd/WBJ2oH8=
github.com/likexian/whois v1.15.5/go.mod h1:4b6o1QTCfjwrB5I3KeNQnn79QtuPUTsewsE+ys94I78=
github.com/likexian/whois-parser v1.24.20 h1:oxEkRi0GxgqWQRLDMJpXU1EhgWmLmkqEFZ2ChXTeQLE=
github.com/likexian/whois-parser v1.24.20/go.mod h1:rAtaofg2luol09H+ogDzGIfcG8ig1NtM5R16uQADDz4=
github.com/linode/linodego v1.33.0 h1:cX2FYry7r6CA1ujBMsdqiM4VhvIQtnWsOuVblzfBhCw=
github.com/linode/linodego v1.33.0/go.mod h1:dSJJgIwqZCF5wnpuC6w5cyIbRtcexAm7uVvuJopGB40=
github.com/lufia/plan9stats v0.0.0-20220913051719-115f729f3c8c h1:VtwQ41oftZwlMnOEbMWQtSEUgU64U4s+GHk7hZK+jtY=
Expand Down Expand Up @@ -603,6 +617,8 @@ github.com/middleware-labs/opentelemetry-collector-contrib/receiver/mysqlreceive
github.com/middleware-labs/opentelemetry-collector-contrib/receiver/mysqlreceiver v0.91.1-0.20240904102402-9a4209dce415/go.mod h1:OttuOJo9K44BQlDnCLA5nsq4RABqXYfqv4VVp3tZj1E=
github.com/middleware-labs/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.91.1-0.20240904102402-9a4209dce415 h1:0b9FB/4QONH739msx7jLNXuJim+YCgjBG8kqnEr7/j4=
github.com/middleware-labs/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.91.1-0.20240904102402-9a4209dce415/go.mod h1:AuqfasnT59jmtuQOc9HnjKML1yGSGEpoMm6nGyUSdl0=
github.com/middleware-labs/synthetics-agent v1.0.27 h1:wT8iBfQcjpMaobO9iTUE2IGB0JXix+Gtv9/X19ajpoo=
github.com/middleware-labs/synthetics-agent v1.0.27/go.mod h1:403/WYsXpxnPCKxiROItjX9+uuqoG/gyTycmPnAH/K4=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
Expand Down Expand Up @@ -812,6 +828,8 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c h1:NRoLoZvkBTKvR5gQLgA3e0hqjkY9u1wm+iOL45VN/qI=
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/prometheus-community/pro-bing v0.1.0 h1:zjzLGhfNPP0bP1OlzGB+SJcguOViw7df12LPg2vUJh8=
github.com/prometheus-community/pro-bing v0.1.0/go.mod h1:BpWlHurD9flHtzq8wrh8QGWYz9ka9z9ZJAyOel8ej58=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
Expand Down Expand Up @@ -1183,6 +1201,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
Expand Down
56 changes: 40 additions & 16 deletions pkg/agent/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,28 @@ func (p InfraPlatform) String() string {
}

type AgentFeatures struct {
MetricCollection bool
LogCollection bool
MetricCollection bool
LogCollection bool
SyntheticMonitoring bool
}

// BaseConfig stores general configuration for all agent types
type BaseConfig struct {
APIKey string
Target string
EnableSyntheticMonitoring bool
ConfigCheckInterval string
FetchAccountOtelConfig bool
DockerEndpoint string
APIURLForConfigCheck string
FluentPort string
InfraPlatform InfraPlatform
OtelConfigFile string
AgentFeatures AgentFeatures
SelfProfiling bool
ProfilngServerURL string
InternalMetricsPort uint
APIKey string
Target string
EnableSyntheticMonitoring bool
ConfigCheckInterval string
FetchAccountOtelConfig bool
DockerEndpoint string
APIURLForConfigCheck string
APIURLForSyntheticMonitoring string
FluentPort string
InfraPlatform InfraPlatform
OtelConfigFile string
AgentFeatures AgentFeatures
SelfProfiling bool
ProfilngServerURL string
InternalMetricsPort uint
}

// String() implements stringer interface for BaseConfig
Expand Down Expand Up @@ -215,6 +217,28 @@ func GetAPIURLForConfigCheck(target string) (string, error) {
return strings.TrimSuffix(target, "/"), nil
}

// GetAPIURLForSyntheticMonitoring constructs the WebSocket URL for synthetic monitoring
func GetAPIURLForSyntheticMonitoring(target string) (string, error) {
// Parse the URL
parsedURL, err := url.Parse(target)
if err != nil {
return "", err
}

// Check if the host part of the URL contains more than one '.'
hostParts := strings.Split(parsedURL.Hostname(), ".")
if len(hostParts) < 3 {
return "", ErrInvalidTarget
}

// Ensure no trailing slash in the path
trimmedURL := strings.TrimSuffix(parsedURL.Host, "/")

// Build the WebSocket URL
webSocketURL := "wss://" + trimmedURL + "/plsrws/v2"
return webSocketURL, nil
}

type Profiler struct {
Logger *zap.Logger
ServerAddress string
Expand Down
42 changes: 42 additions & 0 deletions pkg/agent/definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,48 @@ func TestGetAPIURLForConfigCheck(t *testing.T) {
}
}

func TestGetAPIURLForSyntheticMonitoring(t *testing.T) {
tests := []struct {
name string
url string
expectedResult string
err error
}{
{
name: "URL with both '/' and '.' and without trailing '/'",
url: "https://myaccount.middleware.io",
expectedResult: "wss://myaccount.middleware.io/plsrws/v2",
err: nil,
},
{
name: "URL with trailing '/'",
url: "https://myaccount.middleware.io/",
expectedResult: "wss://myaccount.middleware.io/plsrws/v2",
err: nil,
},
{
name: "URL with only one '.'",
url: "https://middleware.io",
expectedResult: "",
err: ErrInvalidTarget,
},
{
name: "URL with custom domain",
url: "https://myaccount.test.mw.io",
expectedResult: "wss://myaccount.test.mw.io/plsrws/v2",
err: nil,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := GetAPIURLForSyntheticMonitoring(test.url)
assert.Equal(t, test.err, err)
assert.Equal(t, test.expectedResult, result)
})
}
}

func TestWithKubeAgentMonitorClusterName(t *testing.T) {
kubeAgentMonitor := &KubeAgentMonitor{}
expected := "test-cluster"
Expand Down

0 comments on commit 92ba90b

Please sign in to comment.