-
Notifications
You must be signed in to change notification settings - Fork 91
/
LoggerPro.NSQAppender.pas
257 lines (223 loc) · 8.34 KB
/
LoggerPro.NSQAppender.pas
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// *************************************************************************** }
//
// LoggerPro
//
// Copyright (c) 2010-2024 Daniele Teti
//
// https://github.com/danieleteti/loggerpro
//
// Contributors for this file:
// Fulgan - https://github.com/Fulgan
//
// ***************************************************************************
//
// 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.
//
// ***************************************************************************
unit LoggerPro.NSQAppender;
interface
uses Classes, SysUtils, LoggerPro, System.Net.HttpClient;
type
{
Author: Stéphane "Fulgan" GROBETY (https://github.com/Fulgan/)
Log appender for NSQ (https://nsq.io) (https://github.com/nsqio/nsq)
"NSQ is a realtime message processing system designed to operate at bitly's
scale, handling billions of messages per day. It promotes distributed and
decentralized topologies without single points of failure, enabling fault
tolerance and high availability coupled with a reliable message delivery
guarantee"
For testing, you can navigate to the NSQ folder and type the following commands:
This starts the NSQLookup service then starts a listener on the default
endpoint(http:/127.0.0.1:4151)
start nsqlookupd
start nsqd --lookupd-tcp-address=127.0.0.1:4160
This starts a consumer for the topic "test" that outputs the messages to the console:
start nsq_tail --topic=test --lookupd-http-address=127.0.0.1:4161
(optional) This starts a consumer for the ephemeral topic "test" that outputs the messages to the console:
start nsq_tail --topic=test#ephemeral --lookupd-http-address=127.0.0.1:4161
(optional) This starts a NSQAdmin web interface that can be reached on http://localhost:4171/
start nsqadmin --lookupd-http-address=127.0.0.1:4161
Note about consumers:
- If there is no consumer to received messages for a channel, NSQ will
save them to memory and disk unless the topic has been marked as Ephemeral.
Use NSQAdmin to delete any extra channel created.
- Ephemeral topics are not saved or cached and the topic will be deleted
once the last consumer disconnects
- Writing a consumer is more complex than writing a client. A list of available
client libraries can be found at https://nsq.io/clients/client_libraries.html
}
TOnCreateData = procedure(const sender : TObject; const LogItem: TLogItem; var Data: TStream);
TOnNetSendError = procedure(const sender : TObject; const LogItem: TLogItem; const NetError: ENetHTTPClientException; var RetryCount: Integer);
TLoggerProNSQAppenderBase = class(TLoggerProAppenderBase, ILogAppender)
private
FOnCreateData: TOnCreateData;
FOnNetSendError: TOnNetSendError;
procedure SetOnCreateData(const Value: TOnCreateData);
procedure SetOnNetSendError(const Value: TOnNetSendError);
protected
FNSQUrl : string;
FTopic: String;
FUserName, FMachineName: string;
FEphemeral: Boolean;
FLastSignature: string;
public
const DEFAULT_NSQ_URL = 'http://127.0.0.1:4151';
function GetNSQUrl: string;
procedure SetNSQUrl(const Value: string);
function GetTopic: string;
procedure SetTopic(const Value: string);
procedure SetEphemeral(const Value: Boolean);
/// <summary>TLoggerProNSQAppenderBase.Create
/// </summary>
/// <param name="aTopic"> (string) This is the "topic" of the channel. If left
/// empty, the LogItem's tag will be used. </param>
/// <param name="aEphemeral"> (Boolean) If true, the NSQ channel will be marked as
/// Ephemeral: messages sent to this channel will neither be cached nor
/// queued</param>
/// <param name="aNSQUrl"> (string) URL of the NSQD service (usually, http://127.0.
/// 0.1:4151)</param>
/// <param name="aLogFormat"> (string) Log format to use if no custom log message
/// creation event is defined </param>
constructor Create(aTopic: string=''; aEphemeral: Boolean = False;
aNSQUrl: string=DEFAULT_NSQ_URL;
aLogItemRenderer: ILogItemRenderer = nil);
reintroduce;
property NSQUrl: string read GetNSQUrl write SetNSQUrl;
property Ephemeral: Boolean read FEphemeral write SetEphemeral;
property OnCreateData: TOnCreateData read FOnCreateData write SetOnCreateData;
property OnNetSendError: TOnNetSendError read FOnNetSendError write SetOnNetSendError;
property Topic: string read GetTopic write SetTopic;
procedure TearDown; override;
procedure Setup; override;
procedure WriteLog(const aLogItem: TLogItem); override;
function CreateData(const SrcLogItem: TLogItem): TStream; virtual;
end;
implementation
uses System.NetEncoding;
constructor TLoggerProNSQAppenderBase.Create(aTopic: string; aEphemeral: Boolean;
aNSQUrl: string; aLogItemRenderer: ILogItemRenderer);
begin
inherited Create(aLogItemRenderer);
FEphemeral := aEphemeral;
FNSQUrl := 'http://127.0.0.1:4151';
FUserName := aNSQUrl;
FTopic := aTopic;
end;
function TLoggerProNSQAppenderBase.CreateData(
const SrcLogItem: TLogItem): TStream;
begin
result := nil;
try
if assigned(FOnCreateData) then
begin
FOnCreateData(Self, SrcLogItem, Result);
end
else
begin
result := TStringStream.Create(FormatLog(SrcLogItem), TEncoding.UTF8);
end;
except
on e: Exception do
begin
FreeAndNil(Result);
raise;
end;
end;
end;
function TLoggerProNSQAppenderBase.GetNSQUrl: string;
begin
result := FNSQUrl;
end;
function TLoggerProNSQAppenderBase.GetTopic: string;
begin
result := FTopic;
end;
procedure TLoggerProNSQAppenderBase.SetEphemeral(const Value: Boolean);
begin
FEphemeral := Value;
end;
procedure TLoggerProNSQAppenderBase.SetNSQUrl(const Value: string);
begin
FNSQUrl := value;
end;
procedure TLoggerProNSQAppenderBase.SetOnCreateData(const Value: TOnCreateData);
begin
FOnCreateData := Value;
end;
procedure TLoggerProNSQAppenderBase.SetOnNetSendError(
const Value: TOnNetSendError);
begin
FOnNetSendError := Value;
end;
procedure TLoggerProNSQAppenderBase.SetTopic(const Value: string);
begin
FTopic := value;
end;
procedure TLoggerProNSQAppenderBase.Setup;
begin
inherited;
end;
procedure TLoggerProNSQAppenderBase.TearDown;
begin
inherited;
end;
procedure TLoggerProNSQAppenderBase.WriteLog(const aLogItem: TLogItem);
var
FHTTPCli: THTTPClient;
URI: string;
Data: TStream;
TopicName: string;
FRetryCount: Integer;
begin
FRetryCount := 0;
FHTTPCli := THTTPClient.Create;
try
if Topic.trim.IsEmpty then
TopicName := aLogItem.LogTag.Trim
else
TopicName := Topic.Trim;
URI :=NSQUrl + '/pub?topic=' + TNetEncoding.URL.Encode(TopicName);
if Ephemeral then
URI := URI + '#ephemeral';
Data := CreateData(aLogItem);
if Assigned(Data) then
begin
repeat
try
// Set very short timeouts: this is a local call and we don't want to block the queue for too long.
{$IF CompilerVersion >= 31}
FHTTPCli.ConnectionTimeout := 100;
FHTTPCli.ResponseTimeout := 200;
{$ENDIF}
Data.Seek(0, soFromBeginning);
// ignore the respnse: as long as NSQD has accepted the POST, it will handle the result
FHTTPCli.Post(URI, Data);
break;
except
on e: ENetHTTPClientException do
begin
// if there is an event handler for net exception, call it
if Assigned(FOnNetSendError) then
OnNetSendError(self, aLogItem, e, FRetryCount);
// if the handler has set FRetryCount to a positive value then retry the call
if FRetryCount <= 0 then
break;
end;
end;
until false;
end;
finally
FreeAndNil(FHTTPCli);
end;
end;
end.