Skip to content

Commit

Permalink
Use scrape manager (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
isacikgoz authored Dec 21, 2023
1 parent 9b4153b commit f87dfae
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 209 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ linters:
disable-all: true
enable:
- bodyclose
- errcheck
- gocritic
- gofmt
- goimports
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/mattermost/mattermost-plugin-metrics
go 1.20

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/go-kit/log v0.2.1
github.com/mattermost/mattermost/server/public v0.0.9
github.com/mattermost/mattermost/server/v8 v8.0.0-20231109142113-8bf0c1971415
github.com/pkg/errors v0.9.1
Expand All @@ -18,7 +20,6 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aws/aws-sdk-go v1.44.302 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand All @@ -30,7 +31,6 @@ require (
github.com/fatih/color v1.15.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
69 changes: 47 additions & 22 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package main
import (
"encoding/json"
"fmt"
"net"
"path/filepath"
"reflect"

"github.com/mattermost/mattermost/server/public/model"
"github.com/alecthomas/units"
"github.com/pkg/errors"
promModel "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"

"github.com/mattermost/mattermost/server/public/model"
)

// configuration captures the plugin's external configuration as exposed in the Mattermost server
Expand Down Expand Up @@ -51,7 +56,7 @@ func (c *configuration) SetDefaults() {
c.EnableMemorySnapshotOnShutdown = model.NewBool(true)
}
if c.BodySizeLimitBytes == nil {
c.BodySizeLimitBytes = model.NewInt64(10000000)
c.BodySizeLimitBytes = model.NewInt64(int64(units.GiB))
}
if c.SampleLimit == nil {
c.SampleLimit = model.NewInt(0)
Expand Down Expand Up @@ -101,26 +106,6 @@ func (c *configuration) Clone() (*configuration, error) {
return &clone, nil
}

// getConfiguration retrieves the active configuration under lock, making it safe to use
// concurrently. The active configuration may change underneath the client of this method, but
// the struct returned by this API call is considered immutable.
func (p *Plugin) getConfiguration() (*configuration, error) {
p.configurationLock.Lock()
defer p.configurationLock.Unlock()

if p.configuration == nil {
p.configuration = new(configuration)
p.configuration.SetDefaults()
}

return p.configuration.Clone()
}

// func (p *Plugin) saveConfiguration() error {
// p.API.SavePluginConfig()
// return nil
// }

// setConfiguration replaces the active configuration under lock.
//
// Do not call setConfiguration while holding the configurationLock, as sync.Mutex is not
Expand Down Expand Up @@ -222,3 +207,43 @@ func (p *Plugin) isHA() bool {

return cfg.ClusterSettings.Enable != nil && *cfg.ClusterSettings.Enable
}

func generateTargetGroup(appCfg *model.Config, nodes []*model.ClusterDiscovery) (map[string][]*targetgroup.Group, error) {
host, port, err := net.SplitHostPort(*appCfg.MetricsSettings.ListenAddress)
if err != nil {
return nil, fmt.Errorf("could not parse the listen address %q", *appCfg.MetricsSettings.ListenAddress)
}

sync := make(map[string][]*targetgroup.Group)

if nodes == nil || len(nodes) < 2 {
if host == "" {
host = "localhost"
}
sync["prometheus"] = []*targetgroup.Group{
{
Targets: []promModel.LabelSet{
{
promModel.AddressLabel: promModel.LabelValue(net.JoinHostPort(host, port)),
},
},
},
}

return sync, nil
}

targets := make([]promModel.LabelSet, len(nodes))
for i, node := range nodes {
targets[i] = promModel.LabelSet{
promModel.AddressLabel: promModel.LabelValue(net.JoinHostPort(node.Hostname, port)),
}
}
sync["prometheus"] = []*targetgroup.Group{
{
Targets: targets,
},
}

return sync, nil
}
8 changes: 2 additions & 6 deletions server/const.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package main

import (
"github.com/prometheus/prometheus/model/labels"
)

var (
sampleMutator func(labels.Labels) labels.Labels //nolint:unused
const (
PluginName = "mattermost-plugin-metrics"
)
3 changes: 2 additions & 1 deletion server/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"testing"

"github.com/mattermost/mattermost-plugin-metrics/server/mocks"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/mattermost/mattermost-plugin-metrics/server/mocks"
)

func TestLog(t *testing.T) {
Expand Down
131 changes: 131 additions & 0 deletions server/mocks/storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f87dfae

Please sign in to comment.