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

looplab / eventhorizon / 12622512440

05 Jan 2025 07:58PM UTC coverage: 27.495% (-39.9%) from 67.361%
12622512440

Pull #419

github

web-flow
Merge b3c17d928 into ac3a97277
Pull Request #419: fix(ci): update to up/download-artifact v4

1769 of 6434 relevant lines covered (27.49%)

1.41 hits per line

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

0.0
/middleware/eventhandler/observer/middleware.go
1
// Copyright (c) 2020 - The Event Horizon authors.
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

15
package observer
16

17
import (
18
        "fmt"
19
        "os"
20

21
        eh "github.com/looplab/eventhorizon"
22
        "github.com/looplab/eventhorizon/uuid"
23
)
24

25
// Group provides groupings of observers by different criteria.
26
type Group interface {
27
        // Group returns the name of the observer group.
28
        Group() string
29
}
30

31
// group is a string based group that resolves at creation.
32
type group string
33

34
// Group implements the Group method of the Group interface.
35
func (g group) Group() string {
×
36
        return string(g)
×
37
}
×
38

39
// NamedGroup returns a Group with a fixed name.
40
func NamedGroup(name string) Group {
×
41
        return group(name)
×
42
}
×
43

44
// UUIDGroup returns a Group with a fixed UUID.
45
func UUIDGroup(id uuid.UUID) Group {
×
46
        return group(id.String())
×
47
}
×
48

49
// RandomGroup returns a Group from a random UUID, useful to have many completely
50
// separate observers.
51
func RandomGroup() Group {
×
52
        return group(uuid.New().String())
×
53
}
×
54

55
// HostnameGroup returns a Group for the hostname, useful to have an observer per
56
// host.
57
func HostnameGroup() Group {
×
58
        hostname, err := os.Hostname()
×
59
        if err != nil {
×
60
                panic("could not get hostname for HostnameGroup()")
×
61
        }
62

63
        return group(hostname)
×
64
}
65

66
type eventHandler struct {
67
        eh.EventHandler
68
        handlerType eh.EventHandlerType
69
}
70

71
// HandlerType implements the HandlerType method of the EventHandler.
72
func (h *eventHandler) HandlerType() eh.EventHandlerType {
×
73
        return h.handlerType
×
74
}
×
75

76
// InnerHandler implements MiddlewareChain
77
func (h *eventHandler) InnerHandler() eh.EventHandler {
×
78
        return h.EventHandler
×
79
}
×
80

81
// NewMiddleware creates a middleware that lets multiple handlers handle an event
82
// depending on their group. It works by suffixing the group name to the handler type.
83
// To create an observer that is unique for every added handler use the RandomGroup.
84
// To create an observer per host use the HostnameGroup.
85
// To create handling groups manually use either the NamedGroup or UUIDGroup.
86
func NewMiddleware(group Group) func(eh.EventHandler) eh.EventHandler {
×
87
        return func(h eh.EventHandler) eh.EventHandler {
×
88
                return &eventHandler{h, h.HandlerType() + eh.EventHandlerType(fmt.Sprintf("_%s", group.Group()))}
×
89
        }
×
90
}
91

92
// Middleware creates an observer middleware with a random group.
93
func Middleware(h eh.EventHandler) eh.EventHandler {
×
94
        return &eventHandler{h, h.HandlerType() + eh.EventHandlerType(fmt.Sprintf("_%s", RandomGroup().Group()))}
×
95
}
×
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

© 2026 Coveralls, Inc