Sha256: 84315f8e38c8f218482148cbfe6b491cc829a9bc2088689c3c4241e82166bda7

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

require 'ffi'
require 'fiddle'

$libm = Fiddle.dlopen(File.expand_path(File.dirname(__FILE__)) + '/thread_id/thread_id.so')


module Rust
  extend FFI::Library
  ffi_lib File.expand_path(File.dirname(__FILE__)) + '/rbspy/rbspy.' + FFI::Platform::LIBSUFFIX
  attach_function :initialize_agent, [:string, :string, :int, :bool, :string], :bool
  attach_function :add_tag, [:uint64, :string, :string], :bool
  attach_function :remove_tag, [:uint64, :string, :string], :bool
  attach_function :drop_agent, [], :bool
end

module Pyroscope
  Config = Struct.new(:application_name, :server_address, :sample_rate, :detect_subprocesses, :log_level, :tags) do
    def initialize(*)
      super
      self.application_name ||= ''
      self.server_address ||= 'http://localhost:4040'
      self.sample_rate ||= 100
      self.detect_subprocesses ||= true
      self.log_level ||= 'info'
      self.tags ||= []
    end
  end

  class << self
    def configure
      @config = Config.new

      # Pass config to the block
      yield @config

      Rust.initialize_agent(
        @config.application_name,
        @config.server_address,
        @config.sample_rate,
        @config.detect_subprocesses,
        tags_to_string(@config.tags)
      )

      puts @config
    end

    def tag_wrapper(tags)
      add_tags(tags)
      begin
        yield
      ensure
        remove_tags(tags)
      end
    end

    def drop
      Rust.drop_agent
    end
end
end

# convert tags object to string
def tags_to_string(tags)
  tags.map { |k, v| "#{k}=#{v}" }.join(',')
end

# get thread id
def thread_id
  thread_id = Fiddle::Function.new($libm['thread_id'], [], Fiddle::TYPE_INT64_T)
  thread_id.call
end

# add tags
def add_tags(tags)
  tags.each do |tag_name, tag_value|
    thread_id = thread_id()
    Rust.add_tag(thread_id, tag_name.to_s, tag_value.to_s)
  end
end

# remove tags
def remove_tags(tags)
  tags.each do |tag_name, tag_value|
    thread_id = thread_id()
    Rust.remove_tag(thread_id, tag_name.to_s, tag_value.to_s)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pyroscope_beta-0.1.4-x86_64-linux lib/pyroscope_beta.rb
pyroscope_beta-0.1.4 lib/pyroscope_beta.rb
pyroscope_beta-0.1.3 lib/pyroscope_beta.rb
pyroscope_beta-0.1.2 lib/pyroscope_beta.rb
pyroscope_beta-0.1.1 lib/pyroscope_beta.rb