Skip to content

Commit

Permalink
Replace weaveworks common with dskit (#5274)
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum authored Sep 22, 2023
1 parent d59717e commit 713f201
Show file tree
Hide file tree
Showing 31 changed files with 207 additions and 129 deletions.
1 change: 0 additions & 1 deletion .github/depcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ go_modules:
- github.com/prometheus/memcached_exporter
- github.com/prometheus/node_exporter
- github.com/prometheus/statsd_exporter
- github.com/weaveworks/common
- go.opentelemetry.io/collector
- sigs.k8s.io/controller-runtime

Expand Down
4 changes: 2 additions & 2 deletions cmd/grafana-agent/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/grafana/agent/pkg/supportbundle"
"github.com/grafana/agent/pkg/traces"
"github.com/grafana/agent/pkg/usagestats"
"github.com/grafana/dskit/signals"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/signals"
"google.golang.org/grpc"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -395,7 +395,7 @@ func (ep *Entrypoint) Start() error {

// Create a signal handler that will stop the Entrypoint once a termination
// signal is received.
signalHandler := signals.NewHandler(server.GoKitLogger(ep.log))
signalHandler := signals.NewHandler(ep.log)

notifier := make(chan os.Signal, 1)
signal.Notify(notifier, syscall.SIGHUP)
Expand Down
46 changes: 23 additions & 23 deletions component/common/net/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Package http contains a River serializable definition of the weaveworks weaveworks config in
// https://github.com/weaveworks/common/blob/master/server/server.go#L62.
// Package http contains a River serializable definition of the dskit config in
// https://github.com/grafana/dskit/blob/main/server/server.go#L72.
package net

import (
"flag"
"math"
"time"

weaveworks "github.com/weaveworks/common/server"
dskit "github.com/grafana/dskit/server"
)

const (
Expand All @@ -16,27 +16,27 @@ const (
// using zero as default grpc port to assing random free port when not configured
DefaultGRPCPort = 0

// defaults inherited from weaveworks
// defaults inherited from dskit
durationInfinity = time.Duration(math.MaxInt64)
size4MB = 4 << 20
)

// ServerConfig is a River configuration that allows one to configure a weaveworks.Server. It
// ServerConfig is a River configuration that allows one to configure a dskit.Server. It
// exposes a subset of the available configurations.
type ServerConfig struct {
// HTTP configures the HTTP weaveworks. Note that despite the block being present or not,
// the weaveworks is always started.
// HTTP configures the HTTP dskit. Note that despite the block being present or not,
// the dskit is always started.
HTTP *HTTPConfig `river:"http,block,optional"`

// GRPC configures the gRPC weaveworks. Note that despite the block being present or not,
// the weaveworks is always started.
// GRPC configures the gRPC dskit. Note that despite the block being present or not,
// the dskit is always started.
GRPC *GRPCConfig `river:"grpc,block,optional"`

// GracefulShutdownTimeout configures a timeout to gracefully shut down the server.
GracefulShutdownTimeout time.Duration `river:"graceful_shutdown_timeout,attr,optional"`
}

// HTTPConfig configures the HTTP weaveworks started by weaveworks.Server.
// HTTPConfig configures the HTTP dskit started by dskit.Server.
type HTTPConfig struct {
ListenAddress string `river:"listen_address,attr,optional"`
ListenPort int `river:"listen_port,attr,optional"`
Expand All @@ -46,8 +46,8 @@ type HTTPConfig struct {
ServerIdleTimeout time.Duration `river:"server_idle_timeout,attr,optional"`
}

// Into applies the configs from HTTPConfig into a weaveworks.Into.
func (h *HTTPConfig) Into(c *weaveworks.Config) {
// Into applies the configs from HTTPConfig into a dskit.Into.
func (h *HTTPConfig) Into(c *dskit.Config) {
c.HTTPListenAddress = h.ListenAddress
c.HTTPListenPort = h.ListenPort
c.HTTPConnLimit = h.ConnLimit
Expand All @@ -56,7 +56,7 @@ func (h *HTTPConfig) Into(c *weaveworks.Config) {
c.HTTPServerIdleTimeout = h.ServerIdleTimeout
}

// GRPCConfig configures the gRPC weaveworks started by weaveworks.Server.
// GRPCConfig configures the gRPC dskit started by dskit.Server.
type GRPCConfig struct {
ListenAddress string `river:"listen_address,attr,optional"`
ListenPort int `river:"listen_port,attr,optional"`
Expand All @@ -69,8 +69,8 @@ type GRPCConfig struct {
ServerMaxConcurrentStreams uint `river:"server_max_concurrent_streams,attr,optional"`
}

// Into applies the configs from GRPCConfig into a weaveworks.Into.
func (g *GRPCConfig) Into(c *weaveworks.Config) {
// Into applies the configs from GRPCConfig into a dskit.Into.
func (g *GRPCConfig) Into(c *dskit.Config) {
c.GRPCListenAddress = g.ListenAddress
c.GRPCListenPort = g.ListenPort
c.GRPCConnLimit = g.ConnLimit
Expand All @@ -82,11 +82,11 @@ func (g *GRPCConfig) Into(c *weaveworks.Config) {
c.GPRCServerMaxConcurrentStreams = g.ServerMaxConcurrentStreams
}

// Convert converts the River-based ServerConfig into a weaveworks.Config object.
func (c *ServerConfig) convert() weaveworks.Config {
cfg := newWeaveworksDefaultConfig()
// Convert converts the River-based ServerConfig into a dskit.Config object.
func (c *ServerConfig) convert() dskit.Config {
cfg := newdskitDefaultConfig()
// use the configured http/grpc blocks, and if not, use a mixin of our defaults, and
// weaveworks's as a fallback
// dskit's as a fallback
if c.HTTP != nil {
c.HTTP.Into(&cfg)
} else {
Expand All @@ -101,9 +101,9 @@ func (c *ServerConfig) convert() weaveworks.Config {
return cfg
}

// newWeaveworksDefaultConfig creates a new weaveworks.Config object with some overridden defaults.
func newWeaveworksDefaultConfig() weaveworks.Config {
c := weaveworks.Config{}
// newdskitDefaultConfig creates a new dskit.Config object with some overridden defaults.
func newdskitDefaultConfig() dskit.Config {
c := dskit.Config{}
c.RegisterFlags(flag.NewFlagSet("empty", flag.ContinueOnError))
// By default, do not register instrumentation since every metric is later registered
// inside a custom register
Expand All @@ -112,7 +112,7 @@ func newWeaveworksDefaultConfig() weaveworks.Config {
}

// DefaultServerConfig creates a new ServerConfig with defaults applied. Note that some are inherited from
// weaveworks, but copied in our config model to make the mixin logic simpler.
// dskit, but copied in our config model to make the mixin logic simpler.
func DefaultServerConfig() *ServerConfig {
return &ServerConfig{
HTTP: &HTTPConfig{
Expand Down
14 changes: 7 additions & 7 deletions component/common/net/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"
"time"

dskit "github.com/grafana/dskit/server"
"github.com/stretchr/testify/require"
weaveworks "github.com/weaveworks/common/server"

"github.com/grafana/river"
)
Expand Down Expand Up @@ -34,16 +34,16 @@ func TestConfig(t *testing.T) {
type testcase struct {
raw string
errExpected bool
assert func(t *testing.T, config weaveworks.Config)
assert func(t *testing.T, config dskit.Config)
}
var cases = map[string]testcase{
"empty config applies defaults": {
raw: ``,
assert: func(t *testing.T, config weaveworks.Config) {
assert: func(t *testing.T, config dskit.Config) {
// custom defaults
require.Equal(t, DefaultHTTPPort, config.HTTPListenPort)
require.Equal(t, DefaultGRPCPort, config.GRPCListenPort)
// defaults inherited from weaveworks
// defaults inherited from dskit
require.Equal(t, "", config.HTTPListenAddress)
require.Equal(t, "", config.GRPCListenAddress)
require.False(t, config.RegisterInstrumentation)
Expand All @@ -62,7 +62,7 @@ func TestConfig(t *testing.T) {
conn_limit = 10
server_write_timeout = "10s"
}`,
assert: func(t *testing.T, config weaveworks.Config) {
assert: func(t *testing.T, config dskit.Config) {
require.Equal(t, 8080, config.HTTPListenPort)
require.Equal(t, "0.0.0.0", config.HTTPListenAddress)
require.Equal(t, 10, config.HTTPConnLimit)
Expand All @@ -84,7 +84,7 @@ func TestConfig(t *testing.T) {
listen_address = "0.0.0.0"
server_max_send_msg_size = 10
}`,
assert: func(t *testing.T, config weaveworks.Config) {
assert: func(t *testing.T, config dskit.Config) {
// these should be overridden
require.Equal(t, 8080, config.HTTPListenPort)
require.Equal(t, "0.0.0.0", config.HTTPListenAddress)
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestConfig(t *testing.T) {
server_max_send_msg_size = 6
server_max_concurrent_streams = 7
}`,
assert: func(t *testing.T, config weaveworks.Config) {
assert: func(t *testing.T, config dskit.Config) {
// general
require.Equal(t, time.Minute, config.ServerGracefulShutdownTimeout)
// http
Expand Down
19 changes: 9 additions & 10 deletions component/common/net/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
dskit "github.com/grafana/dskit/server"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/weaveworks/common/logging"
weaveworks "github.com/weaveworks/common/server"
)

// TargetServer is wrapper around weaveworks.Server that handles some common configuration used in all flow components
// TargetServer is wrapper around dskit.Server that handles some common configuration used in all flow components
// that expose a network server. It just handles configuration and initialization, the handlers implementation are left
// to the consumer.
type TargetServer struct {
logger log.Logger
config *weaveworks.Config
config *dskit.Config
metricsNamespace string
server *weaveworks.Server
server *dskit.Server
}

// NewTargetServer creates a new TargetServer, applying some defaults to the server configuration.
Expand All @@ -38,7 +37,7 @@ func NewTargetServer(logger log.Logger, metricsNamespace string, reg prometheus.
config = DefaultServerConfig()
}

// convert from River into the weaveworks config
// convert from River into the dskit config
serverCfg := config.convert()
// Set the config to the new combined config.
// Avoid logging entire received request on failures
Expand All @@ -50,19 +49,19 @@ func NewTargetServer(logger log.Logger, metricsNamespace string, reg prometheus.
// To prevent metric collisions because all metrics are going to be registered in the global Prometheus registry.
ts.config.MetricsNamespace = ts.metricsNamespace
// We don't want the /debug and /metrics endpoints running, since this is not the main Flow HTTP server.
// We want this target to expose the least surface area possible, hence disabling WeaveWorks HTTP server metrics
// We want this target to expose the least surface area possible, hence disabling dskit HTTP server metrics
// and debugging functionality.
ts.config.RegisterInstrumentation = false
// Add logger to weaveworks
ts.config.Log = logging.GoKit(ts.logger)
// Add logger to dskit
ts.config.Log = ts.logger

return ts, nil
}

// MountAndRun mounts the handlers and starting the server.
func (ts *TargetServer) MountAndRun(mountRoute func(router *mux.Router)) error {
level.Info(ts.logger).Log("msg", "starting server")
srv, err := weaveworks.New(*ts.config)
srv, err := dskit.New(*ts.config)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions component/loki/process/stages/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

"github.com/alecthomas/units"
"github.com/grafana/agent/pkg/util"
dskit "github.com/grafana/dskit/server"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ww "github.com/weaveworks/common/server"
)

// Not all these are tested but are here to make sure the different types marshal without error
Expand Down Expand Up @@ -47,7 +47,7 @@ stage.drop {

func TestDropStage(t *testing.T) {
// Enable debug logging
cfg := &ww.Config{}
cfg := &dskit.Config{}
require.Nil(t, cfg.LogLevel.Set("debug"))

tenBytes, _ := units.ParseBase2Bytes("10B")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p *PushTarget) push(w http.ResponseWriter, r *http.Request) {

if err := p.doSendEntry(ctx, entry); err != nil {
// NOTE: timeout errors can be tracked with from the metrics exposed by
// the spun weaveworks server.
// the spun dskit server.
// loki.source.gcplog.componentid_push_target_request_duration_seconds_count{status_code="503"}
level.Warn(p.logger).Log("msg", "error sending log entry", "err", err.Error())
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down
2 changes: 1 addition & 1 deletion component/mimir/rules/kubernetes/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/go-kit/log/level"
"github.com/grafana/agent/component"
mimirClient "github.com/grafana/agent/pkg/mimir/client"
"github.com/grafana/dskit/instrument"
promListers "github.com/prometheus-operator/prometheus-operator/pkg/client/listers/monitoring/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/instrument"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
Expand Down
2 changes: 1 addition & 1 deletion converter/internal/staticconvert/internal/build/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (b *IntegrationsV1ConfigBuilder) appendLogging(config *server.Config) {
func toLogging(config *server.Config) *logging.Options {
return &logging.Options{
Level: logging.Level(config.LogLevel.String()),
Format: logging.Format(config.LogFormat.String()),
Format: logging.Format(config.LogFormat),
}
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ require (
github.com/testcontainers/testcontainers-go/modules/k3s v0.0.0-20230615142642-c175df34bd1d
github.com/uber/jaeger-client-go v2.30.0+incompatible
github.com/vincent-petithory/dataurl v1.0.0
github.com/weaveworks/common v0.0.0-20230511094633-334485600903
github.com/webdevops/azure-metrics-exporter v0.0.0-20230502203721-b2bfd97b5313
github.com/webdevops/go-common v0.0.0-20230502000651-d37d46be8ee7
github.com/wk8/go-ordered-map v0.2.0
Expand Down Expand Up @@ -574,7 +573,6 @@ require (
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
github.com/vmware/govmomi v0.27.2 // indirect
github.com/vultr/govultr/v2 v2.17.2 // indirect
github.com/weaveworks/promrus v1.2.0 // indirect
github.com/willf/bitset v1.1.11 // indirect
github.com/willf/bloom v2.0.3+incompatible // indirect
github.com/xanzy/ssh-agent v0.3.1 // indirect
Expand Down
Loading

0 comments on commit 713f201

Please sign in to comment.