Skip to content

Commit

Permalink
Add a dedicated system for generic configuration options
Browse files Browse the repository at this point in the history
There are certain configuration options that basically apply to all
Buildbarn binaries. One example is Jaeger. Right now only bb_storage
supports it, even though we want it to be configurable for all binaries.

Introduce a separate set of messages for setting up these options. Add a
pkg/global package that contains an initialization function for all of
these. From now on we'll be calling this from within main() in all
binaries.

This should make it easier to complete this PR:

buildbarn/bb-remote-execution#32
  • Loading branch information
EdSchouten committed Mar 31, 2020
1 parent 8d9054f commit fb0be44
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 71 deletions.
1 change: 1 addition & 0 deletions cmd/bb_replicator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"//pkg/blobstore/configuration:go_default_library",
"//pkg/blobstore/mirrored:go_default_library",
"//pkg/digest:go_default_library",
"//pkg/global:go_default_library",
"//pkg/grpc:go_default_library",
"//pkg/proto/configuration/bb_replicator:go_default_library",
"//pkg/proto/replicator:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions cmd/bb_replicator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
blobstore_configuration "github.com/buildbarn/bb-storage/pkg/blobstore/configuration"
"github.com/buildbarn/bb-storage/pkg/blobstore/mirrored"
"github.com/buildbarn/bb-storage/pkg/digest"
"github.com/buildbarn/bb-storage/pkg/global"
bb_grpc "github.com/buildbarn/bb-storage/pkg/grpc"
"github.com/buildbarn/bb-storage/pkg/proto/configuration/bb_replicator"
replicator_pb "github.com/buildbarn/bb-storage/pkg/proto/replicator"
Expand All @@ -25,6 +26,9 @@ func main() {
if err := util.UnmarshalConfigurationFromFile(os.Args[1], &configuration); err != nil {
log.Fatalf("Failed to read configuration from %s: %s", os.Args[1], err)
}
if err := global.ApplyConfiguration(configuration.Global); err != nil {
log.Fatal("Failed to apply global configuration options: ", err)
}

source, err := blobstore_configuration.CreateCASBlobAccessObjectFromConfig(
configuration.Source,
Expand Down
2 changes: 1 addition & 1 deletion cmd/bb_storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ go_library(
"//pkg/blobstore/configuration:go_default_library",
"//pkg/builder:go_default_library",
"//pkg/cas:go_default_library",
"//pkg/global:go_default_library",
"//pkg/grpc:go_default_library",
"//pkg/opencensus:go_default_library",
"//pkg/proto/configuration/bb_storage:go_default_library",
"//pkg/util:go_default_library",
"@com_github_bazelbuild_remote_apis//build/bazel/remote/execution/v2:go_default_library",
Expand Down
7 changes: 3 additions & 4 deletions cmd/bb_storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
blobstore_configuration "github.com/buildbarn/bb-storage/pkg/blobstore/configuration"
"github.com/buildbarn/bb-storage/pkg/builder"
"github.com/buildbarn/bb-storage/pkg/cas"
"github.com/buildbarn/bb-storage/pkg/global"
bb_grpc "github.com/buildbarn/bb-storage/pkg/grpc"
"github.com/buildbarn/bb-storage/pkg/opencensus"
"github.com/buildbarn/bb-storage/pkg/proto/configuration/bb_storage"
"github.com/buildbarn/bb-storage/pkg/util"
"github.com/gorilla/mux"
Expand All @@ -30,9 +30,8 @@ func main() {
if err := util.UnmarshalConfigurationFromFile(os.Args[1], &configuration); err != nil {
log.Fatalf("Failed to read configuration from %s: %s", os.Args[1], err)
}

if configuration.Jaeger != nil {
opencensus.Initialize(configuration.Jaeger)
if err := global.ApplyConfiguration(configuration.Global); err != nil {
log.Fatal("Failed to apply global configuration options: ", err)
}

// Storage access.
Expand Down
9 changes: 6 additions & 3 deletions pkg/opencensus/BUILD.bazel → pkg/global/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["init.go"],
importpath = "github.com/buildbarn/bb-storage/pkg/opencensus",
srcs = ["apply_configuration.go"],
importpath = "github.com/buildbarn/bb-storage/pkg/global",
visibility = ["//visibility:public"],
deps = [
"//pkg/proto/configuration/bb_storage:go_default_library",
"//pkg/proto/configuration/global:go_default_library",
"//pkg/util:go_default_library",
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/push:go_default_library",
"@io_opencensus_go//plugin/ocgrpc:go_default_library",
"@io_opencensus_go//stats/view:go_default_library",
"@io_opencensus_go//trace:go_default_library",
Expand Down
86 changes: 86 additions & 0 deletions pkg/global/apply_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package global

import (
"log"
"runtime"
"time"

pb "github.com/buildbarn/bb-storage/pkg/proto/configuration/global"
"github.com/buildbarn/bb-storage/pkg/util"
"github.com/golang/protobuf/ptypes"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"

"contrib.go.opencensus.io/exporter/jaeger"
prometheus_exporter "contrib.go.opencensus.io/exporter/prometheus"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.opencensus.io/zpages"
)

// ApplyConfiguration applies configuration options to the running
// process. These configuration options are global, in that they apply
// to all Buildbarn binaries, regardless of their purpose.
func ApplyConfiguration(configuration *pb.Configuration) error {
// Push traces to Jaeger.
if jaegerConfiguration := configuration.GetJaeger(); jaegerConfiguration != nil {
pe, err := prometheus_exporter.NewExporter(prometheus_exporter.Options{
Registry: prometheus.DefaultRegisterer.(*prometheus.Registry),
Namespace: "bb_storage",
})
if err != nil {
return util.StatusWrap(err, "Failed to create the Prometheus stats exporter")
}
view.RegisterExporter(pe)
if err := view.Register(ocgrpc.DefaultServerViews...); err != nil {
return util.StatusWrap(err, "Failed to register ocgrpc server views")
}
zpages.Handle(nil, "/debug")
je, err := jaeger.NewExporter(jaeger.Options{
AgentEndpoint: jaegerConfiguration.AgentEndpoint,
CollectorEndpoint: jaegerConfiguration.CollectorEndpoint,
Process: jaeger.Process{
ServiceName: jaegerConfiguration.ServiceName,
},
})
if err != nil {
return util.StatusWrap(err, "Failed to create the Jaeger exporter")
}
trace.RegisterExporter(je)
if jaegerConfiguration.AlwaysSample {
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}
}

// Enable mutex profiling.
runtime.SetMutexProfileFraction(int(configuration.GetMutexProfileFraction()))

// Periodically push metrics to a Prometheus Pushgateway, as
// opposed to letting the Prometheus server scrape the metrics.
if pushgateway := configuration.GetPrometheusPushgateway(); pushgateway != nil {
pusher := push.New(pushgateway.Url, pushgateway.Job)
pusher.Gatherer(prometheus.DefaultGatherer)
if basicAuthentication := pushgateway.BasicAuthentication; basicAuthentication != nil {
pusher.BasicAuth(basicAuthentication.Username, basicAuthentication.Password)
}
for key, value := range pushgateway.Grouping {
pusher.Grouping(key, value)
}
pushInterval, err := ptypes.Duration(pushgateway.PushInterval)
if err != nil {
return util.StatusWrap(err, "Failed to parse push interval")
}

go func() {
for {
if err := pusher.Push(); err != nil {
log.Print("Failed to push metrics to Prometheus Pushgateway: ", err)
}
time.Sleep(pushInterval)
}
}()
}

return nil
}
46 changes: 0 additions & 46 deletions pkg/opencensus/init.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/proto/configuration/bb_replicator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ proto_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/proto/configuration/blobstore:blobstore_proto",
"//pkg/proto/configuration/global:global_proto",
"//pkg/proto/configuration/grpc:grpc_proto",
],
)
Expand All @@ -26,6 +27,7 @@ go_proto_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/proto/configuration/blobstore:go_default_library",
"//pkg/proto/configuration/global:go_default_library",
"//pkg/proto/configuration/grpc:go_default_library",
],
)
4 changes: 4 additions & 0 deletions pkg/proto/configuration/bb_replicator/bb_replicator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";
package buildbarn.configuration.bb_replicator;

import "pkg/proto/configuration/blobstore/blobstore.proto";
import "pkg/proto/configuration/global/global.proto";
import "pkg/proto/configuration/grpc/grpc.proto";

option go_package = "github.com/buildbarn/bb-storage/pkg/proto/configuration/bb_replicator";
Expand All @@ -25,4 +26,7 @@ message ApplicationConfiguration {

// Maximum Protobuf message size to unmarshal.
int64 maximum_message_size_bytes = 6;

// Common configuration options that apply to all Buildbarn binaries.
buildbarn.configuration.global.Configuration global = 7;
}
3 changes: 2 additions & 1 deletion pkg/proto/configuration/bb_storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ proto_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/proto/configuration/blobstore:blobstore_proto",
"//pkg/proto/configuration/global:global_proto",
"//pkg/proto/configuration/grpc:grpc_proto",
],
)

