forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
123227: scripts/drtprod: send logs to datadog r=sudomateo a=sudomateo Previously, clusters created by `roachprod` logged exclusively to disk, requiring operators to either SSH into the instance or use `roachprod logs` to view logs for a CockroachDB node. This patch adds a new `roachprod fluent-bit-start` command that, when run, installs and starts Fluent Bit on the CockroachDB cluster listening on `127.0.0.1:5170`. The CockroachDB logging configuration has also been updated to log to this Fluent Bit endpoint, choosing not to error if the endpoint is unavailble. Clusters still log to disk as to not break existing workflows. The `drtprod` script was also updated to install and configure Fluent Bit on the DRT clusters. A complementary `roachprod fluent-bit-stop` command was also added to stop Fluent Bit. Epic: none Release note: none Co-authored-by: Matthew Sanabria <[email protected]>
- Loading branch information
Showing
15 changed files
with
411 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "fluentbit", | ||
srcs = ["fluentbit.go"], | ||
embedsrcs = [ | ||
"files/fluent-bit.service", | ||
"files/fluent-bit.yaml.tmpl", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/roachprod/fluentbit", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/roachprod/install", | ||
"//pkg/roachprod/logger", | ||
"//pkg/roachprod/vm", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[Unit] | ||
Description=Fluent Bit | ||
Documentation=https://docs.fluentbit.io/manual/ | ||
Requires=network.target | ||
After=network.target | ||
|
||
[Service] | ||
Type=simple | ||
EnvironmentFile=-/etc/sysconfig/fluent-bit | ||
EnvironmentFile=-/etc/default/fluent-bit | ||
ExecStart=/opt/fluent-bit/bin/fluent-bit -c //etc/fluent-bit/fluent-bit.yaml | ||
Restart=always | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
service: | ||
flush: 1 | ||
daemon: off | ||
http_server: on | ||
http_listen: 127.0.0.1 | ||
http_port: 2020 | ||
log_level: info | ||
storage.path: /tmp | ||
storage.metrics: on | ||
storage.max_chunks_up: 30 | ||
storage.sync: full | ||
storage.checksum: on | ||
storage.delete_irrecoverable_chunks: on | ||
parsers_file: parsers.conf | ||
plugins_file: plugins.conf | ||
pipeline: | ||
inputs: | ||
- name: tcp | ||
tag: cockroachdb | ||
listen: 127.0.0.1 | ||
port: 5170 | ||
format: json | ||
storage.type: filesystem | ||
alias: cockroachdb | ||
outputs: | ||
- name: datadog | ||
match: cockroachdb | ||
host: http-intake.logs.{{ .DatadogSite }} | ||
tls: on | ||
compress: gzip | ||
apikey: {{ .DatadogAPIKey }} | ||
dd_source: cockroachdb | ||
dd_service: {{ .DatadogService }} | ||
dd_tags: {{ join .Tags `,` }} | ||
alias: cockroachdb | ||
storage.total_limit_size: 25MB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package fluentbit | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
_ "embed" | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/roachprod/install" | ||
"github.com/cockroachdb/cockroach/pkg/roachprod/logger" | ||
"github.com/cockroachdb/cockroach/pkg/roachprod/vm" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
//go:embed files/fluent-bit.yaml.tmpl | ||
var fluentBitTemplate string | ||
|
||
//go:embed files/fluent-bit.service | ||
var fluentBitSystemdUnit string | ||
|
||
// Config represents the information needed to configure and run Fluent Bit on | ||
// a CockroachDB cluster. | ||
type Config struct { | ||
// Datadog site to send telemetry data to (e.g, us5.datadoghq.com). | ||
DatadogSite string | ||
|
||
// Datadog API key to authenticate to Datadog. | ||
DatadogAPIKey string | ||
|
||
// Datadog service for emitted logs. | ||
DatadogService string | ||
|
||
// Datadog team to tag the emitted logs. | ||
DatadogTeam string | ||
} | ||
|
||
// Install installs, configures, and starts Fluent Bit on the given CockroachDB | ||
// cluster c. | ||
func Install(ctx context.Context, l *logger.Logger, c *install.SyncedCluster, config Config) error { | ||
if err := c.Parallel(ctx, l, install.WithNodes(c.Nodes), func(ctx context.Context, node install.Node) (*install.RunResultDetails, error) { | ||
res := &install.RunResultDetails{Node: node} | ||
|
||
if err := install.InstallTool(ctx, l, c, install.Nodes{node}, "fluent-bit", l.Stdout, l.Stderr); err != nil { | ||
res.Err = errors.Wrap(err, "failed installing fluent bit") | ||
return res, res.Err | ||
} | ||
|
||
tags := []string{ | ||
"env:development", | ||
fmt.Sprintf("host:%s", vm.Name(c.Name, int(node))), | ||
fmt.Sprintf("cluster:%s", c.Name), | ||
} | ||
|
||
if config.DatadogTeam != "" { | ||
tags = append(tags, fmt.Sprintf("team:%s", config.DatadogTeam)) | ||
} | ||
|
||
data := templateData{ | ||
DatadogSite: config.DatadogSite, | ||
DatadogAPIKey: config.DatadogAPIKey, | ||
DatadogService: config.DatadogService, | ||
Tags: tags, | ||
} | ||
|
||
fluentBitConfig, err := executeTemplate(data) | ||
if err != nil { | ||
res.Err = errors.Wrapf(err, "failed rendering fluent bit configuration for node %d", node) | ||
return res, res.Err | ||
} | ||
|
||
if err := c.PutString(ctx, l, install.Nodes{node}, fluentBitConfig, "/tmp/fluent-bit.yaml", 0644); err != nil { | ||
res.Err = errors.Wrapf(err, "failed writing fluent bit configuration to node %d", node) | ||
return res, res.Err | ||
} | ||
|
||
if err := c.PutString(ctx, l, install.Nodes{node}, fluentBitSystemdUnit, "/tmp/fluent-bit.service", 0644); err != nil { | ||
res.Err = errors.Wrap(err, "failed writing fluent bit systemd unit file") | ||
return res, res.Err | ||
} | ||
|
||
if err := c.Run(ctx, l, l.Stdout, l.Stderr, install.WithNodes(install.Nodes{node}), "fluent-bit", ` | ||
sudo cp /tmp/fluent-bit.yaml /etc/fluent-bit/fluent-bit.yaml && rm /tmp/fluent-bit.yaml | ||
sudo cp /tmp/fluent-bit.service /etc/systemd/system/fluent-bit.service && rm /tmp/fluent-bit.service | ||
sudo systemctl daemon-reload && sudo systemctl enable fluent-bit && sudo systemctl restart fluent-bit | ||
`); err != nil { | ||
res.Err = errors.Wrap(err, "failed enabling and starting fluent bit service") | ||
return res, res.Err | ||
} | ||
|
||
return res, nil | ||
}); err != nil { | ||
return errors.Wrap(err, "failed starting fluent bit") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Stop stops a running Fluent Bit service on the given CockroachDB cluster c. | ||
func Stop(ctx context.Context, l *logger.Logger, c *install.SyncedCluster) error { | ||
if err := c.Run(ctx, l, l.Stdout, l.Stderr, install.WithNodes(c.Nodes).WithShouldRetryFn(install.AlwaysTrue), "fluent-bit-stop", ` | ||
sudo systemctl disable fluent-bit && sudo systemctl stop fluent-bit | ||
`); err != nil { | ||
return errors.Wrap(err, "failed stopping fluent bit") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type templateData struct { | ||
DatadogSite string | ||
DatadogAPIKey string | ||
DatadogService string | ||
Tags []string | ||
} | ||
|
||
func executeTemplate(data templateData) (string, error) { | ||
tpl, err := template.New("fluent-bit-config"). | ||
Funcs(template.FuncMap{ | ||
"join": strings.Join, | ||
}). | ||
Parse(fluentBitTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
var buf bytes.Buffer | ||
if err := tpl.Execute(&buf, data); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.