Files
T
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

182 lines
5.9 KiB
Go

package main
import (
"fmt"
"path/filepath"
"strconv"
"github.com/langgenius/dify-plugin-daemon/cmd/commandline/bundle"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/bundle_entities"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
"github.com/spf13/cobra"
)
var (
bundleCreateCommand = &cobra.Command{
Use: "init",
Short: "Create a bundle",
Long: "Create a bundle",
Run: func(c *cobra.Command, args []string) {
bundle.InitBundle()
},
}
bundleAnalyzeCommand = &cobra.Command{
Use: "analyze [bundle_path]",
Short: "List all dependencies",
Long: "List all dependencies in the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
bundle.ListDependencies(bundlePath)
},
}
bundleAppendDependencyCommand = &cobra.Command{
Use: "append",
Short: "Append a dependency",
Long: "Append a dependency",
}
bundleAppendGithubDependencyCommand = &cobra.Command{
Use: "github [bundle_path]",
Short: "Append a github dependency",
Long: "Append a github dependency to the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
repoPattern := c.Flag("repo_pattern").Value.String()
githubPattern, err := bundle_entities.NewGithubRepoPattern(repoPattern)
if err != nil {
log.Error("invalid github repo pattern", "error", err)
return
}
bundle.AddGithubDependency(bundlePath, githubPattern)
},
}
bundleAppendMarketplaceDependencyCommand = &cobra.Command{
Use: "marketplace [bundle_path]",
Short: "Append a marketplace dependency",
Long: "Append a marketplace dependency to the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
marketplacePatternString := c.Flag("marketplace_pattern").Value.String()
marketplacePattern, err := bundle_entities.NewMarketplacePattern(marketplacePatternString)
if err != nil {
log.Error("invalid marketplace pattern", "error", err)
return
}
bundle.AddMarketplaceDependency(bundlePath, marketplacePattern)
},
}
bundleAppendPackageDependencyCommand = &cobra.Command{
Use: "package [bundle_path]",
Short: "Append a local package dependency",
Long: "Append a local package dependency to the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
packagePath := c.Flag("package_path").Value.String()
bundle.AddPackageDependency(bundlePath, packagePath)
},
}
bundleRegenerateCommand = &cobra.Command{
Use: "regenerate [bundle_path]",
Short: "Regenerate the bundle",
Long: "Regenerate the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
bundle.RegenerateBundle(bundlePath)
},
}
bundleRemoveDependencyCommand = &cobra.Command{
Use: "remove [bundle_path]",
Short: "Remove a dependency",
Long: "Remove a dependency from the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
index := c.Flag("index").Value.String()
indexInt, err := strconv.Atoi(index)
if err != nil {
log.Error("invalid index", "error", err)
return
}
bundle.RemoveDependency(bundlePath, indexInt)
},
}
bundleBumpVersionCommand = &cobra.Command{
Use: "bump [bundle_path]",
Short: "Bump the version of the bundle",
Long: "Bump the version of the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
targetVersion := c.Flag("target_version").Value.String()
bundle.BumpVersion(bundlePath, targetVersion)
},
}
bundlePackageCommand = &cobra.Command{
Use: "package [bundle_path]",
Short: "Package the bundle",
Long: "Package the specified bundle",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bundlePath := args[0]
// using filename of input_path as output_path if not specified
outputPath := ""
if c.Flag("output_path").Value.String() != "" {
outputPath = c.Flag("output_path").Value.String()
} else {
base := filepath.Base(bundlePath)
if base == "." || base == "/" {
fmt.Println("Error: invalid input path, you should specify the path outside of bundle directory")
return
}
outputPath = base + ".difybndl"
}
bundle.PackageBundle(bundlePath, outputPath)
},
}
)
func init() {
bundleCommand.AddCommand(bundleCreateCommand)
bundleCommand.AddCommand(bundleAppendDependencyCommand)
bundleAppendDependencyCommand.AddCommand(bundleAppendGithubDependencyCommand)
bundleAppendDependencyCommand.AddCommand(bundleAppendMarketplaceDependencyCommand)
bundleAppendDependencyCommand.AddCommand(bundleAppendPackageDependencyCommand)
bundleCommand.AddCommand(bundleRemoveDependencyCommand)
bundleCommand.AddCommand(bundleRegenerateCommand)
bundleCommand.AddCommand(bundleBumpVersionCommand)
bundleCommand.AddCommand(bundlePackageCommand)
bundleCommand.AddCommand(bundleAnalyzeCommand)
bundleAppendGithubDependencyCommand.Flags().StringP("repo_pattern", "r", "", "github repo pattern")
bundleAppendGithubDependencyCommand.MarkFlagRequired("repo_pattern")
bundleAppendMarketplaceDependencyCommand.Flags().StringP("marketplace_pattern", "m", "", "marketplace pattern")
bundleAppendMarketplaceDependencyCommand.MarkFlagRequired("marketplace_pattern")
bundleAppendPackageDependencyCommand.Flags().StringP("package_path", "p", "", "path to the package")
bundleAppendPackageDependencyCommand.MarkFlagRequired("package_path")
bundleRemoveDependencyCommand.Flags().StringP("index", "i", "", "index of the dependency")
bundleRemoveDependencyCommand.MarkFlagRequired("index")
bundleBumpVersionCommand.Flags().StringP("target_version", "t", "", "target version")
bundleBumpVersionCommand.MarkFlagRequired("target_version")
bundlePackageCommand.Flags().StringP("output_path", "o", "", "output path")
}