Repository: dragonflyoss/Dragonfly2
Pull request #1404 (2022-06-21 08:01:11)

feat: dfdaemon upload and object storage service add middlewares
<!--- Provide a general summary of your changes in the Title above -->

## Description
- Dfdaemon upload and object storage service add middlewares.

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1404](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (7f9f58e) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/6f95427bf4a9feeeb96ed442921f8b662dedc4a6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (6f95427) will **decrease** coverage by `0.20%`.
> The diff coverage is `76.47%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main    #1404      +/-   ##
==========================================
- Coverage   45.17%   44.96%   -0.21%     
==========================================
  Files         110      110              
  Lines       10005    10018      +13     
==========================================
- Hits         4520     4505      -15     
- Misses       5020     5048      +28     
  Partials      465      465              
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| unittests | `44.96% <76.47%> (-0.21%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [client/daemon/daemon.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9kYWVtb24uZ28=) | `5.41% <0.00%> (ø)` | |
| [client/daemon/upload/upload\_manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi91cGxvYWQvdXBsb2FkX21hbmFnZXIuZ28=) | `57.14% <86.66%> (+5.58%)` | :arrow_up: |
| [client/daemon/peer/peertask\_piecetask\_poller.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19wb2xsZXIuZ28=) | `30.43% <0.00%> (-16.67%)` | :arrow_down: |
| [...ent/daemon/peer/peertask\_piecetask\_synchronizer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19zeW5jaHJvbml6ZXIuZ28=) | `45.45% <0.00%> (-2.17%)` | :arrow_down: |
| [client/daemon/peer/peertask\_conductor.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1404/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2NvbmR1Y3Rvci5nbw==) | `58.35% <0.00%> (+0.20%)` | :arrow_up: |


Patch:
diff --git a/client/daemon/daemon.go b/client/daemon/daemon.go
index 4827110d..fb022f2f 100644
--- a/client/daemon/daemon.go
+++ b/client/daemon/daemon.go
@@ -219,13 +219,13 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
 		return nil, err
 	}
 
-	uploadManager, err := upload.NewUploadManager(storageManager,
+	uploadManager, err := upload.NewUploadManager(opt, storageManager, d.LogDir(),
 		upload.WithLimiter(rate.NewLimiter(opt.Upload.RateLimit.Limit, int(opt.Upload.RateLimit.Limit))))
 	if err != nil {
 		return nil, err
 	}
 
-	objectStorage, err := objectstorage.New(opt, dynconfig, peerTaskManager, storageManager)
+	objectStorage, err := objectstorage.New(opt, dynconfig, peerTaskManager, storageManager, d.LogDir())
 	if err != nil {
 		return nil, err
 	}
diff --git a/client/daemon/objectstorage/objectstorage.go b/client/daemon/objectstorage/objectstorage.go
index b3d9fb4e..ed33c21f 100644
--- a/client/daemon/objectstorage/objectstorage.go
+++ b/client/daemon/objectstorage/objectstorage.go
@@ -19,15 +19,20 @@ package objectstorage
 import (
 	"context"
 	"errors"
+	"io"
 	"math"
 	"net"
 	"net/http"
+	"os"
+	"path/filepath"
 	"strconv"
 	"strings"
 	"time"
 
 	"github.com/gin-gonic/gin"
 	"github.com/go-http-utils/headers"
+	ginprometheus "github.com/mcuadros/go-gin-prometheus"
+	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
 
 	"d7y.io/dragonfly/v2/client/clientutil"
 	"d7y.io/dragonfly/v2/client/config"
@@ -37,6 +42,13 @@ import (
 	"d7y.io/dragonfly/v2/pkg/rpc/base"
 )
 
+const (
+	PrometheusSubsystemName = "dragonfly_dfdaemon_object_stroage"
+	OtelServiceName         = "dragonfly-dfdaemon-object-storage"
+)
+
+var GinLogFileName = "gin-object-stroage.log"
+
 const (
 	// defaultSignExpireTime is default expire of sign url.
 	defaultSignExpireTime = 5 * time.Minute
@@ -61,7 +73,7 @@ type objectStorage struct {
 }
 
 // New returns a new ObjectStorage instence.
-func New(cfg *config.DaemonOption, dynconfig config.Dynconfig, peerTaskManager peer.TaskManager, storageManager storage.Manager) (ObjectStorage, error) {
+func New(cfg *config.DaemonOption, dynconfig config.Dynconfig, peerTaskManager peer.TaskManager, storageManager storage.Manager, logDir string) (ObjectStorage, error) {
 	o := &objectStorage{
 		dynconfig:       dynconfig,
 		peerTaskManager: peerTaskManager,
@@ -69,7 +81,7 @@ func New(cfg *config.DaemonOption, dynconfig config.Dynconfig, peerTaskManager p
 		peerIDGenerator: peer.NewPeerIDGenerator(cfg.Host.AdvertiseIP),
 	}
 
-	router := o.initRouter()
+	router := o.initRouter(cfg, logDir)
 	o.Server = &http.Server{
 		Handler: router,
 	}
@@ -88,8 +100,34 @@ func (o *objectStorage) Stop() error {
 }
 
 // Initialize router of gin.
-func (o *objectStorage) initRouter() *gin.Engine {
-	r := gin.Default()
+func (o *objectStorage) initRouter(cfg *config.DaemonOption, logDir string) *gin.Engine {
+	// Set mode
+	if !cfg.Verbose {
+		gin.SetMode(gin.ReleaseMode)
+	}
+
+	// Logging to a file
+	if !cfg.Console {
+		gin.DisableConsoleColor()
+		logDir := filepath.Join(logDir, "daemon")
+		f, _ := os.Create(filepath.Join(logDir, GinLogFileName))
+		gin.DefaultWriter = io.MultiWriter(f)
+	}
+
+	r := gin.New()
+
+	// Middleware
+	r.Use(gin.Logger())
+	r.Use(gin.Recovery())
+
+	// Prometheus metrics
+	p := ginprometheus.NewPrometheus(PrometheusSubsystemName)
+	p.Use(r)
+
+	// Opentelemetry
+	if cfg.Options.Telemetry.Jaeger != "" {
+		r.Use(otelgin.Middleware(OtelServiceName))
+	}
 
 	// Health Check.
 	r.GET("/healthy", o.getHealth)
diff --git a/client/daemon/upload/upload_manager.go b/client/daemon/upload/upload_manager.go
index 570096f4..477153e5 100644
--- a/client/daemon/upload/upload_manager.go
+++ b/client/daemon/upload/upload_manager.go
@@ -23,16 +23,28 @@ import (
 	"math"
 	"net"
 	"net/http"
+	"os"
+	"path/filepath"
 
 	"github.com/gin-gonic/gin"
 	"github.com/go-http-utils/headers"
+	ginprometheus "github.com/mcuadros/go-gin-prometheus"
+	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
 	"golang.org/x/time/rate"
 
 	"d7y.io/dragonfly/v2/client/clientutil"
+	"d7y.io/dragonfly/v2/client/config"
 	"d7y.io/dragonfly/v2/client/daemon/storage"
 	logger "d7y.io/dragonfly/v2/internal/dflog"
 )
 
+const (
+	PrometheusSubsystemName = "dragonfly_dfdaemon_upload"
+	OtelServiceName         = "dragonfly-dfdaemon-upload"
+)
+
+var GinLogFileName = "gin-upload.log"
+
 // Manager is the interface used for upload task.
 type Manager interface {
 	// Started upload manager server.
@@ -60,12 +72,12 @@ func WithLimiter(limiter *rate.Limiter) func(*uploadManager) {
 }
 
 // New returns a new Manager instence.
-func NewUploadManager(storageManager storage.Manager, opts ...Option) (Manager, error) {
+func NewUploadManager(cfg *config.DaemonOption, storageManager storage.Manager, logDir string, opts ...Option) (Manager, error) {
 	um := &uploadManager{
 		storageManager: storageManager,
 	}
 
-	router := um.initRouter()
+	router := um.initRouter(cfg, logDir)
 	um.Server = &http.Server{
 		Handler: router,
 	}
@@ -88,8 +100,34 @@ func (um *uploadManager) Stop() error {
 }
 
 // Initialize router of gin.
-func (um *uploadManager) initRouter() *gin.Engine {
-	r := gin.Default()
+func (um *uploadManager) initRouter(cfg *config.DaemonOption, logDir string) *gin.Engine {
+	// Set mode
+	if !cfg.Verbose {
+		gin.SetMode(gin.ReleaseMode)
+	}
+
+	// Logging to a file
+	if !cfg.Console {
+		gin.DisableConsoleColor()
+		logDir := filepath.Join(logDir, "daemon")
+		f, _ := os.Create(filepath.Join(logDir, GinLogFileName))
+		gin.DefaultWriter = io.MultiWriter(f)
+	}
+
+	r := gin.New()
+
+	// Middleware
+	r.Use(gin.Logger())
+	r.Use(gin.Recovery())
+
+	// Prometheus metrics
+	p := ginprometheus.NewPrometheus(PrometheusSubsystemName)
+	p.Use(r)
+
+	// Opentelemetry
+	if cfg.Options.Telemetry.Jaeger != "" {
+		r.Use(otelgin.Middleware(OtelServiceName))
+	}
 
 	// Health Check.
 	r.GET("/healthy", um.getHealth)


Test patch:
diff --git a/client/daemon/upload/upload_manager_test.go b/client/daemon/upload/upload_manager_test.go
index 834ea77c..56957e07 100644
--- a/client/daemon/upload/upload_manager_test.go
+++ b/client/daemon/upload/upload_manager_test.go
@@ -30,6 +30,7 @@ import (
 	testifyassert "github.com/stretchr/testify/assert"
 	"golang.org/x/time/rate"
 
+	"d7y.io/dragonfly/v2/client/config"
 	"d7y.io/dragonfly/v2/client/daemon/storage"
 	"d7y.io/dragonfly/v2/client/daemon/test"
 	mock_storage "d7y.io/dragonfly/v2/client/daemon/test/mock/storage"
@@ -55,7 +56,7 @@ func TestUploadManager_Serve(t *testing.T) {
 				io.NopCloser(nil), nil
 		})
 
-	um, err := NewUploadManager(mockStorageManager, WithLimiter(rate.NewLimiter(16*1024, 16*1024)))
+	um, err := NewUploadManager(config.NewDaemonConfig(), mockStorageManager, os.TempDir(), WithLimiter(rate.NewLimiter(16*1024, 16*1024)))
 	assert.Nil(err, "NewUploadManager")
 
 	listen, err := net.Listen("tcp4", "127.0.0.1:0")