This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Add RemoteBaggageRestrictionManager #229
Merged
+654
−38
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule idl
updated
from 23f33f to c5adbc
37 changes: 37 additions & 0 deletions
37
jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageRestrictionManagerProxy.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,37 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.jaeger.baggage; | ||
|
||
import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; | ||
import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* BaggageRestrictionManagerProxy is an interface for a class that fetches baggage | ||
* restrictions for specific service from a remote source i.e. jaeger-agent. | ||
*/ | ||
public interface BaggageRestrictionManagerProxy { | ||
List<BaggageRestrictionResponse> getBaggageRestrictions(String serviceName) | ||
throws BaggageRestrictionManagerException; | ||
} |
71 changes: 71 additions & 0 deletions
71
jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.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,71 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.jaeger.baggage; | ||
|
||
import static com.uber.jaeger.utils.Utils.makeGetRequest; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; | ||
import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.net.URLEncoder; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HttpBaggageRestrictionManagerProxy implements BaggageRestrictionManagerProxy { | ||
private static final String DEFAULT_HOST_PORT = "localhost:5778"; | ||
private final Gson gson = new Gson(); | ||
private final String hostPort; | ||
|
||
public HttpBaggageRestrictionManagerProxy(String hostPort) { | ||
this.hostPort = hostPort != null ? hostPort : DEFAULT_HOST_PORT; | ||
} | ||
|
||
List<BaggageRestrictionResponse> parseJson(String json) throws BaggageRestrictionManagerException { | ||
try { | ||
Type listType = new TypeToken<ArrayList<BaggageRestrictionResponse>>(){}.getType(); | ||
return gson.fromJson(json, listType); | ||
} catch (JsonSyntaxException e) { | ||
throw new BaggageRestrictionManagerException("Cannot deserialize json", e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<BaggageRestrictionResponse> getBaggageRestrictions(String serviceName) | ||
throws BaggageRestrictionManagerException { | ||
String jsonString; | ||
try { | ||
jsonString = | ||
makeGetRequest( | ||
"http://" + hostPort + "/baggageRestrictions?service=" + URLEncoder.encode(serviceName, "UTF-8")); | ||
} catch (IOException e) { | ||
throw new BaggageRestrictionManagerException( | ||
"http call to get baggage restriction from local agent failed.", e); | ||
} | ||
return parseJson(jsonString); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.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,145 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.jaeger.baggage; | ||
|
||
import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; | ||
import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; | ||
import com.uber.jaeger.metrics.Metrics; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
/** | ||
* NewRestrictionManager returns a BaggageRestrictionManager that polls the agent for the latest | ||
* baggage restrictions. | ||
*/ | ||
public class RemoteBaggageRestrictionManager implements BaggageRestrictionManager { | ||
private static final int DEFAULT_REFRESH_INTERVAL_MS = 60000; | ||
private static final int DEFAULT_INITIAL_DELAY_MS = 0; | ||
|
||
private final String serviceName; | ||
private final BaggageRestrictionManagerProxy proxy; | ||
private final Timer pollTimer; | ||
private final Metrics metrics; | ||
private final boolean denyBaggageOnInitializationFailure; | ||
private volatile boolean initialized; | ||
private volatile Map<String, Restriction> restrictions = new HashMap<String, Restriction>(); | ||
private final Restriction invalidRestriction; | ||
private final Restriction validRestriction; | ||
|
||
public RemoteBaggageRestrictionManager( | ||
String serviceName, | ||
BaggageRestrictionManagerProxy proxy, | ||
Metrics metrics, | ||
boolean denyBaggageOnInitializationFailure | ||
) { | ||
this(serviceName, proxy, metrics, denyBaggageOnInitializationFailure, DEFAULT_REFRESH_INTERVAL_MS); | ||
} | ||
|
||
public RemoteBaggageRestrictionManager( | ||
String serviceName, | ||
BaggageRestrictionManagerProxy proxy, | ||
Metrics metrics, | ||
boolean denyBaggageOnInitializationFailure, | ||
int refreshIntervalMs | ||
) { | ||
this(serviceName, proxy, metrics, denyBaggageOnInitializationFailure, refreshIntervalMs, DEFAULT_INITIAL_DELAY_MS); | ||
} | ||
|
||
RemoteBaggageRestrictionManager( | ||
String serviceName, | ||
BaggageRestrictionManagerProxy proxy, | ||
Metrics metrics, | ||
boolean denyBaggageOnInitializationFailure, | ||
int refreshIntervalMs, | ||
int initialDelayMs | ||
) { | ||
this.serviceName = serviceName; | ||
this.proxy = proxy; | ||
this.metrics = metrics; | ||
this.denyBaggageOnInitializationFailure = denyBaggageOnInitializationFailure; | ||
this.initialized = false; | ||
this.invalidRestriction = Restriction.of(false, 0); | ||
this.validRestriction = Restriction.of(true, DEFAULT_MAX_VALUE_LENGTH); | ||
|
||
pollTimer = new Timer(true); // true makes this a daemon thread | ||
pollTimer.schedule( | ||
new TimerTask() { | ||
@Override | ||
public void run() { | ||
updateBaggageRestrictions(); | ||
} | ||
}, | ||
initialDelayMs, | ||
refreshIntervalMs); | ||
} | ||
|
||
public boolean isReady() { | ||
return initialized; | ||
} | ||
|
||
void updateBaggageRestrictions() { | ||
List<BaggageRestrictionResponse> response; | ||
try { | ||
response = proxy.getBaggageRestrictions(serviceName); | ||
} catch (BaggageRestrictionManagerException e) { | ||
metrics.baggageRestrictionsUpdateFailure.inc(1); | ||
return; | ||
} | ||
|
||
updateBaggageRestrictions(response); | ||
metrics.baggageRestrictionsUpdateSuccess.inc(1); | ||
} | ||
|
||
private void updateBaggageRestrictions(List<BaggageRestrictionResponse> restrictions) { | ||
Map<String, Restriction> baggageRestrictions = new HashMap<String, Restriction>(); | ||
for (BaggageRestrictionResponse restriction : restrictions) { | ||
baggageRestrictions.put(restriction.getBaggageKey(), Restriction.of(true, restriction.getMaxValueLength())); | ||
} | ||
this.restrictions = baggageRestrictions; | ||
initialized = true; | ||
} | ||
|
||
public void close() { | ||
pollTimer.cancel(); | ||
} | ||
|
||
@Override | ||
public Restriction getRestriction(String service, String key) { | ||
if (!initialized) { | ||
if (denyBaggageOnInitializationFailure) { | ||
return invalidRestriction; | ||
} else { | ||
return validRestriction; | ||
} | ||
} | ||
Restriction restriction = this.restrictions.get(key); | ||
if (restriction != null) { | ||
return restriction; | ||
} | ||
return invalidRestriction; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
jaeger-core/src/main/java/com/uber/jaeger/baggage/http/BaggageRestrictionResponse.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,31 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.jaeger.baggage.http; | ||
|
||
import lombok.Value; | ||
|
||
@Value | ||
public class BaggageRestrictionResponse { | ||
String baggageKey; | ||
int maxValueLength; | ||
} |
35 changes: 35 additions & 0 deletions
35
jaeger-core/src/main/java/com/uber/jaeger/exceptions/BaggageRestrictionManagerException.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,35 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.jaeger.exceptions; | ||
|
||
import java.io.IOException; | ||
|
||
public class BaggageRestrictionManagerException extends IOException { | ||
public BaggageRestrictionManagerException(String msg) { | ||
super(msg); | ||
} | ||
|
||
public BaggageRestrictionManagerException(String msg, Throwable cause) { | ||
super(msg, cause); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialDelayMs is only exposed for testing purposes so I can determine when the first call to remote agent is made. Under normal operations, this class will start up and asynchronously fetch restrictions. If the user wants to know if restrictions are ready, they can check via isReady()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes more sense as code comment