29
29
import graphql .schema .idl .SchemaParser ;
30
30
import graphql .schema .idl .TypeDefinitionRegistry ;
31
31
import org .openqa .selenium .grid .distributor .Distributor ;
32
+ import org .openqa .selenium .internal .Require ;
32
33
import org .openqa .selenium .json .Json ;
33
34
import org .openqa .selenium .remote .http .Contents ;
34
35
import org .openqa .selenium .remote .http .HttpHandler ;
35
36
import org .openqa .selenium .remote .http .HttpMethod ;
36
37
import org .openqa .selenium .remote .http .HttpRequest ;
37
38
import org .openqa .selenium .remote .http .HttpResponse ;
39
+ import org .openqa .selenium .remote .tracing .AttributeKey ;
40
+ import org .openqa .selenium .remote .tracing .EventAttribute ;
41
+ import org .openqa .selenium .remote .tracing .EventAttributeValue ;
42
+ import org .openqa .selenium .remote .tracing .Span ;
43
+ import org .openqa .selenium .remote .tracing .Tracer ;
38
44
39
45
import java .io .IOException ;
40
46
import java .io .InputStream ;
50
56
import static org .openqa .selenium .json .Json .JSON_UTF_8 ;
51
57
import static org .openqa .selenium .json .Json .MAP_TYPE ;
52
58
import static org .openqa .selenium .remote .http .Contents .utf8String ;
59
+ import static org .openqa .selenium .remote .tracing .HttpTracing .newSpanAsChildOf ;
60
+ import static org .openqa .selenium .remote .tracing .Tags .HTTP_RESPONSE ;
61
+ import static org .openqa .selenium .remote .tracing .Tags .HTTP_RESPONSE_EVENT ;
62
+ import static org .openqa .selenium .remote .tracing .Tags .HTTP_REQUEST ;
63
+ import static org .openqa .selenium .remote .tracing .Tags .HTTP_REQUEST_EVENT ;
53
64
54
65
public class GraphqlHandler implements HttpHandler {
55
66
56
67
public static final String GRID_SCHEMA = "/org/openqa/selenium/grid/graphql/selenium-grid-schema.graphqls" ;
57
68
public static final Json JSON = new Json ();
69
+ private final Tracer tracer ;
58
70
private final Distributor distributor ;
59
71
private final URI publicUri ;
60
72
private final GraphQL graphQl ;
61
73
62
- public GraphqlHandler (Distributor distributor , URI publicUri ) {
63
- this .distributor = Objects .requireNonNull (distributor );
64
- this .publicUri = Objects .requireNonNull (publicUri );
74
+
75
+ public GraphqlHandler (Tracer tracer , Distributor distributor , URI publicUri ) {
76
+ this .distributor = Require .nonNull ("Distributor" , distributor );
77
+ this .publicUri = Require .nonNull ("Uri" , publicUri );
78
+ this .tracer = Require .nonNull ("Tracer" , tracer );
65
79
66
80
GraphQLSchema schema = new SchemaGenerator ()
67
81
.makeExecutableSchema (buildTypeDefinitionRegistry (), buildRuntimeWiring ());
@@ -88,34 +102,66 @@ public GraphqlHandler(Distributor distributor, URI publicUri) {
88
102
89
103
@ Override
90
104
public HttpResponse execute (HttpRequest req ) throws UncheckedIOException {
91
- Map <String , Object > inputs = JSON .toType (Contents .string (req ), MAP_TYPE );
105
+ try (Span span = newSpanAsChildOf (tracer , req , "grid.status" )) {
106
+ HttpResponse response ;
107
+ Map <String , Object > inputs = JSON .toType (Contents .string (req ), MAP_TYPE );
92
108
93
- if (!(inputs .get ("query" ) instanceof String )) {
94
- return new HttpResponse ()
95
- .setStatus (HTTP_INTERNAL_ERROR )
96
- .setContent (Contents .utf8String ("Unable to find query" ));
97
- }
109
+ Map <String , EventAttributeValue > attributeMap = new HashMap <>();
110
+ attributeMap .put (AttributeKey .LOGGER_CLASS .getKey (),
111
+ EventAttribute .setValue (getClass ().getName ()));
98
112
99
- String query = (String ) inputs .get ("query" );
100
- @ SuppressWarnings ("unchecked" ) Map <String , Object > variables = inputs .get ("variables" ) instanceof Map ?
101
- (Map <String , Object >) inputs .get ("variables" ) :
102
- new HashMap <>();
113
+ HTTP_REQUEST .accept (span , req );
114
+ HTTP_REQUEST_EVENT .accept (attributeMap , req );
103
115
104
- ExecutionInput executionInput = ExecutionInput .newExecutionInput (query )
105
- .variables (variables )
106
- .build ();
116
+ if (!(inputs .get ("query" ) instanceof String )) {
117
+ response = new HttpResponse ()
118
+ .setStatus (HTTP_INTERNAL_ERROR )
119
+ .setContent (Contents .utf8String ("Unable to find query" ));
107
120
108
- ExecutionResult result = graphQl .execute (executionInput );
121
+ HTTP_RESPONSE .accept (span , response );
122
+ HTTP_RESPONSE_EVENT .accept (attributeMap , response );
109
123
110
- if (result .isDataPresent ()) {
111
- return new HttpResponse ()
112
- .addHeader ("Content-Type" , JSON_UTF_8 )
113
- .setContent (utf8String (JSON .toJson (result .toSpecification ())));
114
- }
124
+ attributeMap .put (AttributeKey .EXCEPTION_MESSAGE .getKey (),
125
+ EventAttribute .setValue ("Unable to find query" ));
126
+ span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
127
+ return response ;
128
+ }
129
+
130
+ String query = (String ) inputs .get ("query" );
131
+ @ SuppressWarnings ("unchecked" ) Map <String , Object > variables = inputs .get ("variables" ) instanceof Map ?
132
+ (Map <String , Object >) inputs .get ("variables" ) :
133
+ new HashMap <>();
134
+
135
+ ExecutionInput executionInput = ExecutionInput .newExecutionInput (query )
136
+ .variables (variables )
137
+ .build ();
138
+
139
+ ExecutionResult result = graphQl .execute (executionInput );
115
140
116
- return new HttpResponse ()
117
- .setStatus (HTTP_INTERNAL_ERROR )
118
- .setContent (utf8String (JSON .toJson (result .getErrors ())));
141
+ if (result .isDataPresent ()) {
142
+ response = new HttpResponse ()
143
+ .addHeader ("Content-Type" , JSON_UTF_8 )
144
+ .setContent (utf8String (JSON .toJson (result .toSpecification ())));
145
+
146
+ HTTP_RESPONSE .accept (span , response );
147
+ HTTP_RESPONSE_EVENT .accept (attributeMap , response );
148
+ span .addEvent ("Graphql query executed" , attributeMap );
149
+
150
+ return response ;
151
+ }
152
+
153
+ response = new HttpResponse ()
154
+ .setStatus (HTTP_INTERNAL_ERROR )
155
+ .setContent (utf8String (JSON .toJson (result .getErrors ())));
156
+ HTTP_RESPONSE .accept (span , response );
157
+ HTTP_RESPONSE_EVENT .accept (attributeMap , response );
158
+
159
+ attributeMap .put (AttributeKey .EXCEPTION_MESSAGE .getKey (),
160
+ EventAttribute .setValue ("Error while executing the query" ));
161
+ span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
162
+
163
+ return response ;
164
+ }
119
165
}
120
166
121
167
private RuntimeWiring buildRuntimeWiring () {
0 commit comments