|
| 1 | +package oracle.jdbc.provider.observability.tracers; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import oracle.jdbc.DatabaseFunction; |
| 7 | +import oracle.jdbc.TraceEventListener.JdbcExecutionEvent; |
| 8 | +import oracle.jdbc.TraceEventListener.Sequence; |
| 9 | +import oracle.jdbc.TraceEventListener.TraceContext; |
| 10 | +import oracle.jdbc.provider.observability.configuration.ObservabilityConfiguration; |
| 11 | +import jdk.jfr.AnnotationElement; |
| 12 | +import jdk.jfr.Event; |
| 13 | +import jdk.jfr.EventFactory; |
| 14 | +import jdk.jfr.Label; |
| 15 | +import jdk.jfr.Name; |
| 16 | +import jdk.jfr.Category; |
| 17 | +import jdk.jfr.ValueDescriptor; |
| 18 | + |
| 19 | +public class JFRTracer implements ObservabilityTracer{ |
| 20 | + |
| 21 | + public JFRTracer() {} |
| 22 | + |
| 23 | + @Override |
| 24 | + public Object traceRoudtrip(Sequence sequence, TraceContext traceContext, Object userContext) { |
| 25 | + if (sequence.equals(Sequence.BEFORE)) { |
| 26 | + Event event = createEvent( |
| 27 | + traceContext.databaseFunction()); |
| 28 | + event.begin(); |
| 29 | + return event; |
| 30 | + } else { |
| 31 | + if (userContext != null) { |
| 32 | + Event event = (Event) userContext; |
| 33 | + event.set(0, traceContext.getConnectionId()); |
| 34 | + event.set(1, traceContext.databaseOperation()); |
| 35 | + event.set(2, traceContext.tenant()); |
| 36 | + event.set(3, traceContext.getSqlId()); |
| 37 | + if (ObservabilityConfiguration.getInstance().getSensitiveDataEnabled()) { |
| 38 | + event.set(4, traceContext.originalSqlText()); |
| 39 | + event.set(5, traceContext.actualSqlText()); |
| 40 | + event.set(6, traceContext.user()); |
| 41 | + } |
| 42 | + event.end(); |
| 43 | + event.commit(); |
| 44 | + } |
| 45 | + } |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Object traceExecutionEvent(JdbcExecutionEvent event, Object userContext, Object... params) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates an event for a given database operation. |
| 56 | + * @param databaseFunction The database function originating the round trip. |
| 57 | + * @return returns a Java Flight Recorder Event containing the following |
| 58 | + * fields: |
| 59 | + * <ul> |
| 60 | + * <li>ConnectionID</li> |
| 61 | + * <li>DatabaseOperation</li> |
| 62 | + * <li>OriginalSqlText</li> |
| 63 | + * <li>ActualSqlText</li> |
| 64 | + * <li>User</li> |
| 65 | + * </ul> |
| 66 | + */ |
| 67 | + private static Event createEvent(DatabaseFunction databaseFunction) { |
| 68 | + String eventName = "oracle.jdbc.provider.jfr.roundtrip." + databaseFunction.toString(); |
| 69 | + List<AnnotationElement> eventAnnotations = new ArrayList<AnnotationElement>(); |
| 70 | + eventAnnotations |
| 71 | + .add(new AnnotationElement(Name.class, eventName)); |
| 72 | + eventAnnotations.add(new AnnotationElement(Label.class, databaseFunction.getDescription())); |
| 73 | + eventAnnotations.add(new AnnotationElement(Category.class, new String[] { "Oracle JDBC", "Round trips" })); |
| 74 | + |
| 75 | + List<ValueDescriptor> fields = new ArrayList<ValueDescriptor>(); |
| 76 | + fields.add(new ValueDescriptor(String.class, "Connection_ID")); |
| 77 | + fields.add(new ValueDescriptor(String.class, "Database_operation")); |
| 78 | + fields.add(new ValueDescriptor(String.class, "Database_tenant")); |
| 79 | + fields.add(new ValueDescriptor(String.class, "SQL_ID")); |
| 80 | + fields.add(new ValueDescriptor(String.class, "Original_SQL_text")); |
| 81 | + fields.add(new ValueDescriptor(String.class, "Actual_SQL_text")); |
| 82 | + fields.add(new ValueDescriptor(String.class, "Database_user")); |
| 83 | + |
| 84 | + EventFactory f = EventFactory.create(eventAnnotations, fields); |
| 85 | + return f.newEvent(); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
0 commit comments