-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlogging_hook_spec.rb
More file actions
251 lines (209 loc) · 8.9 KB
/
Copy pathlogging_hook_spec.rb
File metadata and controls
251 lines (209 loc) · 8.9 KB
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
# frozen_string_literal: true
require "spec_helper"
RSpec.describe OpenFeature::SDK::Hooks::LoggingHook do
let(:client_metadata) { OpenFeature::SDK::ClientMetadata.new(domain: "test-domain") }
let(:provider_metadata) { OpenFeature::SDK::Provider::ProviderMetadata.new(name: "test-provider") }
let(:evaluation_context) { OpenFeature::SDK::EvaluationContext.new(targeting_key: "user-123", env: "prod") }
let(:hook_context) do
OpenFeature::SDK::Hooks::HookContext.new(
flag_key: "my-flag",
flag_value_type: :boolean,
default_value: false,
evaluation_context: evaluation_context,
client_metadata: client_metadata,
provider_metadata: provider_metadata
)
end
let(:evaluation_details) do
resolution = OpenFeature::SDK::Provider::ResolutionDetails.new(
value: true,
reason: "TARGETING_MATCH",
variant: "enabled"
)
OpenFeature::SDK::EvaluationDetails.new(
flag_key: "my-flag",
resolution_details: resolution
)
end
let(:logger) { double("logger") }
let(:hints) { OpenFeature::SDK::Hooks::Hints.new }
describe "#before" do
it "logs at debug level with correct fields" do
hook = described_class.new(logger: logger)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).to include("stage=before")
expect(message).to include("domain=test-domain")
expect(message).to include("provider_name=test-provider")
expect(message).to include("flag_key=my-flag")
expect(message).to include("default_value=false")
end
hook.before(hook_context: hook_context, hints: hints)
end
it "does not include evaluation_context by default" do
hook = described_class.new(logger: logger)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).not_to include("evaluation_context=")
end
hook.before(hook_context: hook_context, hints: hints)
end
it "includes evaluation_context when enabled" do
hook = described_class.new(logger: logger, include_evaluation_context: true)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).to include("evaluation_context=")
expect(message).to include("targeting_key=user-123")
expect(message).to include("env=prod")
end
hook.before(hook_context: hook_context, hints: hints)
end
it "returns nil" do
hook = described_class.new(logger: logger)
allow(logger).to receive(:debug)
expect(hook.before(hook_context: hook_context, hints: hints)).to be_nil
end
end
describe "#after" do
it "logs at debug level with evaluation details" do
hook = described_class.new(logger: logger)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).to include("stage=after")
expect(message).to include("domain=test-domain")
expect(message).to include("provider_name=test-provider")
expect(message).to include("flag_key=my-flag")
expect(message).to include("default_value=false")
expect(message).to include("reason=TARGETING_MATCH")
expect(message).to include("variant=enabled")
expect(message).to include("value=true")
end
hook.after(hook_context: hook_context, evaluation_details: evaluation_details, hints: hints)
end
it "omits reason and variant when nil" do
resolution = OpenFeature::SDK::Provider::ResolutionDetails.new(value: "hello")
details = OpenFeature::SDK::EvaluationDetails.new(flag_key: "my-flag", resolution_details: resolution)
hook = described_class.new(logger: logger)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).not_to include("reason=")
expect(message).not_to include("variant=")
expect(message).to include("value=hello")
end
hook.after(hook_context: hook_context, evaluation_details: details, hints: hints)
end
it "includes evaluation_context when enabled" do
hook = described_class.new(logger: logger, include_evaluation_context: true)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).to include("evaluation_context=")
expect(message).to include("targeting_key=user-123")
end
hook.after(hook_context: hook_context, evaluation_details: evaluation_details, hints: hints)
end
it "returns nil" do
hook = described_class.new(logger: logger)
allow(logger).to receive(:debug)
expect(hook.after(hook_context: hook_context, evaluation_details: evaluation_details, hints: hints)).to be_nil
end
end
describe "#error" do
let(:exception) { StandardError.new("something went wrong") }
it "logs at error level with error fields" do
hook = described_class.new(logger: logger)
expect(logger).to receive(:error) do |&block|
message = block.call
expect(message).to include("stage=error")
expect(message).to include("domain=test-domain")
expect(message).to include("provider_name=test-provider")
expect(message).to include("flag_key=my-flag")
expect(message).to include("default_value=false")
expect(message).to include("error_code=GENERAL")
expect(message).to include("error_message=something went wrong")
end
hook.error(hook_context: hook_context, exception: exception, hints: hints)
end
it "uses error_code from exception when available" do
error_with_code = OpenFeature::SDK::ProviderInitializationError.new(
"init failed",
error_code: "CUSTOM_ERROR_CODE"
)
hook = described_class.new(logger: logger)
expect(logger).to receive(:error) do |&block|
message = block.call
expect(message).to include("error_code=CUSTOM_ERROR_CODE")
end
hook.error(hook_context: hook_context, exception: error_with_code, hints: hints)
end
it "includes evaluation_context when enabled" do
hook = described_class.new(logger: logger, include_evaluation_context: true)
expect(logger).to receive(:error) do |&block|
message = block.call
expect(message).to include("evaluation_context=")
expect(message).to include("targeting_key=user-123")
end
hook.error(hook_context: hook_context, exception: exception, hints: hints)
end
it "returns nil" do
hook = described_class.new(logger: logger)
allow(logger).to receive(:error)
expect(hook.error(hook_context: hook_context, exception: exception, hints: hints)).to be_nil
end
end
describe "#finally" do
it "does not log (uses default no-op from Hook module)" do
hook = described_class.new(logger: logger)
expect(logger).not_to receive(:debug)
expect(logger).not_to receive(:info)
expect(logger).not_to receive(:error)
hook.finally(hook_context: hook_context, evaluation_details: evaluation_details, hints: hints)
end
end
describe "logger fallback" do
it "falls back to OpenFeature::SDK.configuration.logger" do
config_logger = double("config_logger")
allow(OpenFeature::SDK).to receive(:configuration).and_return(
instance_double(OpenFeature::SDK::Configuration, logger: config_logger)
)
hook = described_class.new
expect(config_logger).to receive(:debug)
hook.before(hook_context: hook_context, hints: hints)
end
it "handles nil logger gracefully" do
allow(OpenFeature::SDK).to receive(:configuration).and_return(
instance_double(OpenFeature::SDK::Configuration, logger: nil)
)
hook = described_class.new
expect { hook.before(hook_context: hook_context, hints: hints) }.not_to raise_error
end
end
describe "log injection sanitization" do
it "sanitizes newlines in logged values" do
injected_context = OpenFeature::SDK::Hooks::HookContext.new(
flag_key: "safe-key\ninjected=malicious",
flag_value_type: :boolean,
default_value: false,
evaluation_context: evaluation_context,
client_metadata: client_metadata,
provider_metadata: provider_metadata
)
hook = described_class.new(logger: logger)
expect(logger).to receive(:debug) do |&block|
message = block.call
expect(message).not_to include("\n")
expect(message).to include("safe-key injected=malicious")
end
hook.before(hook_context: injected_context, hints: hints)
end
it "sanitizes newlines in error messages" do
exception = StandardError.new("error\nfake_entry=injected")
hook = described_class.new(logger: logger)
expect(logger).to receive(:error) do |&block|
message = block.call
expect(message).not_to include("\n")
expect(message).to include("error fake_entry=injected")
end
hook.error(hook_context: hook_context, exception: exception, hints: hints)
end
end
end