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

69 lines
2.2 KiB
Go

package debugging_runtime
import (
"encoding/json"
"errors"
"github.com/langgenius/dify-plugin-daemon/internal/core/io_tunnel/access_types"
"github.com/langgenius/dify-plugin-daemon/internal/types/exception"
"github.com/langgenius/dify-plugin-daemon/pkg/entities"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
routinepkg "github.com/langgenius/dify-plugin-daemon/pkg/routine"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/parser"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/routine"
"github.com/panjf2000/gnet/v2"
)
func (r *RemotePluginRuntime) Listen(sessionId string) (*entities.Broadcast[plugin_entities.SessionMessage], error) {
listener := entities.NewCallbackHandler[plugin_entities.SessionMessage]()
listener.OnClose(func() {
// execute in new goroutine to avoid deadlock
routine.Submit(routinepkg.Labels{
routinepkg.RoutineLabelKeyModule: "debugging_runtime",
routinepkg.RoutineLabelKeyMethod: "removeMessageCallbackHandler",
}, func() {
r.removeMessageCallbackHandler(sessionId)
r.removeSessionMessageCloser(sessionId)
})
})
// add session message closer to avoid unexpected connection closed
r.addSessionMessageCloser(sessionId, func() {
listener.Send(plugin_entities.SessionMessage{
Type: plugin_entities.SESSION_MESSAGE_TYPE_ERROR,
Data: json.RawMessage(parser.MarshalJson(plugin_entities.ErrorResponse{
ErrorType: exception.PluginConnectionClosedError,
Message: "Connection closed unexpectedly",
Args: map[string]any{},
})),
})
})
r.addMessageCallbackHandler(sessionId, func(data []byte) {
// unmarshal the session message
chunk, err := parser.UnmarshalJsonBytes[plugin_entities.SessionMessage](data)
if err != nil {
log.Error("unmarshal json failed, failed to parse session message", "error", err)
return
}
listener.Send(chunk)
})
return listener, nil
}
func (r *RemotePluginRuntime) Write(
sessionId string,
action access_types.PluginAccessAction,
data []byte,
) error {
if r.conn == nil {
return errors.New("connection not established")
}
return r.conn.AsyncWrite(append(data, '\n'), func(c gnet.Conn, err error) error {
return err
})
}