go_proto_library(
name = "bb_storage_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "github.com/buildbarn/bb-storage/pkg/proto/configuration/bb_storage",
proto = ":bb_storage_proto",
visibility = ["//visibility:public"],
deps = [
"//pkg/proto/configuration/blobstore:go_default_library",
"//pkg/proto/configuration/global:go_default_library",
"//pkg/proto/configuration/grpc:go_default_library",
],
)
Expand Down
22 changes: 6 additions & 16 deletions pkg/proto/configuration/bb_storage/bb_storage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,17 @@ syntax = "proto3";
package buildbarn.configuration.bb_storage;

import "pkg/proto/configuration/blobstore/blobstore.proto";
import "pkg/proto/configuration/global/global.proto";
import "pkg/proto/configuration/grpc/grpc.proto";

option go_package = "github.com/buildbarn/bb-storage/pkg/proto/configuration/bb_storage";

message JaegerConfiguration {
// Jaeger agent endpoint.
string agent_endpoint = 1;

// Jaeger collector endpoint.
string collector_endpoint = 2;

// OpenTracing service name.
string service_name = 3;

// Whether or not all traces should be sampled.
bool always_sample = 4;
}

message ApplicationConfiguration {
// Blobstore configuration for the bb-storage instance.
buildbarn.configuration.blobstore.BlobstoreConfiguration blobstore = 1;

// Jaeger configuration for tracing.
JaegerConfiguration jaeger = 2;
// Jaeger configuration has moved into 'global'.
reserved 2;

// Address on which to listen to expose Prometheus metrics.
string http_listen_address = 3;
Expand All @@ -47,4 +34,7 @@ message ApplicationConfiguration {

// Maximum Protobuf message size to unmarshal.
int64 maximum_message_size_bytes = 8;

// Common configuration options that apply to all Buildbarn binaries.
buildbarn.configuration.global.Configuration global = 9;
}
24 changes: 24 additions & 0 deletions pkg/proto/configuration/global/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

go_library(
name = "go_default_library",
embed = [":global_go_proto"],
importpath = "github.com/buildbarn/bb-storage/pkg/proto/configuration/global",
visibility = ["//visibility:public"],
)

proto_library(
name = "global_proto",
srcs = ["global.proto"],
visibility = ["//visibility:public"],
deps = ["@com_google_protobuf//:duration_proto"],
)

go_proto_library(
name = "global_go_proto",
importpath = "github.com/buildbarn/bb-storage/pkg/proto/configuration/global",
proto = ":global_proto",
visibility = ["//visibility:public"],
)
60 changes: 60 additions & 0 deletions pkg/proto/configuration/global/global.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
syntax = "proto3";

package buildbarn.configuration.global;

import "google/protobuf/duration.proto";

option go_package = "github.com/buildbarn/bb-storage/pkg/proto/configuration/global";

message JaegerConfiguration {
// Jaeger agent endpoint.
string agent_endpoint = 1;

// Jaeger collector endpoint.
string collector_endpoint = 2;

// OpenTracing service name.
string service_name = 3;

// Whether or not all traces should be sampled.
bool always_sample = 4;
}

message BasicAuthenticationConfiguration {
// Username to store in the "Authorization: Basic" header.
string username = 1;

// Password to store in the "Authorization: Basic" header.
string password = 2;
}

message PrometheusPushgatewayConfiguration {
// URL of the Prometheus Pushgateway server. Do not include the
// "/metrics/jobs/..." part in the URL.
string url = 1;

// Name of the job to announce to the Prometheus Pushgateway.
string job = 2;

// If set, enable the use of HTTP basic authentication.
BasicAuthenticationConfiguration basic_authentication = 3;

// Label pairs to use as the grouping key.
map<string, string> grouping = 4;

// Interval between metrics pushes.
google.protobuf.Duration push_interval = 5;
}

message Configuration {
// Jaeger configuration for tracing.
JaegerConfiguration jaeger = 1;

// Sets the runtime.SetMutexProfileFraction(), so that the HTTP debug
// endpoints used by pprof expose mutex profiling information.
int32 mutex_profile_fraction = 2;

// Periodically push metrics to a Prometheus Pushgateway, as opposed
// to letting the Prometheus server scrape the metrics.
PrometheusPushgatewayConfiguration prometheus_pushgateway = 3;
}

0 comments on commit fb0be44

Please sign in to comment.