This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
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.
Summary: # Problem Description Loom uses base64 encoding for the string representation of Trace IDs. The base64 alphabet uses `+` and `/` characters in addition to alphanumeric characters. Trace file names contain Trace IDs, and in order it to be valid filenames, Trace IDs are sanitized to replace unsupported characters. The current approach is replacing both `+` and `/` with an underscore (`_`). This transformation makes it impossible to reconstruct the actual Trace ID from the filename, which could be useful in situations where we need to access the Trace ID without parsing the trace file contents (on either client or server). # Change Description With this change, the sanitization function becomes reversible, by having it replace `+` with `_p_` and `/` with `_s_`. Reviewed By: aandreyeu Differential Revision: D36764316 fbshipit-source-id: 744ac37fe6c190436a2445170fac6d1003b3b2f1
- Loading branch information
1 parent
256981b
commit 67c41c4
Showing
6 changed files
with
119 additions
and
11 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
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,48 @@ | ||
/** | ||
* Copyright 2004-present, Facebook, Inc. | ||
* | ||
* <p>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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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. | ||
*/ | ||
package com.facebook.profilo.core; | ||
|
||
/** | ||
* Provides operations to convert between base64-encoded Trace IDs and file/folder names. | ||
* | ||
* <p>This class is not meant to be instantiated. | ||
* | ||
* <p>The transformation is designed to be reversible for diagnostic purposes. | ||
*/ | ||
public final class TraceIdSanitizer { | ||
private static final String SANITIZED_PLUS = "_p_"; | ||
private static final String SANITIZED_SLASH = "_s_"; | ||
|
||
/** | ||
* Converts the Trace ID into a string that can safely be used as part of a directory name. | ||
* | ||
* <p>Base64 uses alphanumeric characters as well as '+' and '/'. In filenames, we replace '+' | ||
* with '_p_' and '/' with '_s_'. | ||
* | ||
* @param traceId The base64-encoded Trace ID to convert. | ||
* @return A string consisting only of alphanumeric characters and '_'. | ||
*/ | ||
public static String sanitizeTraceId(String traceId) { | ||
return traceId.replace("+", SANITIZED_PLUS).replace("/", SANITIZED_SLASH); | ||
} | ||
|
||
/** | ||
* Returns the original Trace ID corresponding to the given sanitized Trace ID. | ||
* | ||
* @seealso sanitizeTraceId | ||
*/ | ||
public static String restoreSanitizedTraceId(String sanitizedTraceId) { | ||
return sanitizedTraceId.replace(SANITIZED_PLUS, "+").replace(SANITIZED_SLASH, "/"); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
java/test/com/facebook/profilo/core/TraceIdSanitizerTest.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,48 @@ | ||
/** | ||
* Copyright 2004-present, Facebook, Inc. | ||
* | ||
* <p>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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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. | ||
*/ | ||
package com.facebook.profilo.core; | ||
|
||
import static com.facebook.profilo.core.TraceIdSanitizer.restoreSanitizedTraceId; | ||
import static com.facebook.profilo.core.TraceIdSanitizer.sanitizeTraceId; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.facebook.testing.robolectric.v4.WithTestDefaultsRunner; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** Tests {@link TraceIdSanitizer} */ | ||
@RunWith(WithTestDefaultsRunner.class) | ||
public class TraceIdSanitizerTest { | ||
@Before | ||
public void setUp() {} | ||
|
||
@Test | ||
public void testSanitizeTraceId() { | ||
assertThat(sanitizeTraceId("")).isEqualTo(""); | ||
assertThat(sanitizeTraceId("+")).isEqualTo("_p_"); | ||
assertThat(sanitizeTraceId("/")).isEqualTo("_s_"); | ||
assertThat(sanitizeTraceId("aAmMzZ059")).isEqualTo("aAmMzZ059"); | ||
assertThat(sanitizeTraceId("a+b/c")).isEqualTo("a_p_b_s_c"); | ||
} | ||
|
||
@Test | ||
public void testRestoreSanitizedTraceId() { | ||
assertThat(restoreSanitizedTraceId("")).isEqualTo(""); | ||
assertThat(restoreSanitizedTraceId("_p_")).isEqualTo("+"); | ||
assertThat(restoreSanitizedTraceId("_s_")).isEqualTo("/"); | ||
assertThat(restoreSanitizedTraceId("aAmMzZ059")).isEqualTo("aAmMzZ059"); | ||
assertThat(restoreSanitizedTraceId("a_p_b_s_c")).isEqualTo("a+b/c"); | ||
} | ||
} |