-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compressor SPI to support additional compression algos (#5990)
- Loading branch information
Showing
20 changed files
with
327 additions
and
154 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...rters/common/src/main/java/io/opentelemetry/exporter/internal/compression/Compressor.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,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.compression; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** | ||
* An abstraction for compressing messages. Implementation MUST be thread safe as the same instance | ||
* is expected to be used many times and concurrently. Instances are usually singletons. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
@ThreadSafe | ||
public interface Compressor { | ||
|
||
/** | ||
* The name of the compressor encoding. | ||
* | ||
* <p>Used to identify the compressor during configuration and to populate the {@code | ||
* Content-Encoding} header. | ||
*/ | ||
String getEncoding(); | ||
|
||
/** Wrap the {@code outputStream} with a compressing output stream. */ | ||
OutputStream compress(OutputStream outputStream) throws IOException; | ||
} |
18 changes: 18 additions & 0 deletions
18
...mmon/src/main/java/io/opentelemetry/exporter/internal/compression/CompressorProvider.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,18 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.compression; | ||
|
||
/** | ||
* A service provider interface (SPI) for providing {@link Compressor}s. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public interface CompressorProvider { | ||
|
||
/** Return the {@link Compressor}. */ | ||
Compressor getInstance(); | ||
} |
59 changes: 59 additions & 0 deletions
59
...s/common/src/main/java/io/opentelemetry/exporter/internal/compression/CompressorUtil.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,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.compression; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
|
||
/** | ||
* Utilities for resolving SPI {@link Compressor}s. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
* | ||
* @see CompressorProvider | ||
*/ | ||
public final class CompressorUtil { | ||
|
||
private static final Map<String, Compressor> compressorRegistry = buildCompressorRegistry(); | ||
|
||
private CompressorUtil() {} | ||
|
||
/** Get list of loaded compressors, named according to {@link Compressor#getEncoding()}. */ | ||
public static Set<String> supportedCompressors() { | ||
return Collections.unmodifiableSet(compressorRegistry.keySet()); | ||
} | ||
|
||
/** | ||
* Resolve the {@link Compressor} with the {@link Compressor#getEncoding()} equal to the {@code | ||
* encoding}. | ||
* | ||
* @throws IllegalArgumentException if no match is found | ||
*/ | ||
public static Compressor resolveCompressor(String encoding) { | ||
Compressor compressor = compressorRegistry.get(encoding); | ||
if (compressor == null) { | ||
throw new IllegalArgumentException( | ||
"Could not resolve compressor for encoding \"" + encoding + "\"."); | ||
} | ||
return compressor; | ||
} | ||
|
||
private static Map<String, Compressor> buildCompressorRegistry() { | ||
Map<String, Compressor> compressors = new HashMap<>(); | ||
for (CompressorProvider spi : | ||
ServiceLoader.load(CompressorProvider.class, CompressorUtil.class.getClassLoader())) { | ||
Compressor compressor = spi.getInstance(); | ||
compressors.put(compressor.getEncoding(), compressor); | ||
} | ||
// Hardcode gzip compressor | ||
compressors.put(GzipCompressor.getInstance().getEncoding(), GzipCompressor.getInstance()); | ||
return compressors; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...s/common/src/main/java/io/opentelemetry/exporter/internal/compression/GzipCompressor.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 The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.compression; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
/** | ||
* Gzip {@link Compressor}. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public final class GzipCompressor implements Compressor { | ||
|
||
private static final GzipCompressor INSTANCE = new GzipCompressor(); | ||
|
||
private GzipCompressor() {} | ||
|
||
public static GzipCompressor getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public String getEncoding() { | ||
return "gzip"; | ||
} | ||
|
||
@Override | ||
public OutputStream compress(OutputStream outputStream) throws IOException { | ||
return new GZIPOutputStream(outputStream); | ||
} | ||
} |
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
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.