• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1886556705

24 Jun 2025 12:11PM UTC coverage: 66.403% (+0.7%) from 65.731%
1886556705

Pull #751

gitlab-ci

mzedel
feat(gui): let UI aggregate all device software again - not just rootfs info

- due to the continued absence of the reporting backend this is re-introduced, but now in combination with the report scoped device retrieval

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #751: ME-542 - device reports per widget only

29581 of 44548 relevant lines covered (66.4%)

1.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.38
/backend/pkg/requestid/middleware.go
1
// Copyright 2024 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package requestid
15

16
import (
17
        "github.com/ant0ine/go-json-rest/rest"
18
        "github.com/gin-gonic/gin"
19
        "github.com/google/uuid"
20

21
        "github.com/mendersoftware/mender-server/pkg/log"
22
        "github.com/mendersoftware/mender-server/pkg/requestlog"
23
)
24

25
const RequestIdHeader = "X-MEN-RequestID"
26

27
type MiddlewareOptions struct {
28
        // GenerateRequestID decides whether a request ID should
29
        // be generated when none exists. (default: true)
30
        GenerateRequestID *bool
31
}
32

33
func NewMiddlewareOptions() *MiddlewareOptions {
6✔
34
        return new(MiddlewareOptions)
6✔
35
}
6✔
36

37
func (opt *MiddlewareOptions) SetGenerateRequestID(gen bool) *MiddlewareOptions {
6✔
38
        opt.GenerateRequestID = &gen
6✔
39
        return opt
6✔
40
}
6✔
41

42
// Middleware provides requestid middleware for the gin-gonic framework.
43
func Middleware(opts ...*MiddlewareOptions) gin.HandlerFunc {
6✔
44
        opt := NewMiddlewareOptions().
6✔
45
                SetGenerateRequestID(true)
6✔
46
        for _, o := range opts {
6✔
47
                if o == nil {
×
48
                        continue
×
49
                }
50
                if o.GenerateRequestID != nil {
×
51
                        opt.GenerateRequestID = o.GenerateRequestID
×
52
                }
×
53
        }
54
        return func(c *gin.Context) {
12✔
55
                ctx := c.Request.Context()
6✔
56

6✔
57
                requestID := c.GetHeader(RequestIdHeader)
6✔
58
                if requestID == "" && *opt.GenerateRequestID {
12✔
59
                        uid, _ := uuid.NewRandom()
6✔
60
                        requestID = uid.String()
6✔
61
                }
6✔
62
                ctx = WithContext(ctx, requestID)
6✔
63

6✔
64
                logger := log.FromContext(ctx)
6✔
65
                if logger != nil {
12✔
66
                        logger = logger.F(log.Ctx{"request_id": requestID})
6✔
67
                        ctx = log.WithContext(ctx, logger)
6✔
68
                }
6✔
69
                c.Header(RequestIdHeader, requestID)
6✔
70
                c.Request = c.Request.WithContext(ctx)
6✔
71
        }
72
}
73

74
// RequestIdMiddleware sets the X-MEN-RequestID header if it's not present,
75
// and adds the request id to the request logger's context.
76
type RequestIdMiddleware struct {
77
}
78

79
// MiddlewareFunc makes RequestIdMiddleware implement the Middleware interface.
80
func (mw *RequestIdMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc {
3✔
81
        return func(w rest.ResponseWriter, r *rest.Request) {
6✔
82
                logger := requestlog.GetRequestLogger(r)
3✔
83

3✔
84
                reqId := r.Header.Get(RequestIdHeader)
3✔
85
                if reqId == "" {
6✔
86
                        uid, _ := uuid.NewRandom()
3✔
87
                        reqId = uid.String()
3✔
88
                }
3✔
89

90
                r = SetReqId(r, reqId)
3✔
91

3✔
92
                // enrich log context
3✔
93
                if logger != nil {
6✔
94
                        logger = logger.F(log.Ctx{"request_id": reqId})
3✔
95
                        r = requestlog.SetRequestLogger(r, logger)
3✔
96
                }
3✔
97

98
                //return the reuqest ID in response too, the client can log it
99
                //for end-to-end req tracing
100
                w.Header().Add(RequestIdHeader, reqId)
3✔
101

3✔
102
                h(w, r)
3✔
103
        }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc