{"size": 1717, "ext": "js", "max_stars_repo_path": "lib/core/span.js", "max_stars_repo_name": "capella/jaeger-tracer-node", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/core/span.js", "max_issues_repo_name": "capella/jaeger-tracer-node", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/core/span.js", "max_forks_repo_name": "capella/jaeger-tracer-node", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 16.6699029126, "max_line_length": 77, "alphanum_fraction": 0.5981362842}
const eventContext = require('tfg-event-context')
const opentracing = require('opentracing')

class Span {
  constructor (tracer, parent, operationName, tags) {
    this._tracer = tracer
    this._span = tracer.startSpan(operationName, {
      childOf: parent,
      tags: tags
    })

    let context = this._span.context()
    context.finalizeSampling()
  }

  startSpan (operationName, tags) {
    return new Span(this._tracer, this._span, operationName, tags)
  }

  propagate (cb) {
    let ctx = eventContext.createContext()
    let state = ctx.getState()

    if (state.span === this) {
      return cb()
    }

    state.span = this
    return ctx.run(cb)
  }

  inject (carrier) {
    this._tracer.inject(this._span, opentracing.FORMAT_HTTP_HEADERS, carrier)
  }

  setOperationName (operationName) {
    this._span.setOperationName(operationName)
  }

  setTag (tag, value) {
    this._span.setTag(tag, value)
  }

  addTags (tags) {
    this._span.addTags(tags)
  }

  finish (err) {
    if (err) {
      this._span.setTag('error', true)
      this._span.log({
        'event': 'error',
        'message': err.message,
        'stack': err.stack,
        'error.kind': err.name
      })
    }
    this._span.finish()
  }

  isValid () {
    return true
  }
}

Span.Mock = class SpanMock {
  constructor (tracer) {
    this._tracer = tracer
  }

  startSpan (operationName, tags) {
    if (this._tracer) {
      return new Span(this._tracer, null, operationName, tags)
    }
    return new Span.Mock()
  }

  propagate (cb) {
    cb()
  }

  inject () {
  }

  setOperationName () {
  }

  setTag () {
  }

  addTags () {
  }

  finish () {
  }

  isValid () {
    return false
  }
}

module.exports = Span
