all files / src/ thrift.js

100% Statements 55/55
92.86% Branches 13/14
100% Functions 4/4
100% Lines 52/52
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 137 138 139 140 141                                                          131× 131× 347×   347×   347× 347× 347× 347× 347×   347× 347× 347× 107× 107× 240× 12× 12× 228×   227× 227× 226×         347×                     131×       17× 17×           17×       17× 17×                         17×       17× 17× 17×   17×                              
// @flow
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
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 {
                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
        }
    }
}