Skip to content

Commit 67e34ee

Browse files
committed
bug fixes for tracing
1 parent ecd91db commit 67e34ee

File tree

9 files changed

+154
-64
lines changed

9 files changed

+154
-64
lines changed

marklogic-data-hub/src/main/resources/ml-config/databases/trace-database.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{
2929
"scalar-type": "string",
3030
"namespace-uri": "",
31-
"localname": "trace-id",
31+
"localname": "traceId",
3232
"collation": "http://marklogic.com/collation/codepoint",
3333
"range-value-positions": false,
3434
"invalid-values": "reject"

marklogic-data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/lib/trace-lib.xqy

Lines changed: 121 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare variable $FORMAT-JSON := "json";
3434
declare %private variable $current-trace-settings := map:map();
3535

3636
declare %private variable $current-trace := map:new((
37-
map:entry("trace-id", xdmp:random()),
37+
map:entry("traceId", xdmp:random()),
3838
map:entry("created", fn:current-dateTime())
3939
));
4040

@@ -74,16 +74,17 @@ declare function trace:write-trace()
7474
xdmp:to-json($current-trace)
7575
else
7676
element trace {
77-
element trace-id { map:get($current-trace, "trace-id") },
77+
element format { map:get($current-trace, "format") },
78+
element traceId { map:get($current-trace, "traceId") },
7879
element created { map:get($current-trace, "created") },
7980
element identifier { map:get($current-trace, "identifier") },
80-
element flow-type { map:get($current-trace, "flow-type") },
81-
for $key in ("collector-plugin", "content-plugin", "headers-plugin", "triples-plugin", "writer-plugin")
81+
element flowType { map:get($current-trace, "flowType") },
82+
for $key in ("collectorPlugin", "contentPlugin", "headersPlugin", "triplesPlugin", "writerPlugin")
8283
let $m := map:get($current-trace, $key)
8384
return
8485
if (fn:exists($m)) then
8586
element { $key } {
86-
element plugin-module-uri { map:get($m, "plugin-module-uri") },
87+
element pluginModuleUri { map:get($m, "pluginModuleUri") },
8788
element input { map:get($m, "input") },
8889
element output { map:get($m, "output") },
8990
element duration { map:get($m, "duration") }
@@ -99,10 +100,10 @@ declare function trace:write-trace()
99100
declare variable $trace external;
100101
101102
xdmp:document-insert(
102-
"/" || $trace/trace-id,
103+
"/" || $trace/traceId,
103104
$trace,
104105
xdmp:default-permissions(),
105-
($trace/*:type)
106+
("trace", $trace/*:type)
106107
)
107108
',
108109
map:new((
@@ -119,60 +120,74 @@ declare function trace:plugin-trace(
119120
$identifier,
120121
$module-uri,
121122
$plugin-type as xs:string,
122-
$flow-type as xs:string,
123+
$flowType as xs:string,
123124
$input,
124125
$output,
125126
$duration as xs:dayTimeDuration)
126127
{
128+
let $format :=
129+
let $o :=
130+
if ($input instance of document-node()) then
131+
$input/node()
132+
else
133+
$input
134+
return
135+
typeswitch($o)
136+
case element() return "xml"
137+
case object-node() return "json"
138+
case array-node() return "json"
139+
default return "xml"
140+
return
141+
map:put($current-trace, "format", $format),
127142
map:put($current-trace, "identifier", $identifier),
128-
map:put($current-trace, "flow-type", $flow-type),
143+
map:put($current-trace, "flowType", $flowType),
129144
let $plugin-map := map:new((
130-
map:entry("plugin-module-uri", $module-uri),
145+
map:entry("pluginModuleUri", $module-uri),
131146
map:entry("input", $input),
132147
map:entry("output", $output),
133148
map:entry("duration", $duration)
134149
))
135150
return
136-
map:put($current-trace, $plugin-type || "-plugin", $plugin-map)
151+
map:put($current-trace, $plugin-type || "Plugin", $plugin-map)
137152
};
138153

139154
declare function trace:error-trace(
140155
$identifier as xs:string?,
141156
$module-uri as xs:string,
142157
$plugin-type as xs:string,
143-
$flow-type as xs:string,
158+
$flowType as xs:string,
144159
$input,
145160
$error as element(error:error),
146161
$duration as xs:dayTimeDuration)
147162
{
163+
let $format :=
164+
let $o :=
165+
if ($input instance of document-node()) then
166+
$input/node()
167+
else
168+
$input
169+
return
170+
typeswitch($o)
171+
case element() return "xml"
172+
case object-node() return "json"
173+
case array-node() return "json"
174+
default return "xml"
175+
return
176+
map:put($current-trace, "format", $format),
148177
map:put($current-trace, "identifier", $identifier),
149-
map:put($current-trace, "flow-type", $flow-type),
178+
map:put($current-trace, "flowType", $flowType),
150179
map:put($current-trace-settings, "_has_errors", fn:true()),
151180
let $plugin-map := map:new((
152-
map:entry("plugin-module-uri", $module-uri),
181+
map:entry("pluginModuleUri", $module-uri),
153182
map:entry("input", $input),
154183
map:entry("error", $error),
155184
map:entry("duration", $duration)
156185
))
157-
let $_ := map:put($current-trace, $plugin-type || "-plugin", $plugin-map)
186+
let $_ := map:put($current-trace, $plugin-type || "Plugin", $plugin-map)
158187
let $_ := trace:write-trace()
159188
return ()
160189
};
161190

162-
declare function trace:camel-case( $str as xs:string ) as xs:string
163-
{
164-
if( fn:contains($str , "-" ) ) then
165-
let $subs := fn:tokenize($str,"-")
166-
return
167-
fn:string-join((
168-
$subs[1],
169-
for $s in $subs[ fn:position() gt 1 ]
170-
return (
171-
fn:upper-case( fn:substring($s , 1 , 1 )) , fn:lower-case(fn:substring($s,2)))),"")
172-
else $str
173-
174-
};
175-
176191
declare function trace:_walk_json($nodes as node()* ,$o)
177192
{
178193
let $quote-options :=
@@ -184,26 +199,56 @@ declare function trace:_walk_json($nodes as node()* ,$o)
184199
for $n in $nodes
185200
return
186201
typeswitch($n)
202+
case array-node() return
203+
let $name as xs:string := fn:string(fn:node-name($n))
204+
return
205+
map:put($o, $name, xdmp:quote($n))
206+
case object-node() return
207+
let $oo := json:object()
208+
let $name as xs:string := fn:string(fn:node-name($n))
209+
return
210+
if ($name = "input") then
211+
if ($n/node()) then
212+
let $_ :=
213+
for $x in $n/node()
214+
let $nn := fn:string(fn:node-name($x))
215+
return
216+
map:put($oo, $nn, xdmp:quote($x, $quote-options))
217+
return
218+
map:put($o, $name, $oo)
219+
else
220+
map:put($o, $name, null-node {})
221+
else if ($name = "output") then
222+
map:put($o, $name, xdmp:quote($n, $quote-options))
223+
else
224+
let $_ := trace:_walk_json($n/node(), $oo)
225+
return
226+
map:put($o, $name, $oo)
227+
case number-node() |
228+
boolean-node() |
229+
null-node() |
230+
text() return
231+
map:put($o, fn:string(fn:node-name($n)), fn:data($n))
187232
case element(input) return
188233
let $oo := json:object()
189234
let $_ :=
190235
for $x in $n/*
191236
return
192-
map:put($oo, trace:camel-case(fn:local-name($x)), xdmp:quote($x, $quote-options))
237+
map:put($oo, fn:local-name($x), xdmp:quote($x, $quote-options))
193238
return
194239
map:put($o, "input", $oo)
195240
case element(output) return
196-
map:put($o, trace:camel-case(fn:local-name($n)), xdmp:quote($n/node(), $quote-options))
241+
map:put($o, fn:local-name($n), xdmp:quote($n/node(), $quote-options))
197242
case element(duration) return
198243
map:put($o, "duration", fn:seconds-from-duration(xs:dayTimeDuration($n)))
199244
case element() return
200245
if ($n/*) then
201246
let $oo := json:object()
202247
let $_ := trace:_walk_json($n/*, $oo)
203248
return
204-
map:put($o, trace:camel-case(fn:local-name($n)), $oo)
249+
map:put($o, fn:local-name($n), $oo)
205250
else
206-
map:put($o, trace:camel-case(fn:local-name($n)), fn:data($n))
251+
map:put($o, fn:local-name($n), fn:data($n))
207252
default return
208253
$n
209254
};
@@ -212,7 +257,14 @@ declare function trace:trace-to-json($trace)
212257
{
213258
if ($trace instance of element()) then
214259
let $o := json:object()
215-
let $_ := trace:_walk_json($trace/*, $o)
260+
let $walk-me :=
261+
let $n := $trace/node()
262+
return
263+
if ($n instance of object-node()) then
264+
$n/node()
265+
else
266+
$n
267+
let $_ := trace:_walk_json($walk-me, $o)
216268
return
217269
$o
218270
else
@@ -221,15 +273,26 @@ declare function trace:trace-to-json($trace)
221273

222274
declare function trace:trace-to-json-slim($trace)
223275
{
224-
let $o := json:object()
225-
let $_ := (
226-
map:put($o, "traceId", $trace/trace-id/string()),
227-
map:put($o, "created", $trace/created/string()),
228-
map:put($o, "identifier", $trace/identifier/string()),
229-
map:put($o, "flowType", $trace/flow-type/string())
230-
)
231-
return
232-
$o
276+
(:if ($trace instance of element()) then:)
277+
let $o := json:object()
278+
let $_ := (
279+
map:put($o, "traceId", $trace/traceId/string()),
280+
map:put($o, "created", $trace/created/string()),
281+
map:put($o, "identifier", $trace/identifier/string()),
282+
map:put($o, "flowType", $trace/flowType/string())
283+
)
284+
return
285+
$o
286+
(: else
287+
let $o := json:object()
288+
let $_ := (
289+
map:put($o, "traceId", $trace/traceId),
290+
map:put($o, "created", $trace/created),
291+
map:put($o, "identifier", $trace/identifier),
292+
map:put($o, "flowType", $trace/flowType)
293+
)
294+
return
295+
$o:)
233296
};
234297

235298
declare function trace:find-traces(
@@ -239,11 +302,16 @@ declare function trace:find-traces(
239302
{
240303
let $options :=
241304
<options xmlns="http://marklogic.com/appservices/search">
305+
<additional-query>{cts:collection-query("trace")}</additional-query>
242306
<return-results>false</return-results>
243307
<return-facets>true</return-facets>
308+
<sort-order type="xs:dateTime"
309+
direction="descending">
310+
<element ns="" name="created"/>
311+
</sort-order>
244312
</options>
245313
let $query := search:parse($q)
246-
let $count := xdmp:estimate(cts:search(fn:doc(), cts:query($query)))
314+
let $count := search:estimate($query, $options)
247315
let $start := ($page - 1) * $page-length + 1
248316
let $end := fn:min(($start + $page-length - 1, $count))
249317
let $results :=
@@ -283,19 +351,24 @@ declare function trace:get-traces($page as xs:int, $page-length as xs:int)
283351

284352
declare function trace:get-trace($id as xs:string)
285353
{
286-
trace:trace-to-json(/trace[trace-id = $id])
354+
let $query :=
355+
cts:or-query((
356+
cts:element-range-query(xs:QName("traceId"), "=", $id, ("collation=http://marklogic.com/collation/codepoint")),
357+
cts:json-property-range-query("traceId", "=", $id, ("collation=http://marklogic.com/collation/codepoint"))
358+
))
359+
return
360+
trace:trace-to-json(cts:search(fn:doc(), $query)[1])
287361
};
288362

289-
declare function trace:get-trace-ids($q as xs:string?)
363+
declare function trace:get-traceIds($q as xs:string?)
290364
{
291365
let $query :=
292366
if ($q) then
293367
cts:element-value-query(xs:QName("identifier"), fn:lower-case($q) || "*", "wildcarded")
294368
else ()
295-
let $_ := xdmp:log($query)
296369
let $results :=
297370
cts:element-value-co-occurrences(
298-
xs:QName("trace-id"),
371+
xs:QName("traceId"),
299372
xs:QName("identifier"),
300373
(
301374
"limit=10",

marklogic-data-hub/src/trace-ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"codemirror": "^5.15.0",
2626
"eventemitter2": "^0.4.14",
2727
"font-awesome": "^4.3.0",
28+
"jsonformatter": "^0.6.0",
2829
"lodash": "^3.9.3",
2930
"material-design-icons": "^2.0.0",
3031
"moment": "^2.13.0",

marklogic-data-hub/src/trace-ui/src/bootstrap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'codemirror/mode/javascript/javascript';
1010

1111
import 'angular-ui-codemirror';
1212
import 'angular-moment';
13+
import 'jsonformatter';
14+
import 'jsonformatter/dist/json-formatter.min.css';
1315

1416
import {bootstrap} from 'ng-forward';
1517
import {componentHooks} from 'ng-forward/cjs/decorators/component';
@@ -23,4 +25,4 @@ componentHooks._extendDDO.push((ddo) => {
2325
ddo.controllerAs = 'ctrl';
2426
});
2527

26-
bootstrap(App, ['ui.router', 'ui.codemirror', 'angularMoment']);
28+
bootstrap(App, ['ui.router', 'ui.codemirror', 'angularMoment', 'jsonFormatter']);

marklogic-data-hub/src/trace-ui/src/components/trace-search/trace-search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class TraceSearch {
3838
}
3939

4040
search() {
41-
const q = this.query || '*';
41+
const q = this.query || '';
4242
const p = this.page || 1;
4343
return this.traceService.search(q, p).then(resp => {
4444
this.searchResponse = resp.data;

marklogic-data-hub/src/trace-ui/src/components/trace/trace.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<section class="plugin-nav" layout="row" layout-align="center center">
2-
<md-content class="md-whiteframe-2dp" flex="66">
2+
<md-content class="md-whiteframe-2dp" flex>
33
<md-subheader>
44
<div>Trace {{ctrl.trace.traceId}}</div>
55
<div>Identifier: {{ctrl.trace.identifier}}</div>
@@ -35,8 +35,9 @@ <h3 class="ng-binding">Input</h3>
3535
</div>
3636
</md-toolbar>
3737
<md-content layout-margin>
38-
<md-subheader ng-init="collapsed = false" ng-click="collapsed = !collapsed" ng-repeat-start="(key, value) in ctrl.currentPlugin.input">{{key}} <md-icon>keyboard_arrow_down</md-icon></md-subheader>
39-
<div class="cm" ng-if="!collapsed" ng-repeat-end ui-codemirror="ctrl.codeMirrorConfig" ng-model="value"></div>
38+
<md-subheader ng-init="collapsed = false" ng-click="collapsed = !collapsed" ng-repeat-start="(key, value) in ctrl.currentPlugin.input">{{key}} <md-icon>{{ collapsed ? 'keyboard_arrow_left' : 'keyboard_arrow_down'}}</md-icon></md-subheader>
39+
<json-formatter class="json-formatter" ng-if="!collapsed && ctrl.trace.format === 'json'" json="value" open="1"></json-formatter>
40+
<div class="cm" ng-if="!collapsed && ctrl.trace.format === 'xml'" ng-repeat-end ui-codemirror="ctrl.codeMirrorConfig" ng-model="value"></div>
4041
</md-content>
4142
</div>
4243
<div class="md-whiteframe-z1" flex>
@@ -45,9 +46,13 @@ <h3 class="ng-binding">Input</h3>
4546
<h3 class="ng-binding">output</h3>
4647
</div>
4748
</md-toolbar>
48-
<md-content layout-margin ng-init="ctrl.outputCollapsed = false" >
49-
<md-subheader ng-click="ctrl.outputCollapsed = !ctrl.outputCollapsed" ng-if="ctrl.currentPlugin.output">Output <md-icon>keyboard_arrow_down</md-icon></md-subheader>
50-
<div class="cm" ng-if="!ctrl.outputCollapsed && ctrl.currentPlugin.output" ui-codemirror="ctrl.codeMirrorConfig" ng-model="ctrl.currentPlugin.output"></div>
49+
<md-content layout-margin ng-init="ctrl.outputCollapsed = false; ctrl.errorCollapsed = false" >
50+
<md-subheader ng-click="ctrl.outputCollapsed = !ctrl.outputCollapsed" ng-if="ctrl.currentPlugin.output">Output <md-icon>{{ ctrl.outputCollapsed ? 'keyboard_arrow_left' : 'keyboard_arrow_down'}}</md-icon></md-subheader>
51+
<json-formatter class="json-formatter" ng-if="!ctrl.outputCollapsed && ctrl.currentPlugin.output && ctrl.trace.format === 'json'" json="ctrl.currentPlugin.output" open="1"></json-formatter>
52+
<div class="cm" ng-if="!ctrl.outputCollapsed && ctrl.currentPlugin.output && ctrl.trace.format === 'xml'" ui-codemirror="ctrl.codeMirrorConfig" ng-model="ctrl.currentPlugin.output"></div>
53+
54+
<md-subheader ng-click="ctrl.errorCollapsed = !ctrl.errorCollapsed" ng-if="ctrl.currentPlugin.error">Error <md-icon>{{ ctrl.errorCollapsed ? 'keyboard_arrow_left' : 'keyboard_arrow_down'}}</md-icon></md-subheader>
55+
<div class="cm" ng-if="!ctrl.errorCollapsed && ctrl.currentPlugin.error" ui-codemirror="ctrl.codeMirrorConfig" ng-model="ctrl.currentPlugin.error"></div>
5156
</md-content>
5257
</div>
5358
</div>

0 commit comments

Comments
 (0)