Skip to content
This repository has been archived by the owner on Feb 28, 2025. It is now read-only.

Stream agent logs remotely #1774

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
47f8f33
add agent log streaming
Aug 17, 2023
c5ad6b4
add proto handler tests
Oct 6, 2023
7913bef
follow logs with nested streaming
Oct 10, 2023
cffdfaa
configure plugin loggers with agent id
Oct 16, 2023
e116eca
ensure atomic writes
Oct 16, 2023
be5de83
attempt to fix data race
Oct 18, 2023
0f25210
fix plugin logger forwarding
Oct 18, 2023
5cf5963
forward plugin logs without hclog
Nov 6, 2023
82bf9c0
fix lock synchronization
Nov 6, 2023
f868eab
add command flag completion
Nov 10, 2023
16a3628
remove plugin loggers outside plugins
Nov 14, 2023
dbcf761
fix plugin name colors
Nov 16, 2023
f68af02
setup context plugin logger
Nov 16, 2023
53bf78e
move aiops plugin lg to ctx
Nov 17, 2023
213d660
move alerting plugin lg to ctx
Nov 17, 2023
702efce
move logging plugin lg to ctx
Nov 17, 2023
e50be00
move metrics plugin lg to ctx
Nov 17, 2023
61ab617
move slo plugin lg to ctx
Nov 17, 2023
bea0d56
move topology plugin lg to ctx
Nov 17, 2023
9822a82
move remaining plugin lg to ctx
Nov 17, 2023
5743999
init plugin loggers with module name instead of agent id
Nov 17, 2023
31256e2
store mode in plugin context
Nov 22, 2023
5ab672e
separate test plugin logs by agent id
Nov 23, 2023
def5fd0
group related functions together
Nov 23, 2023
e2e7aa0
set otel tracing logger to error level
Nov 27, 2023
a389903
share lock among log file writers
Nov 27, 2023
9e6a6a5
attempt to fix web test data race
Nov 28, 2023
84223d0
fixup ctx data races
Nov 29, 2023
8a04996
init global gateway plugin loggers without streaming
Nov 29, 2023
a1b1fb5
close log streaming file on agent restart
Nov 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ replace (
// TODO: Update when https://github.com/grafana-operator/grafana-operator/pull/1301/files is merged
github.com/grafana-operator/grafana-operator/v5 => github.com/jaehnri/grafana-operator/v5 v5.0.0-20231107224428-8e66ce068452
// https://github.com/hashicorp/go-plugin/pull/251
github.com/hashicorp/go-plugin => github.com/alexandreLamarre/go-plugin v0.1.1-0.20230417174342-eab684801be5
github.com/hashicorp/go-plugin => github.com/alexandreLamarre/go-plugin v0.0.0-20231106165427-ede0fcce2acc
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza => github.com/rancher-sandbox/otel-collector-contrib/pkg/stanza v0.0.0-20231108205431-9f8e6699a9d4
github.com/opensearch-project/opensearch-go/v2 => github.com/dbason/opensearch-go/v2 v2.0.0-20221202021211-6aec8f80bc41
github.com/rancher/charts-build-scripts => github.com/dbason/charts-build-scripts v0.3.4-0.20220429024555-807c076e8116
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alexandreLamarre/go-plugin v0.1.1-0.20230417174342-eab684801be5 h1:VByWr0MtaW/iS+MncS+5E8lCi2Hgc3JHVnMD6bCCwEk=
github.com/alexandreLamarre/go-plugin v0.1.1-0.20230417174342-eab684801be5/go.mod h1:6/1TEzT0eQznvI/gV2CM29DLSkAK/e58mUWKVsPaph0=
github.com/alexandreLamarre/go-plugin v0.0.0-20231106165427-ede0fcce2acc h1:b7BMoL6UF6kRTW3apg0XJmIG039TExK9arOKSOIY7hA=
github.com/alexandreLamarre/go-plugin v0.0.0-20231106165427-ede0fcce2acc/go.mod h1:1BphuLk8r4UASxJBCggGswD3Wn84QLOBANVCAdNoeoY=
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO85qeSydJtItA4T55Pw6BtAejd0APRJOCE=
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.30.4 h1:8S4/o1/KoUArAGbGwPxcwf0krlzceva2XVOSchFS7Eo=
Expand Down
2 changes: 1 addition & 1 deletion internal/alerting/syncer/syncer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewAlertingSyncerV1(
init.Store(false)
server := &AlertManagerSyncerV1{
serverConfig: serverConfig,
lg: logger.NewPluginLogger().WithGroup("alerting-syncer"),
lg: logger.New().WithGroup("alerting-syncer"),
tlsConfig: tlsConfig,
}
go func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/agent/v1/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (

type Agent struct {
controlv1.UnimplementedHealthServer
controlv1.UnimplementedLogServer
AgentOptions
config v1beta1.AgentConfigSpec
router *gin.Engine
Expand Down
43 changes: 29 additions & 14 deletions pkg/agent/v2/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/rancher/opni/pkg/ident/identserver"
"github.com/rancher/opni/pkg/keyring"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/logger/remotelogs"
"github.com/rancher/opni/pkg/machinery"
"github.com/rancher/opni/pkg/plugins"
"github.com/rancher/opni/pkg/plugins/apis/apiextensions"
Expand Down Expand Up @@ -118,11 +119,28 @@ func WithRebootstrap(rebootstrap bool) AgentOption {
func New(ctx context.Context, conf *v1beta1.AgentConfig, opts ...AgentOption) (*Agent, error) {
options := AgentOptions{}
options.apply(opts...)

initCtx, initCancel := context.WithTimeout(ctx, 10*time.Second)
defer initCancel()

ipBuilder, err := ident.GetProviderBuilder(conf.Spec.IdentityProvider)
if err != nil {
return nil, fmt.Errorf("configuration error: %w", err)
}
ip := ipBuilder()
id, err := ip.UniqueIdentifier(initCtx)
if err != nil {
return nil, fmt.Errorf("error getting unique identifier: %w", err)
}

level := logger.DefaultLogLevel.Level()
if conf.Spec.LogLevel != "" {
level = logger.ParseLevel(conf.Spec.LogLevel)
}
lg := logger.New(logger.WithLogLevel(level)).WithGroup("agent")

ctx = logger.WithAgentId(ctx, id)
ctx = logger.WithMode(ctx, meta.ModeAgent)
lg := logger.New(logger.WithLogLevel(level), logger.WithFileWriter(logger.WriteOnlyFile(logger.GetLogFileName(id)))).WithGroup("agent")
lg.Debug(fmt.Sprintf("using log level: %s", level.String()))

var pl *plugins.PluginLoader
Expand Down Expand Up @@ -198,19 +216,6 @@ func New(ctx context.Context, conf *v1beta1.AgentConfig, opts ...AgentOption) (*
return nil, fmt.Errorf("agent upgrade configuration error: %w", err)
}

initCtx, initCancel := context.WithTimeout(ctx, 10*time.Second)
defer initCancel()

ipBuilder, err := ident.GetProviderBuilder(conf.Spec.IdentityProvider)
if err != nil {
return nil, fmt.Errorf("configuration error: %w", err)
}
ip := ipBuilder()
id, err := ip.UniqueIdentifier(initCtx)
if err != nil {
return nil, fmt.Errorf("error getting unique identifier: %w", err)
}

sb, err := machinery.ConfigureStorageBackend(initCtx, &conf.Spec.Storage)
if err != nil {
return nil, fmt.Errorf("error configuring keyring store broker: %w", err)
Expand Down Expand Up @@ -312,6 +317,14 @@ func New(ctx context.Context, conf *v1beta1.AgentConfig, opts ...AgentOption) (*
gatewayClient.RegisterSplicedStream(cc, md.Filename())
}))

ls := remotelogs.NewLogServer()
controlv1.RegisterLogServer(gatewayClient, ls)

pl.Hook(hooks.OnLoadMC(func(lc controlv1.LogClient, m meta.PluginMeta, cc *grpc.ClientConn) {
client := controlv1.NewLogClient(cc)
ls.AddClient(m.Filename(), client)
}))

return &Agent{
AgentOptions: options,
config: conf.Spec,
Expand Down Expand Up @@ -395,6 +408,7 @@ func (a *Agent) ListenAndServe(ctx context.Context) error {
close(done)
}))

ctx = logger.WithAgentId(ctx, a.tenantID)
a.pluginLoader.LoadPlugins(ctx, a.config.PluginDir, plugins.AgentScheme,
plugins.WithManifest(pluginManifest),
)
Expand Down Expand Up @@ -493,6 +507,7 @@ ROUTES:

func (a *Agent) runGatewayClient(ctx context.Context) error {
lg := a.logger
defer logger.CloseLogStreaming(a.tenantID)
isRetry := false
for ctx.Err() == nil {
if isRetry {
Expand Down
2 changes: 1 addition & 1 deletion pkg/alerting/drivers/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import (
// },
// body: nil,
// values: nil,
// logger: logger.NewPluginLogger().Named("alerting"),
// logger: logger.New().Named("alerting"),
// }
// }

Expand Down
5 changes: 3 additions & 2 deletions pkg/alerting/extensions/embedded_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"errors"
"net/http"

"log/slog"

"github.com/rancher/opni/pkg/alerting/cache"
"github.com/rancher/opni/pkg/alerting/extensions/destination"
"github.com/rancher/opni/pkg/alerting/shared"
alertingv1 "github.com/rancher/opni/pkg/apis/alerting/v1"
"github.com/rancher/opni/pkg/logger"
"log/slog"

// add profiles
_ "net/http/pprof"
Expand Down Expand Up @@ -64,7 +65,7 @@ func StartOpniEmbeddedServer(
opniAddr string,
sendK8s bool,
) *http.Server {
lg := logger.NewPluginLogger().WithGroup("opni.alerting")
lg := logger.New().WithGroup("opni.alerting")
es := NewEmbeddedServer(lg, 125, sendK8s)
mux := http.NewServeMux()

Expand Down
2 changes: 1 addition & 1 deletion pkg/alerting/storage/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDefaultAlertingBroker(js nats.JetStreamContext, opts ...storage_opts.Cli
options := &storage_opts.ClientSetOptions{}
options.Apply(opts...)
if options.Logger == nil {
options.Logger = logger.NewPluginLogger().WithGroup("alerting-storage-client-set")
options.Logger = logger.New().WithGroup("alerting-storage-client-set")
}
if options.TrackerTtl == 0 {
options.TrackerTtl = defaultTrackerTTLV1
Expand Down
Loading