-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.x' into crypto-backport
Signed-off-by: Gaurav Bafna <85113518+gbbafna@users.noreply.github.com>
- Loading branch information
Showing
30 changed files
with
787 additions
and
126 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
45 changes: 45 additions & 0 deletions
45
...rc/main/java/org/opensearch/analysis/common/DelimitedTermFrequencyTokenFilterFactory.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,45 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.analysis.common; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.miscellaneous.DelimitedTermFrequencyTokenFilter; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.analysis.AbstractTokenFilterFactory; | ||
|
||
public class DelimitedTermFrequencyTokenFilterFactory extends AbstractTokenFilterFactory { | ||
public static final char DEFAULT_DELIMITER = '|'; | ||
private static final String DELIMITER = "delimiter"; | ||
private final char delimiter; | ||
|
||
DelimitedTermFrequencyTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) { | ||
super(indexSettings, name, settings); | ||
delimiter = parseDelimiter(settings); | ||
} | ||
|
||
@Override | ||
public TokenStream create(TokenStream tokenStream) { | ||
return new DelimitedTermFrequencyTokenFilter(tokenStream, delimiter); | ||
} | ||
|
||
private static char parseDelimiter(Settings settings) { | ||
String delimiter = settings.get(DELIMITER); | ||
if (delimiter == null) { | ||
return DEFAULT_DELIMITER; | ||
} else if (delimiter.length() == 1) { | ||
return delimiter.charAt(0); | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
"Setting [" + DELIMITER + "] must be a single, non-null character. [" + delimiter + "] was provided." | ||
); | ||
} | ||
} |
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
89 changes: 89 additions & 0 deletions
89
...st/java/org/opensearch/analysis/common/DelimitedTermFrequencyTokenFilterFactoryTests.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,89 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.analysis.common; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.Tokenizer; | ||
import org.apache.lucene.analysis.core.WhitespaceTokenizer; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.apache.lucene.analysis.tokenattributes.TermFrequencyAttribute; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.index.analysis.AnalysisTestsHelper; | ||
import org.opensearch.index.analysis.TokenFilterFactory; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.test.OpenSearchTokenStreamTestCase; | ||
|
||
import java.io.StringReader; | ||
|
||
public class DelimitedTermFrequencyTokenFilterFactoryTests extends OpenSearchTokenStreamTestCase { | ||
|
||
public void testDefault() throws Exception { | ||
OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.filter.my_delimited_term_freq.type", "delimited_term_freq") | ||
.build(), | ||
new CommonAnalysisPlugin() | ||
); | ||
doTest(analysis, "cat|4 dog|5"); | ||
} | ||
|
||
public void testDelimiter() throws Exception { | ||
OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.filter.my_delimited_term_freq.type", "delimited_term_freq") | ||
.put("index.analysis.filter.my_delimited_term_freq.delimiter", ":") | ||
.build(), | ||
new CommonAnalysisPlugin() | ||
); | ||
doTest(analysis, "cat:4 dog:5"); | ||
} | ||
|
||
public void testDelimiterLongerThanOneCharThrows() { | ||
IllegalArgumentException ex = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.filter.my_delimited_term_freq.type", "delimited_term_freq") | ||
.put("index.analysis.filter.my_delimited_term_freq.delimiter", "^^") | ||
.build(), | ||
new CommonAnalysisPlugin() | ||
) | ||
); | ||
|
||
assertEquals("Setting [delimiter] must be a single, non-null character. [^^] was provided.", ex.getMessage()); | ||
} | ||
|
||
private void doTest(OpenSearchTestCase.TestAnalysis analysis, String source) throws Exception { | ||
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_delimited_term_freq"); | ||
Tokenizer tokenizer = new WhitespaceTokenizer(); | ||
tokenizer.setReader(new StringReader(source)); | ||
|
||
TokenStream stream = tokenFilter.create(tokenizer); | ||
|
||
CharTermAttribute termAtt = stream.getAttribute(CharTermAttribute.class); | ||
TermFrequencyAttribute tfAtt = stream.getAttribute(TermFrequencyAttribute.class); | ||
stream.reset(); | ||
assertTermEquals("cat", stream, termAtt, tfAtt, 4); | ||
assertTermEquals("dog", stream, termAtt, tfAtt, 5); | ||
assertFalse(stream.incrementToken()); | ||
stream.end(); | ||
stream.close(); | ||
} | ||
|
||
void assertTermEquals(String expected, TokenStream stream, CharTermAttribute termAtt, TermFrequencyAttribute tfAtt, int expectedTf) | ||
throws Exception { | ||
assertTrue(stream.incrementToken()); | ||
assertEquals(expected, termAtt.toString()); | ||
assertEquals(expectedTf, tfAtt.getTermFrequency()); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
...try-otel/src/main/java/org/opensearch/telemetry/tracing/sampler/ProbabilisticSampler.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,82 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.telemetry.tracing.sampler; | ||
|
||
import org.opensearch.telemetry.TelemetrySettings; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.data.LinkData; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
|
||
/** | ||
* ProbabilisticSampler implements a head-based sampling strategy based on provided settings. | ||
*/ | ||
public class ProbabilisticSampler implements Sampler { | ||
private Sampler defaultSampler; | ||
private final TelemetrySettings telemetrySettings; | ||
private double samplingRatio; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param telemetrySettings Telemetry settings. | ||
*/ | ||
public ProbabilisticSampler(TelemetrySettings telemetrySettings) { | ||
this.telemetrySettings = Objects.requireNonNull(telemetrySettings); | ||
this.samplingRatio = telemetrySettings.getTracerHeadSamplerSamplingRatio(); | ||
this.defaultSampler = Sampler.traceIdRatioBased(samplingRatio); | ||
} | ||
|
||
Sampler getSampler() { | ||
double newSamplingRatio = telemetrySettings.getTracerHeadSamplerSamplingRatio(); | ||
if (isSamplingRatioChanged(newSamplingRatio)) { | ||
synchronized (this) { | ||
this.samplingRatio = newSamplingRatio; | ||
defaultSampler = Sampler.traceIdRatioBased(samplingRatio); | ||
} | ||
} | ||
return defaultSampler; | ||
} | ||
|
||
private boolean isSamplingRatioChanged(double newSamplingRatio) { | ||
return Double.compare(this.samplingRatio, newSamplingRatio) != 0; | ||
} | ||
|
||
double getSamplingRatio() { | ||
return samplingRatio; | ||
} | ||
|
||
@Override | ||
public SamplingResult shouldSample( | ||
Context parentContext, | ||
String traceId, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks | ||
) { | ||
return getSampler().shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Probabilistic Sampler"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getDescription(); | ||
} | ||
} |
Oops, something went wrong.