1<!DOCTYPE html>
2<!--
3Copyright (c) 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/importer/importer.html">
9
10<script>
11
12'use strict';
13
14/**
15 * @fileoverview GcloudTraceImporter imports JSON data from Google Cloud Trace.
16 */
17tr.exportTo('tr.e.importer.gcloud_trace', function() {
18  function GcloudTraceImporter(model, eventData) {
19    this.importPriority = 2;
20    this.eventData_ = eventData;
21  }
22
23  /**
24   * @return {boolean} Whether obj looks like the JSON output from Cloud Trace.
25   */
26  GcloudTraceImporter.canImport = function(eventData) {
27    if (typeof(eventData) !== 'string' && !(eventData instanceof String))
28      return false;
29
30    // Slice the data so we don't potentially do a replace on a gigantic string.
31    var normalizedEventData = eventData.slice(0, 20).replace(/\s/g, '');
32    if (normalizedEventData.length < 14)
33      return false;
34
35    return normalizedEventData.slice(0, 14) == '{"projectId":"';
36  };
37
38  GcloudTraceImporter.prototype = {
39
40    __proto__: tr.importer.Importer.prototype,
41
42    get importerName() {
43      return 'GcloudTraceImporter';
44    },
45
46    /**
47     * Called by the Model to extract subtraces from the event data. The
48     * subtraces are passed on to other importers that can recognize them.
49     */
50    extractSubtraces: function() {
51      var traceEvents = this.createEventsForTrace();
52      return traceEvents ? [traceEvents] : [];
53    },
54
55    createEventsForTrace: function() {
56      var events = [];
57      var trace = JSON.parse(this.eventData_);
58      var spanLength = trace.spans.length;
59      for (var i = 0; i < spanLength; i++) {
60        events.push(this.createEventForSpan(trace.traceId, trace.spans[i]));
61      }
62      return {
63        'traceEvents': events
64      };
65    },
66
67    createEventForSpan: function(traceId, span) {
68      var newArgs = {};
69      if (span.labels) {
70        newArgs = JSON.parse(JSON.stringify(span.labels));
71      }
72      newArgs['Span ID'] = span.spanId;
73      newArgs['Start Time'] = span.startTime;
74      newArgs['End Time'] = span.endTime;
75      if (span.parentSpanId) {
76        newArgs['Parent Span ID'] = span.parentSpanId;
77      }
78      // The timestamps are ISO-standard strings, which are parsed to millis,
79      // then converted to the micros that the trace viewer expects.
80      return {
81        name: span.name,
82        args: newArgs,
83        pid: traceId,
84        ts: Date.parse(span.startTime) * 1000,
85        dur: (Date.parse(span.endTime) - Date.parse(span.startTime)) * 1000,
86        cat: 'tracespan',
87        tid: traceId,
88        ph: 'X'
89      };
90    }
91  };
92
93  tr.importer.Importer.register(GcloudTraceImporter);
94
95  return {
96    GcloudTraceImporter: GcloudTraceImporter
97  };
98});
99</script>
100