Files
Byron.wang ca3d00229e Feat/Implement structured logging and Trace ID propagation (#552)
* use slog instead of log package and format to new log schema

* update the environment name to LOG_OUTPUT_FORMAT

* add the env to .env.example

* fix log reference error

* change the order of milldlewares

* delete unused code

* fix the concurrently session potential race condition

* fix the log format in tests

* update the duplicate code

* refactor: convert log functions to slog structured format

- Change log.Error/Info/Warn/Debug/Panic to accept msg + key-value pairs
- Remove printf-style formatting from log functions
- Update log calls in internal/cluster, internal/db, internal/core/session_manager
- Remove unused 'initialized' variable from log package
- Remaining files will be updated in follow-up commits

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: update all log call sites to use slog structured format

Convert all log.Error, log.Info, log.Warn, log.Debug, and log.Panic
calls from printf-style formatting to slog key-value pairs.

Before: log.Error("failed to do something: %s", err.Error())
After:  log.Error("failed to do something", "error", err)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: update cmd/ log calls to use slog structured format

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat: implement GnetLogger for structured logging in gnet

* refactor: remove deprecated log visibility functions and related calls

* feat: enhance session management with trace and identity context propagation

* feat: implement serverless transaction handler and writer for plugin runtime

* refactor: rename context field to traceCtx in RealBackwardsInvocation

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Yeuoly <admin@srmxy.cn>
2025-12-30 11:00:48 +08:00

108 lines
2.3 KiB
Go

package debugging_runtime
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_transport"
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
"github.com/panjf2000/gnet/v2"
gnet_errors "github.com/panjf2000/gnet/v2/pkg/errors"
)
type RemotePluginServer struct {
server *DifyServer
}
type RemotePluginServerInterface interface {
Stop() error
Launch() error
}
// Stop stops the server
func (r *RemotePluginServer) Stop() error {
err := r.server.engine.Stop(context.Background())
if err == gnet_errors.ErrEmptyEngine || err == gnet_errors.ErrEngineInShutdown {
return nil
}
return err
}
// Launch starts the server
func (r *RemotePluginServer) Launch() error {
err := gnet.Run(
r.server, r.server.addr, gnet.WithMulticore(r.server.multicore),
gnet.WithNumEventLoop(r.server.numLoops),
gnet.WithLogger(GnetLogger{}),
)
if err != nil {
r.Stop()
}
// collect shutdown signal
go r.collectShutdownSignal()
return err
}
func (s *RemotePluginServer) collectShutdownSignal() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
<-c
// shutdown server
s.Stop()
}
// NewDebuggingPluginServer creates a new RemotePluginServer
func NewDebuggingPluginServer(
config *app.Config, media_transport *media_transport.MediaBucket,
) *RemotePluginServer {
addr := fmt.Sprintf(
"tcp://%s:%d",
config.PluginRemoteInstallingHost,
config.PluginRemoteInstallingPort,
)
multicore := true
s := &DifyServer{
mediaManager: media_transport,
addr: addr,
port: config.PluginRemoteInstallingPort,
multicore: multicore,
numLoops: config.PluginRemoteInstallServerEventLoopNums,
plugins: make(map[int]*RemotePluginRuntime),
pluginsLock: &sync.RWMutex{},
maxConn: int32(config.PluginRemoteInstallingMaxConn),
notifiers: []PluginRuntimeNotifier{},
notifierMutex: &sync.RWMutex{},
}
manager := &RemotePluginServer{
server: s,
}
return manager
}
// AddNotifier adds a notifier to the runtime
func (r *RemotePluginServer) AddNotifier(notifier PluginRuntimeNotifier) {
r.server.AddNotifier(notifier)
}
// WalkNotifiers walks through all the notifiers and calls the given function
func (r *RemotePluginServer) WalkNotifiers(fn func(notifier PluginRuntimeNotifier)) {
r.server.WalkNotifiers(fn)
}