| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
var htrace = htrace || {}; |
| 21 |
|
| 22 |
// Parse an ISO8601 date string into a moment.js object. |
| 23 |
htrace.parseDate = function(val) { |
| 24 |
if (val.match(/^[0-9]([0-9]*)$/)) { |
| 25 |
// Treat an all-numeric field as UTC milliseconds since the epoch. |
| 26 |
return moment.utc(parseInt(val, 10)); |
| 27 |
} |
| 28 |
// Look for approved date formats. |
| 29 |
var toTry = [ |
| 30 |
"YYYY-MM-DDTHH:mm:ss,SSS", |
| 31 |
"YYYY-MM-DDTHH:mm:ss", |
| 32 |
"YYYY-MM-DDTHH:mm", |
| 33 |
"YYYY-MM-DD" |
| 34 |
]; |
| 35 |
for (var i = 0; i < toTry.length; i++) { |
| 36 |
var m = moment.utc(val, toTry[i], true); |
| 37 |
if (m.isValid()) { |
| 38 |
return m; |
| 39 |
} |
| 40 |
} |
| 41 |
throw "Please enter the date either as YYYY-MM-DDTHH:mm:ss,SSS " + |
| 42 |
"in UTC, or as the number of milliseconds since the epoch."; |
| 43 |
}; |
| 44 |
|
| 45 |
// Convert a moment.js moment into an ISO8601-style date string. |
| 46 |
htrace.dateToString = function(val) { |
| 47 |
return moment.utc(val).format("YYYY-MM-DDTHH:mm:ss,SSS"); |
| 48 |
}; |
| 49 |
|
| 50 |
// Normalize a span ID into the format the server expects to see-- |
| 51 |
// i.e. something like 00000000000000000000000000000000. |
| 52 |
htrace.normalizeSpanId = function(str) { |
| 53 |
if (str.length != 32) { |
| 54 |
throw "The length of '" + str + "' was " + str.length + |
| 55 |
", but span IDs must be 32 characters long."; |
| 56 |
} |
| 57 |
if (str.search(/[^0-9a-fA-F]/) != -1) { |
| 58 |
throw "Span IDs must contain only hexadecimal digits, but '" + str + |
| 59 |
"' contained invalid characters."; |
| 60 |
} |
| 61 |
return str; |
| 62 |
}; |