Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TEIIDTOOLS-437 initial opentracing commit
- Loading branch information
Showing
24 changed files
with
779 additions
and
20 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
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
83 changes: 83 additions & 0 deletions
83
client/src/main/java/org/teiid/jdbc/tracing/GlobalTracerInjector.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,83 @@ | ||
/* | ||
* Copyright Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the COPYRIGHT.txt file distributed with this work. | ||
* | ||
* 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 | ||
* | ||
* 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.teiid.jdbc.tracing; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.teiid.jdbc.tracing.TracingHelper.Injector; | ||
|
||
import io.opentracing.Span; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.propagation.Format.Builtin; | ||
import io.opentracing.propagation.TextMapInjectAdapter; | ||
import io.opentracing.util.GlobalTracer; | ||
|
||
/** | ||
* Uses the opentracing library to create a json string representation of the span context | ||
* and provides a way to manipulate a static tracer without using the GlobalTracer registration | ||
*/ | ||
public class GlobalTracerInjector implements Injector { | ||
|
||
private static Tracer TRACER = GlobalTracer.get(); | ||
|
||
@Override | ||
public String getSpanContext() { | ||
return getSpanContext(TRACER); | ||
} | ||
|
||
protected static String getSpanContext(Tracer tracer) { | ||
Span span = tracer.activeSpan(); | ||
if (span == null) { | ||
return null; | ||
} | ||
Map<String,String> spanMap = new HashMap<String, String>(); | ||
tracer.inject(span.context(), Builtin.TEXT_MAP, new TextMapInjectAdapter(spanMap)); | ||
|
||
//simple json creation | ||
StringBuilder json = new StringBuilder(); | ||
json.append('{'); | ||
boolean first = true; | ||
for (Map.Entry<String, String> entry : spanMap.entrySet()) { | ||
if (!first) { | ||
json.append(','); | ||
} else { | ||
first = false; | ||
} | ||
json.append('"').append(entry.getKey().replace("\"", "\\\"")) //$NON-NLS-1$ //$NON-NLS-2$ | ||
.append("\":\"") //$NON-NLS-1$ | ||
.append(entry.getValue().replace("\"", "\\\"")).append('"'); //$NON-NLS-1$ //$NON-NLS-2$ | ||
} | ||
json.append('}'); | ||
return json.toString(); | ||
} | ||
|
||
/* | ||
* Used to workaround that the GlobalTracer can only be registered once. | ||
*/ | ||
|
||
public static Tracer getTracer() { | ||
return TRACER; | ||
} | ||
|
||
public static void setTracer(Tracer tracer) { | ||
TRACER = tracer; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
client/src/main/java/org/teiid/jdbc/tracing/TracingHelper.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 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the COPYRIGHT.txt file distributed with this work. | ||
* | ||
* 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 | ||
* | ||
* 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.teiid.jdbc.tracing; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.teiid.core.util.ReflectionHelper; | ||
|
||
/** | ||
* Used to reflection load logic that is dependent upon the opentracing library, or provide a dummy implementation | ||
*/ | ||
public class TracingHelper { | ||
|
||
public interface Injector { | ||
String getSpanContext(); | ||
} | ||
|
||
private static Logger logger = Logger.getLogger("org.teiid.jdbc"); //$NON-NLS-1$ | ||
|
||
private static Injector INJECTOR; | ||
|
||
public static String getSpanContext() { | ||
if (INJECTOR == null) { | ||
try { | ||
INJECTOR = (Injector) ReflectionHelper.create("org.teiid.jdbc.tracing.GlobalTracerInjector", null, TracingHelper.class.getClassLoader()); //$NON-NLS-1$ | ||
} catch (Throwable e) { //must catch both Error and Exception | ||
logger.log(Level.FINE, "Unable to load opentracing libraries, propagation will not be used", e); //$NON-NLS-1$ | ||
} | ||
if (INJECTOR == null) { | ||
INJECTOR = new Injector() { | ||
|
||
@Override | ||
public String getSpanContext() { | ||
return null; | ||
} | ||
}; | ||
} | ||
} | ||
return INJECTOR.getSpanContext(); | ||
} | ||
|
||
} |
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
42 changes: 42 additions & 0 deletions
42
client/src/test/java/org/teiid/jdbc/tracing/TestTracing.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,42 @@ | ||
/* | ||
* Copyright Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the COPYRIGHT.txt file distributed with this work. | ||
* | ||
* 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 | ||
* | ||
* 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.teiid.jdbc.tracing; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
import io.opentracing.Scope; | ||
import io.opentracing.mock.MockTracer; | ||
|
||
@SuppressWarnings("nls") | ||
public class TestTracing { | ||
|
||
@Test public void testSpanContextInjection() { | ||
MockTracer tracer = new MockTracer(); | ||
assertNull(GlobalTracerInjector.getSpanContext(tracer)); | ||
Scope ignored = tracer.buildSpan("x").startActive(true); | ||
try { | ||
assertEquals("{\"spanid\":\"2\",\"traceid\":\"1\"}", GlobalTracerInjector.getSpanContext(tracer)); | ||
} finally { | ||
ignored.close(); | ||
} | ||
} | ||
|
||
} |
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.