all files / src/ thrift.js

100% Statements 58/58
93.75% Branches 15/16
100% Functions 4/4
100% Lines 55/55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                          158× 158× 561×   561×   561× 561× 561× 561× 561×   561× 561× 561× 133× 133× 428× 13× 13× 415× 414×   413× 413× 412×         561×                     158×       13× 13×           13×       13× 13×                         13×       13× 13× 13×   13×                              
// @flow
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.
 
import fs from 'fs';
import opentracing from 'opentracing';
import path from 'path';
import { Thrift } from 'thriftrw';
import Utils from './util.js';
 
export default class ThriftUtils {
  static _thrift = new Thrift({
    source: fs.readFileSync(path.join(__dirname, './jaeger-idl/thrift/jaeger.thrift'), 'ascii'),
    allowOptionalArguments: true,
  });
  static emptyBuffer: Buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0]);
 
  static getThriftTags(initialTags: Array<Tag>): Array<any> {
    let thriftTags = [];
    for (let i = 0; i < initialTags.length; i++) {
      let tag = initialTags[i];
 
      let key: string = tag.key;
 
      let vLong: Buffer = ThriftUtils.emptyBuffer;
      let vBinary: Buffer = ThriftUtils.emptyBuffer;
      let vBool: boolean = false;
      let vDouble: number = 0;
      let vStr: string = '';
 
      let vType: string = '';
      let valueType = typeof tag.value;
      if (valueType === 'number') {
        vType = ThriftUtils._thrift.TagType.DOUBLE;
        vDouble = tag.value;
      } else if (valueType === 'boolean') {
        vType = ThriftUtils._thrift.TagType.BOOL;
        vBool = tag.value;
      } else if (tag.value instanceof Buffer) {
        vType = ThriftUtils._thrift.TagType.BINARY;
        vBinary = tag.value;
      } else if (valueType === 'object') {
        vType = ThriftUtils._thrift.TagType.STRING;
        vStr = JSON.stringify(tag.value);
      } else {
        vType = ThriftUtils._thrift.TagType.STRING;
        if (valueType === 'string') {
          vStr = tag.value;
        } else {
          vStr = String(tag.value);
        }
      }
 
      thriftTags.push({
        key: key,
        vType: vType,
        vStr: vStr,
        vDouble: vDouble,
        vBool: vBool,
        vLong: vLong,
        vBinary: vBinary,
      });
    }
 
    return thriftTags;
  }
 
  static getThriftLogs(logs: Array<LogData>): Array<any> {
    let thriftLogs = [];
    for (let i = 0; i < logs.length; i++) {
      let log = logs[i];
      thriftLogs.push({
        timestamp: Utils.encodeInt64(log.timestamp * 1000), // to microseconds
        fields: ThriftUtils.getThriftTags(log.fields),
      });
    }
 
    return thriftLogs;
  }
 
  static spanRefsToThriftRefs(refs: Array<Reference>): Array<any> {
    let thriftRefs = [];
    for (let i = 0; i < refs.length; i++) {
      let refEnum;
      let ref = refs[i];
      let context = refs[i].referencedContext();
 
      if (ref.type() === opentracing.REFERENCE_CHILD_OF) {
        refEnum = ThriftUtils._thrift.SpanRefType.CHILD_OF;
      } else Eif (ref.type() === opentracing.REFERENCE_FOLLOWS_FROM) {
        refEnum = ThriftUtils._thrift.SpanRefType.FOLLOWS_FROM;
      } else {
        continue;
      }
 
      thriftRefs.push({
        refType: refEnum,
        traceIdLow: context.traceId,
        traceIdHigh: ThriftUtils.emptyBuffer,
        spanId: context.spanId,
      });
    }
 
    return thriftRefs;
  }
 
  static spanToThrift(span: Span): any {
    let tags = ThriftUtils.getThriftTags(span._tags);
    let logs = ThriftUtils.getThriftLogs(span._logs);
    let unsigned = true;
 
    return {
      traceIdLow: span._spanContext.traceId,
      traceIdHigh: ThriftUtils.emptyBuffer, // TODO(oibe) implement 128 bit ids
      spanId: span._spanContext.spanId,
      parentSpanId: span._spanContext.parentId || ThriftUtils.emptyBuffer,
      operationName: span._operationName,
      references: ThriftUtils.spanRefsToThriftRefs(span._references),
      flags: span._spanContext.flags,
      startTime: Utils.encodeInt64(span._startTime * 1000), // to microseconds
      duration: Utils.encodeInt64(span._duration * 1000), // to microseconds
      tags: tags,
      logs: logs,
    };
  }
}