dsmiley commented on code in PR #1854: URL: https://github.com/apache/solr/pull/1854#discussion_r1299243709
########## solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/TestSimplePropagatorDistributedTracing.java: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.solr.opentelemetry; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.TracerProvider; +import java.io.IOException; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.core.SolrCore; +import org.apache.solr.logging.MDCLoggingContext; +import org.apache.solr.util.LogListener; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.MDC; + +public class TestSimplePropagatorDistributedTracing extends SolrCloudTestCase { + + private static final String COLLECTION = "collection1"; + + @BeforeClass + public static void setupCluster() throws Exception { + + // enable simple propagator + System.setProperty("solr.otel.simplePropagatorOnly", "true"); + + // force early init + CustomTestOtelTracerConfigurator.prepareForTest(); + + configureCluster(4) + .addConfig("config", TEST_PATH().resolve("collection1").resolve("conf")) + .withSolrXml(TEST_PATH().resolve("solr.xml")) + .configure(); + + // tracer should be disabled + assertEquals( + "Expecting active otel, not noop impl", + TracerProvider.noop(), + GlobalOpenTelemetry.get().getTracerProvider()); + + CollectionAdminRequest.createCollection(COLLECTION, "config", 2, 2) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(COLLECTION, 2, 4); + } + + @AfterClass + public static void afterClass() { + CustomTestOtelTracerConfigurator.resetForTest(); + } + + @Test + public void test() throws IOException, SolrServerException { + CloudSolrClient cloudClient = cluster.getSolrClient(); + + // Indexing + cloudClient.add(COLLECTION, sdoc("id", "1")); + cloudClient.add(COLLECTION, sdoc("id", "2")); + cloudClient.commit(COLLECTION); + // TODO check tid + + try (LogListener reqLog = LogListener.info(SolrCore.class.getName() + ".Request")) { + assertNull(MDC.get(MDCLoggingContext.TRACE_ID)); + // Searching + cloudClient.query(COLLECTION, new SolrQuery("*:*")); + // all events have the same traceid + assertSameTraceId(reqLog); + } + } + + private void assertSameTraceId(LogListener reqLog) { Review Comment: if none had a traceId, I think this method would succeed. Do we want that? ########## solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/TestSimplePropagatorDistributedTracing.java: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.solr.opentelemetry; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.TracerProvider; +import java.io.IOException; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.core.SolrCore; +import org.apache.solr.logging.MDCLoggingContext; +import org.apache.solr.util.LogListener; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.MDC; + +public class TestSimplePropagatorDistributedTracing extends SolrCloudTestCase { + + private static final String COLLECTION = "collection1"; + + @BeforeClass + public static void setupCluster() throws Exception { + + // enable simple propagator + System.setProperty("solr.otel.simplePropagatorOnly", "true"); + + // force early init + CustomTestOtelTracerConfigurator.prepareForTest(); + + configureCluster(4) + .addConfig("config", TEST_PATH().resolve("collection1").resolve("conf")) + .withSolrXml(TEST_PATH().resolve("solr.xml")) + .configure(); + + // tracer should be disabled + assertEquals( + "Expecting active otel, not noop impl", Review Comment: the message appears inverted; this asserts that `noop` is the current TracerProvider ########## solr/modules/opentelemetry/src/java/org/apache/solr/opentelemetry/simplepropagator/SimplePropagator.java: ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.solr.opentelemetry.simplepropagator; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; +import io.opentelemetry.context.propagation.TextMapGetter; +import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.context.propagation.TextMapSetter; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.solr.logging.MDCLoggingContext; + +/** Simple Http Header Propagator */ +public class SimplePropagator implements TextMapPropagator { + + private static final TextMapPropagator INSTANCE = new SimplePropagator(); + private static final ContextKey<String> TRACE_ID_KEY = ContextKey.named("trace_id"); + + private static final String TRACE_ID = System.getProperty("TRACE_ID", "X-Trace-Id"); + private static final List<String> FIELDS = List.of(TRACE_ID); + + private static final AtomicLong traceCounter = new AtomicLong(0); + + public static TextMapPropagator getInstance() { + return INSTANCE; + } + + private SimplePropagator() {} + + @Override + public Collection<String> fields() { + return FIELDS; + } + + @Override + public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) { + if (setter == null) { + return; + } + String traceId = context.get(TRACE_ID_KEY); + if (traceId != null) { + setter.set(carrier, TRACE_ID, traceId); + } + } + + @Override + public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter) { + String traceId = getter.get(carrier, TRACE_ID); + if (traceId == null) { + traceId = newTraceId(); + } + + MDCLoggingContext.setTracerId(traceId); + return context.with(TRACE_ID_KEY, traceId); + } + + private static String newTraceId() { + return String.format("%s-%d", UUID.randomUUID(), traceCounter.incrementAndGet()); Review Comment: Maybe we should use the same format as "rid" -- and there was some discussion on this between @gerlowskija and me and we settled on the first part being the hostname. See org.apache.solr.handler.component.SearchHandler#generateRid ########## solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/TestSimplePropagatorDistributedTracing.java: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.solr.opentelemetry; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.TracerProvider; +import java.io.IOException; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.core.SolrCore; +import org.apache.solr.logging.MDCLoggingContext; +import org.apache.solr.util.LogListener; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.MDC; + +public class TestSimplePropagatorDistributedTracing extends SolrCloudTestCase { + + private static final String COLLECTION = "collection1"; + + @BeforeClass + public static void setupCluster() throws Exception { + + // enable simple propagator + System.setProperty("solr.otel.simplePropagatorOnly", "true"); + + // force early init + CustomTestOtelTracerConfigurator.prepareForTest(); + + configureCluster(4) + .addConfig("config", TEST_PATH().resolve("collection1").resolve("conf")) + .withSolrXml(TEST_PATH().resolve("solr.xml")) + .configure(); + + // tracer should be disabled + assertEquals( + "Expecting active otel, not noop impl", + TracerProvider.noop(), + GlobalOpenTelemetry.get().getTracerProvider()); + + CollectionAdminRequest.createCollection(COLLECTION, "config", 2, 2) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(COLLECTION, 2, 4); + } + + @AfterClass + public static void afterClass() { + CustomTestOtelTracerConfigurator.resetForTest(); + } + + @Test + public void test() throws IOException, SolrServerException { + CloudSolrClient cloudClient = cluster.getSolrClient(); + + // Indexing + cloudClient.add(COLLECTION, sdoc("id", "1")); + cloudClient.add(COLLECTION, sdoc("id", "2")); + cloudClient.commit(COLLECTION); + // TODO check tid + + try (LogListener reqLog = LogListener.info(SolrCore.class.getName() + ".Request")) { + assertNull(MDC.get(MDCLoggingContext.TRACE_ID)); Review Comment: why check this? Is it related to reqLog, acquired just before? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
