-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement service and endpoint for changing logging level temporarily (…
…#778) * Implement service and endpoint for changing logging level temporarily * Add tests * Rollback experimentation code * Fix docs Co-authored-by: rpanchyk <rpanchyk@rubiconproject.com>
- Loading branch information
Showing
13 changed files
with
357 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Change logging level endpoint | ||
|
||
This endpoint has a path `/logging/changelevel` by default (can be configured). | ||
|
||
This endpoint allows changing `org.prebid.server` logger level temporarily, mainly for troubleshooting production issues. | ||
|
||
### Query Params | ||
- `level` - desired logging level to set; must be one of `error`, `warn`, `info`, `debug` | ||
- `duration` - for how long to change level before it gets reset to original; there is an upper threshold for this | ||
value set in [configuration](../../config-app.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/org/prebid/server/handler/LoggerControlKnobHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.prebid.server.handler; | ||
|
||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.MultiMap; | ||
import io.vertx.ext.web.RoutingContext; | ||
import org.prebid.server.exception.InvalidRequestException; | ||
import org.prebid.server.log.LoggerControlKnob; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class LoggerControlKnobHandler implements Handler<RoutingContext> { | ||
|
||
private static final String LEVEL_PARAMETER = "level"; | ||
private static final String DURATION_PARAMETER = "duration"; | ||
|
||
private static final Set<String> ALLOWED_LEVELS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( | ||
"error", "warn", "info", "debug"))); | ||
|
||
private final long maxDurationMs; | ||
private final LoggerControlKnob loggerControlKnob; | ||
|
||
public LoggerControlKnobHandler(long maxDurationMs, LoggerControlKnob loggerControlKnob) { | ||
this.maxDurationMs = maxDurationMs; | ||
this.loggerControlKnob = Objects.requireNonNull(loggerControlKnob); | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext context) { | ||
final MultiMap parameters = context.request().params(); | ||
|
||
try { | ||
loggerControlKnob.changeLogLevel(readLevel(parameters), readDuration(parameters)); | ||
} catch (InvalidRequestException e) { | ||
context.response().setStatusCode(HttpResponseStatus.BAD_REQUEST.code()).end(e.getMessage()); | ||
return; | ||
} | ||
|
||
context.response().end(); | ||
} | ||
|
||
private String readLevel(MultiMap parameters) { | ||
final String level = parameters.get(LEVEL_PARAMETER); | ||
|
||
if (level == null) { | ||
throw new InvalidRequestException(String.format("Missing required parameter '%s'", LEVEL_PARAMETER)); | ||
} | ||
|
||
if (!ALLOWED_LEVELS.contains(level.toLowerCase())) { | ||
throw new InvalidRequestException(String.format( | ||
"Invalid '%s' parameter value, allowed values '%s'", LEVEL_PARAMETER, ALLOWED_LEVELS)); | ||
} | ||
|
||
return level; | ||
} | ||
|
||
private Duration readDuration(MultiMap parameters) { | ||
final Integer duration = getIntParameter(DURATION_PARAMETER, parameters); | ||
|
||
if (duration == null) { | ||
throw new InvalidRequestException(String.format("Missing required parameter '%s'", DURATION_PARAMETER)); | ||
} | ||
|
||
if (duration < 1 || duration > maxDurationMs) { | ||
throw new InvalidRequestException(String.format( | ||
"Parameter '%s' must be between %d and %d", DURATION_PARAMETER, 0, maxDurationMs)); | ||
} | ||
|
||
return Duration.ofMillis(duration); | ||
} | ||
|
||
private Integer getIntParameter(String parameterName, MultiMap parameters) { | ||
final String value = parameters.get(parameterName); | ||
try { | ||
return value != null ? Integer.parseInt(value) : null; | ||
} catch (NumberFormatException e) { | ||
throw new InvalidRequestException(String.format("Invalid '%s' parameter value", parameterName)); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/prebid/server/log/LoggerControlKnob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.prebid.server.log; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import io.vertx.core.Vertx; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class LoggerControlKnob { | ||
|
||
private static final String PREBID_LOGGER = "org.prebid.server"; | ||
|
||
private final Vertx vertx; | ||
private final Logger logger; | ||
private final Level originalLevel; | ||
|
||
private final Lock lock = new ReentrantLock(); | ||
private Long restoreTimerId = null; | ||
|
||
public LoggerControlKnob(Vertx vertx) { | ||
this.vertx = Objects.requireNonNull(vertx); | ||
|
||
logger = getPrebidLogger(); | ||
originalLevel = logger != null ? logger.getLevel() : null; | ||
} | ||
|
||
public void changeLogLevel(String level, Duration duration) { | ||
if (logger == null) { | ||
return; | ||
} | ||
|
||
lock.lock(); | ||
try { | ||
if (restoreTimerId != null) { | ||
vertx.cancelTimer(restoreTimerId); | ||
restoreTimerId = null; | ||
} | ||
|
||
logger.setLevel(Level.toLevel(level, originalLevel)); | ||
restoreTimerId = vertx.setTimer(duration.toMillis(), this::resetLoggerLevel); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
private static Logger getPrebidLogger() { | ||
final org.slf4j.Logger prebidSlf4jLogger = LoggerFactory.getLogger(PREBID_LOGGER); | ||
return prebidSlf4jLogger instanceof Logger ? (Logger) prebidSlf4jLogger : null; | ||
} | ||
|
||
private void resetLoggerLevel(long triggeredTimerId) { | ||
lock.lock(); | ||
try { | ||
if (restoreTimerId == null || triggeredTimerId != restoreTimerId) { | ||
return; | ||
} | ||
|
||
logger.setLevel(originalLevel); | ||
restoreTimerId = null; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.