Skip to content

Commit 4664e60

Browse files
committed
Add LoggingFacade APIs that supports LoggingFilter
1 parent 5ef1532 commit 4664e60

File tree

10 files changed

+533
-34
lines changed

10 files changed

+533
-34
lines changed

operator/src/main/java/oracle/kubernetes/operator/DomainProcessorImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import oracle.kubernetes.operator.logging.LoggingFactory;
3838
import oracle.kubernetes.operator.logging.LoggingFilter;
3939
import oracle.kubernetes.operator.logging.MessageKeys;
40+
import oracle.kubernetes.operator.logging.OncePerMessageLoggingFilter;
4041
import oracle.kubernetes.operator.steps.BeforeAdminServiceStep;
4142
import oracle.kubernetes.operator.steps.DeleteDomainStep;
4243
import oracle.kubernetes.operator.steps.DomainPresenceStep;
@@ -291,7 +292,7 @@ public void dispatchDomainWatch(Watch.Response<Domain> item) {
291292

292293
private void scheduleDomainStatusUpdating(DomainPresenceInfo info) {
293294
AtomicInteger unchangedCount = new AtomicInteger(0);
294-
final LoggingFilter loggingFilter = new LoggingFilter();
295+
final OncePerMessageLoggingFilter loggingFilter = new OncePerMessageLoggingFilter();
295296
Runnable command =
296297
new Runnable() {
297298
public void run() {
@@ -303,7 +304,7 @@ public void run() {
303304
.put(
304305
ProcessingConstants.DOMAIN_COMPONENT_NAME,
305306
Component.createFor(info, delegate.getVersion()));
306-
packet.put(ProcessingConstants.LOGGING_FILTER, loggingFilter);
307+
packet.put(LoggingFilter.LOGGING_FILTER_PACKET_KEY, loggingFilter);
307308
MainTuning main = TuningParameters.getInstance().getMainTuning();
308309
Step strategy =
309310
DomainStatusUpdater.createStatusStep(main.statusUpdateTimeoutSeconds, null);
@@ -316,9 +317,9 @@ public void run() {
316317
@Override
317318
public void onCompletion(Packet packet) {
318319
if (Boolean.TRUE.equals(packet.get(ProcessingConstants.SERVER_HEALTH_READ))) {
319-
loggingFilter.setCanLog(true);
320+
loggingFilter.setFilteringOff(true);
320321
} else {
321-
loggingFilter.setCanLog(false);
322+
loggingFilter.setFilteringOn();
322323
}
323324
Boolean isStatusUnchanged =
324325
(Boolean) packet.get(ProcessingConstants.STATUS_UNCHANGED);
@@ -352,7 +353,7 @@ public void onCompletion(Packet packet) {
352353
@Override
353354
public void onThrowable(Packet packet, Throwable throwable) {
354355
LOGGER.severe(MessageKeys.EXCEPTION, throwable);
355-
loggingFilter.setCanLog(false);
356+
loggingFilter.setFilteringOn();
356357
// retry to trying after shorter delay because of exception
357358
unchangedCount.set(0);
358359
registerStatusUpdater(

operator/src/main/java/oracle/kubernetes/operator/ProcessingConstants.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,5 @@ public interface ProcessingConstants {
3434
public static final String DOMAIN_INTROSPECTOR_LOG_RESULT = "domainIntrospectorLogResult";
3535
public static final String SIT_CONFIG_MAP = "sitConfigMap";
3636

37-
String LOGGING_FILTER = "optionalLogging";
38-
String SERVER_HEALTH_READ = "serverHealthRead";
37+
public static final String SERVER_HEALTH_READ = "serverHealthRead";
3938
}

operator/src/main/java/oracle/kubernetes/operator/helpers/SecretHelper.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.HashMap;
1010
import java.util.List;
1111
import java.util.Map;
12-
import oracle.kubernetes.operator.ProcessingConstants;
1312
import oracle.kubernetes.operator.logging.LoggingFacade;
1413
import oracle.kubernetes.operator.logging.LoggingFactory;
1514
import oracle.kubernetes.operator.logging.LoggingFilter;
@@ -118,7 +117,7 @@ public NextAction apply(Packet packet) {
118117
}
119118

120119
LOGGER.fine(MessageKeys.RETRIEVING_SECRET, secretName);
121-
final LoggingFilter loggingFilter = packet.getValue(ProcessingConstants.LOGGING_FILTER);
120+
final LoggingFilter loggingFilter = packet.getValue(LoggingFilter.LOGGING_FILTER_PACKET_KEY);
122121
CallBuilderFactory factory =
123122
ContainerResolver.getInstance().getContainer().getSPI(CallBuilderFactory.class);
124123
Step read =
@@ -135,13 +134,7 @@ public NextAction onFailure(
135134
int statusCode,
136135
Map<String, List<String>> responseHeaders) {
137136
if (statusCode == CallBuilder.NOT_FOUND) {
138-
if (LoggingFilter.canLog(loggingFilter)) {
139-
LOGGER.warning(
140-
MessageKeys.SECRET_NOT_FOUND,
141-
secretName
142-
+ ", LOGGING_FILTER: "
143-
+ packet.get(ProcessingConstants.LOGGING_FILTER));
144-
}
137+
LOGGER.warning(loggingFilter, MessageKeys.SECRET_NOT_FOUND, secretName);
145138
return doNext(packet);
146139
}
147140
return super.onFailure(packet, e, statusCode, responseHeaders);
@@ -171,17 +164,15 @@ private static Map<String, byte[]> harvestAdminSecretData(
171164
if (usernameBytes != null) {
172165
secretData.put(ADMIN_SERVER_CREDENTIALS_USERNAME, usernameBytes);
173166
} else {
174-
if (LoggingFilter.canLog(loggingFilter)) {
175-
LOGGER.warning(MessageKeys.SECRET_DATA_NOT_FOUND, ADMIN_SERVER_CREDENTIALS_USERNAME);
176-
}
167+
LOGGER.warning(
168+
loggingFilter, MessageKeys.SECRET_DATA_NOT_FOUND, ADMIN_SERVER_CREDENTIALS_USERNAME);
177169
}
178170

179171
if (passwordBytes != null) {
180172
secretData.put(ADMIN_SERVER_CREDENTIALS_PASSWORD, passwordBytes);
181173
} else {
182-
if (LoggingFilter.canLog(loggingFilter)) {
183-
LOGGER.warning(MessageKeys.SECRET_DATA_NOT_FOUND, ADMIN_SERVER_CREDENTIALS_PASSWORD);
184-
}
174+
LOGGER.warning(
175+
loggingFilter, MessageKeys.SECRET_DATA_NOT_FOUND, ADMIN_SERVER_CREDENTIALS_PASSWORD);
185176
}
186177
return secretData;
187178
}

operator/src/main/java/oracle/kubernetes/operator/logging/LoggingFacade.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,20 @@ public void info(String msg, Object... params) {
292292
}
293293
}
294294

295+
/**
296+
* Logs a message which requires parameters at the INFO level with a logging filter applied
297+
*
298+
* @param loggingFilter LoggingFilter to be applied, can be null
299+
* @param msg the message to log
300+
* @param params varargs list of objects to include in the log message
301+
*/
302+
public void info(LoggingFilter loggingFilter, String msg, Object... params) {
303+
if (isInfoEnabled() && LoggingFilter.canLog(loggingFilter, msg)) {
304+
CallerDetails details = inferCaller();
305+
logger.logp(Level.INFO, details.clazz, details.method, msg, params);
306+
}
307+
}
308+
295309
/**
296310
* Logs a message which accompanies a Throwable at the INFO level.
297311
*
@@ -449,6 +463,20 @@ public void severe(String msg, Object... params) {
449463
}
450464
}
451465

466+
/**
467+
* Logs a message which requires parameters at the SEVERE level with a logging filter applied
468+
*
469+
* @param loggingFilter LoggingFilter to be applied, can be null
470+
* @param msg the message to log
471+
* @param params varargs list of objects to include in the log message
472+
*/
473+
public void severe(LoggingFilter loggingFilter, String msg, Object... params) {
474+
if (isSevereEnabled() && LoggingFilter.canLog(loggingFilter, msg)) {
475+
CallerDetails details = inferCaller();
476+
logger.logp(Level.SEVERE, details.clazz, details.method, msg, params);
477+
}
478+
}
479+
452480
/**
453481
* Logs a message which accompanies a Throwable at the SEVERE level.
454482
*
@@ -462,6 +490,20 @@ public void severe(String msg, Throwable thrown) {
462490
}
463491
}
464492

493+
/**
494+
* Logs a message which accompanies a Throwable at the SEVERE level with a logging filter applied
495+
*
496+
* @param loggingFilter LoggingFilter to be applied, can be null
497+
* @param msg the message to log
498+
* @param thrown an Exception to include in the logged message
499+
*/
500+
public void severe(LoggingFilter loggingFilter, String msg, Throwable thrown) {
501+
if (isSevereEnabled() && LoggingFilter.canLog(loggingFilter, msg)) {
502+
CallerDetails details = inferCaller();
503+
logger.logp(Level.SEVERE, details.clazz, details.method, msg, thrown);
504+
}
505+
}
506+
465507
/**
466508
* Logs that an exception will be thrown. The calling class and method names will be inferred.
467509
*
@@ -499,6 +541,20 @@ public void warning(String msg, Object... params) {
499541
}
500542
}
501543

544+
/**
545+
* Logs a message which requires parameters at the WARNING level with a logging filter applied
546+
*
547+
* @param loggingFilter LoggingFilter to be applied, can be null
548+
* @param msg the message to log
549+
* @param params varargs list of objects to include in the log message
550+
*/
551+
public void warning(LoggingFilter loggingFilter, String msg, Object... params) {
552+
if (isWarningEnabled() && LoggingFilter.canLog(loggingFilter, msg)) {
553+
CallerDetails details = inferCaller();
554+
logger.logp(Level.WARNING, details.clazz, details.method, msg, params);
555+
}
556+
}
557+
502558
/**
503559
* Logs a message which accompanies a Throwable at the WARNING level.
504560
*
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
// Copyright 2017, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

55
package oracle.kubernetes.operator.logging;
66

7-
public class LoggingFilter {
7+
/** A filter to control whether a log message should be logged */
8+
public interface LoggingFilter {
89

9-
volatile boolean loggingAllowed;
10+
// Constant for key for storing LoggingFilter object in Packet map
11+
String LOGGING_FILTER_PACKET_KEY = "loggingFilter";
1012

11-
public void setCanLog(boolean canLog) {
12-
loggingAllowed = canLog;
13-
}
14-
15-
public boolean canLog() {
16-
return loggingAllowed;
17-
}
13+
/**
14+
* Checks if the message should be logged
15+
*
16+
* @param msg the message to be loggged
17+
*/
18+
boolean canLog(String msg);
1819

19-
public static boolean canLog(LoggingFilter loggingFilter) {
20+
/**
21+
* Checks if the message should be logged according to an optional LoggingFilter
22+
*
23+
* @param loggingFilter LoggingFilter that decides whether the log message should be logged, can
24+
* be null
25+
* @param msg The log message to be loggd
26+
* @return the canLog() return value from the provided loggingFilter, message, and parameters, or
27+
* true if loggingFilter is null
28+
*/
29+
static boolean canLog(LoggingFilter loggingFilter, String msg) {
2030
if (loggingFilter == null) {
2131
return true;
2232
}
23-
return loggingFilter.canLog();
33+
return loggingFilter.canLog(msg);
2434
}
2535
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.logging;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/** A LoggingFilter that logs each log message, which are typically message keys, at most once */
11+
public class OncePerMessageLoggingFilter implements LoggingFilter {
12+
13+
// allow all messages to be logged when filtering is off
14+
volatile boolean filtering = false;
15+
16+
List messagesLogged = new ArrayList();
17+
18+
/**
19+
* Turn on filtering of log messages and skip logging of messages that have already been logged
20+
*/
21+
public void setFilteringOn() {
22+
filtering = true;
23+
}
24+
25+
/**
26+
* Turn off filtering of log messages and allow all log messages to be logged
27+
*
28+
* @param resetLogHistory Whether to also clear the history of messages logged so those messages
29+
* will be logged again next time filtering is turned on
30+
*/
31+
public void setFilteringOff(boolean resetLogHistory) {
32+
filtering = false;
33+
if (resetLogHistory) {
34+
resetLogHistory();
35+
}
36+
}
37+
38+
/** Clears the list of history of messages logged and turn off filtering */
39+
public void resetLogHistory() {
40+
synchronized (messagesLogged) {
41+
messagesLogged.clear();
42+
}
43+
}
44+
45+
@Override
46+
public boolean canLog(String msg) {
47+
synchronized (messagesLogged) {
48+
// Do not log if filtering is on and message has already been logged
49+
if (filtering && messagesLogged.contains(msg)) {
50+
return false;
51+
}
52+
messagesLogged.add(msg);
53+
return true;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)