-
Notifications
You must be signed in to change notification settings - Fork 494
/
Copy pathevents.go
142 lines (125 loc) · 4.59 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eventhubs
import (
"context"
"strconv"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
"github.com/google/uuid"
"github.com/spf13/cast"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/pubsub"
)
// Type for the handler for messages coming in from the subscriptions.
type SubscribeHandler func(ctx context.Context, data []byte, metadata map[string]string) error
const (
// Event Hubs SystemProperties names for metadata passthrough.
sysPropSequenceNumber = "x-opt-sequence-number"
sysPropEnqueuedTime = "x-opt-enqueued-time"
sysPropOffset = "x-opt-offset"
sysPropPartitionID = "x-opt-partition-id"
sysPropPartitionKey = "x-opt-partition-key"
sysPropIotHubDeviceConnectionID = "iothub-connection-device-id"
sysPropIotHubAuthGenerationID = "iothub-connection-auth-generation-id"
sysPropIotHubConnectionAuthMethod = "iothub-connection-auth-method"
sysPropIotHubConnectionModuleID = "iothub-connection-module-id"
sysPropIotHubEnqueuedTime = "iothub-enqueuedtime"
sysPropMessageID = "message-id"
)
// Returns metadata from received azure eventhub message
func getMetadataFromEventData(e *azeventhubs.ReceivedEventData, getAllProperties bool) map[string]string {
// Allocate with an initial capacity of 10 which covers the common properties, also from IoT Hub
md := make(map[string]string, 10)
md[sysPropSequenceNumber] = strconv.FormatInt(e.SequenceNumber, 10)
if e.EnqueuedTime != nil {
md[sysPropEnqueuedTime] = e.EnqueuedTime.Format(time.RFC3339)
}
md[sysPropOffset] = strconv.FormatInt(e.Offset, 10)
if e.PartitionKey != nil {
md[sysPropPartitionKey] = *e.PartitionKey
}
if e.MessageID != nil && *e.MessageID != "" {
md[sysPropMessageID] = *e.MessageID
}
// Iterate through the system properties looking for those coming from IoT Hub
for k, v := range e.SystemProperties {
switch k {
// The following metadata properties are only present if event was generated by Azure IoT Hub.
case sysPropIotHubDeviceConnectionID,
sysPropIotHubAuthGenerationID,
sysPropIotHubConnectionAuthMethod,
sysPropIotHubConnectionModuleID,
sysPropIotHubEnqueuedTime:
addPropertyToMetadata(k, v, md)
default:
// nop
}
}
// Added properties if any (includes application properties from Azure IoT Hub)
if getAllProperties && len(e.Properties) > 0 {
for k, v := range e.Properties {
addPropertyToMetadata(k, v, md)
}
}
return md
}
// Returns bindings read response message from azure eventhub message
func NewBindingsReadResponseFromEventData(e *azeventhubs.ReceivedEventData, topic string, getAllProperties bool) (*bindings.ReadResponse, error) {
meta := getMetadataFromEventData(e, getAllProperties)
msg := &bindings.ReadResponse{
Data: e.Body,
Metadata: meta,
}
return msg, nil
}
// Returns a new pubsub message from azure eventhub message
func NewPubsubMessageFromEventData(e *azeventhubs.ReceivedEventData, topic string, getAllProperties bool) (*pubsub.NewMessage, error) {
meta := getMetadataFromEventData(e, getAllProperties)
msg := &pubsub.NewMessage{
Data: e.Body,
Topic: topic,
Metadata: meta,
}
return msg, nil
}
// Returns a new bulk pubsub message entry from azure eventhub message
func NewBulkMessageEntryFromEventData(e *azeventhubs.ReceivedEventData, topic string, getAllProperties bool) (pubsub.BulkMessageEntry, error) {
entryID, err := uuid.NewRandom()
if err != nil {
return pubsub.BulkMessageEntry{}, err
}
meta := getMetadataFromEventData(e, getAllProperties)
entry := pubsub.BulkMessageEntry{
EntryId: entryID.String(),
Event: e.Body,
Metadata: meta,
}
return entry, nil
}
// Adds a property to the response metadata
func addPropertyToMetadata(key string, value any, md map[string]string) {
switch v := value.(type) {
case *time.Time:
if v != nil {
md[key] = v.Format(time.RFC3339)
}
case time.Time:
md[key] = v.Format(time.RFC3339)
default:
str, err := cast.ToStringE(value)
if err == nil {
md[key] = str
}
}
}