Skip to content

Commit 0d17ff2

Browse files
author
Maia Engeli
authored
Merge pull request #51 from librato/custom_transaction_name_AO-7719
Custom transaction name ao 7719
2 parents 026c33a + 73fd38f commit 0d17ff2

35 files changed

+635
-245
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ext/oboe_metal/src/oboe.h
1515
ext/oboe_metal/src/oboe.hpp
1616
ext/oboe_metal/src/oboe_debug.h
1717
ext/oboe_metal/src/oboe_wrap.cxx
18+
ext/oboe_metal/src/bson/*
1819
log/
1920
.vagrant
2021
gemfiles/.bundle

.travis.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ cache:
55
directories:
66
- vendor/bundle
77

8+
env:
9+
- DBTYPE=mysql
10+
- DBTYPE=mysql2
11+
- DBTYPE=postgresql
12+
813
rvm:
914
- 2.5.0
1015
- 2.4.3
@@ -23,11 +28,6 @@ gemfile:
2328
- gemfiles/rails51.gemfile
2429
- gemfiles/delayed_job.gemfile
2530

26-
env:
27-
- DBTYPE=mysql
28-
- DBTYPE=mysql2
29-
- DBTYPE=postgresql
30-
3131
matrix:
3232
exclude:
3333
- rvm: 2.5.0
@@ -103,6 +103,8 @@ before_script:
103103
- psql -c 'create database travis_ci_test;' -U postgres
104104
- mysql -e 'create database travis_ci_test;'
105105
- sleep 10
106+
- export APPOPTICS_TOKEN_BUCKET_CAPACITY=1000
107+
- export APPOPTICS_TOKEN_BUCKET_RATE=1000
106108

107109
script: "N=1 bundle exec rake test"
108110

Dockerfile_test

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FROM ubuntu:16.04
22

3+
# to use with ./run_tests_docker.rb
4+
# docker build -f Dockerfile_test -t ruby_appoptics_apm .
5+
36
# install OS packages
47
RUN apt-get update \
58
&& apt-get install -y --no-install-recommends \
@@ -36,7 +39,7 @@ RUN . ~/.profile \
3639
&& rbenv install 2.3.6 \
3740
&& rbenv install 2.4.3 \
3841
&& rbenv install 2.5.0
39-
# && rbenv install jruby-9.0.5.0
42+
# && rbenv install jruby-9.1.16.0
4043

4144
# install swig 3.0.8
4245
RUN curl -SL http://kent.dl.sourceforge.net/project/swig/swig/swig-3.0.8/swig-3.0.8.tar.gz \
@@ -65,3 +68,5 @@ RUN apt-get update && \
6568
ENV PATH="/root/.rbenv/bin:/root/.rbenv/shims:$PATH"
6669
ENV RUBY_ENV=test
6770
ENV DOCKER_PSQL_PASS=docker
71+
ENV APPOPTICS_TOKEN_BUCKET_CAPACITY=1000
72+
ENV APPOPTICS_TOKEN_BUCKET_RATE=1000

Rakefile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,23 @@ task :fetch_ext_deps do
7373
end
7474

7575
# The c-lib version is different from the gem version
76-
oboe_version = ENV['OBOE_VERSION'] || '2.0.10' # 'latest'
77-
oboe_src_dir = "https://s3-us-west-2.amazonaws.com/rc-files-t2/c-lib/#{oboe_version}"
76+
oboe_version = ENV['OBOE_VERSION'] || 'latest'
77+
oboe_s3_dir = "https://s3-us-west-2.amazonaws.com/rc-files-t2/c-lib/#{oboe_version}"
7878
ext_src_dir = File.expand_path('ext/oboe_metal/src')
7979

8080
# VERSION is used by extconf.rb to download the correct liboboe when installing the gem
81-
%w(oboe.i oboe.h oboe.hpp oboe_debug.h VERSION).each do |filename|
82-
remote_file = File.join(oboe_src_dir, filename)
81+
remote_file = File.join(oboe_s3_dir, 'VERSION')
82+
local_file = File.join(ext_src_dir, 'VERSION')
83+
puts "fetching #{remote_file} to #{local_file}"
84+
open(remote_file, 'rb') do |rf|
85+
content = rf.read
86+
File.open(local_file, 'wb') { |f| f.puts content }
87+
end
88+
89+
# oboe and bson header files
90+
FileUtils.mkdir_p(File.join(ext_src_dir, 'bson'))
91+
%w(oboe.h oboe.hpp oboe_debug.h oboe.i bson/bson.h bson/platform_hacks.h).each do |filename|
92+
remote_file = File.join(oboe_s3_dir, 'include', filename)
8393
local_file = File.join(ext_src_dir, filename)
8494

8595
puts "fetching #{remote_file} to #{local_file}"
@@ -88,6 +98,7 @@ task :fetch_ext_deps do
8898
File.open(local_file, 'wb') { |f| f.puts content }
8999
end
90100
end
101+
91102
FileUtils.cd(ext_src_dir) do
92103
system('swig -c++ -ruby -module oboe_metal oboe.i')
93104
FileUtils.rm('oboe.i')

appoptics_apm.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ Gem::Specification.new do |s|
1515
s.description = %q{The AppOpticsAPM gem provides performance instrumentation for MRI Ruby and related frameworks.}
1616

1717
s.extra_rdoc_files = ["LICENSE"]
18-
s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|gemfiles|examples)/}) }
18+
s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|gemfiles)/}) }
1919
s.files += ['ext/oboe_metal/src/oboe.h',
2020
'ext/oboe_metal/src/oboe.hpp',
2121
'ext/oboe_metal/src/oboe_debug.h',
2222
'ext/oboe_metal/src/oboe_wrap.cxx',
23+
'ext/oboe_metal/src/bson/bson.h',
24+
'ext/oboe_metal/src/bson/platform_hacks.h',
2325
'ext/oboe_metal/src/VERSION']
2426

2527
# TODO this is commented out util we can actually provide gems for different platforms

ext/oboe_metal/src/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.10
1+
2.1.0

lib/appoptics_apm.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
require 'appoptics_apm/frameworks/sinatra'
6161
require 'appoptics_apm/frameworks/padrino'
6262
require 'appoptics_apm/frameworks/grape'
63+
else
64+
require 'appoptics_apm/noop/context'
6365
end
6466

6567
# Load Ruby module last. If there is no framework detected,

lib/appoptics_apm/api/logging.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Logging
4040
#
4141
# Returns nothing.
4242
def log(layer, label, opts = {}, event=nil)
43-
return if !AppOpticsAPM.tracing?
43+
return AppOpticsAPM::Context.toString unless AppOpticsAPM.tracing?
4444

4545
event ||= AppOpticsAPM::Context.createEvent
4646
log_event(layer, label, event, opts)
@@ -66,11 +66,11 @@ def log(layer, label, opts = {}, event=nil)
6666
#
6767
# Returns nothing.
6868
def log_exception(layer, exn, opts = {})
69-
return if !AppOpticsAPM.tracing? || exn.instance_variable_get(:@oboe_logged)
69+
return AppOpticsAPM::Context.toString if !AppOpticsAPM.tracing? || exn.instance_variable_get(:@oboe_logged)
7070

7171
unless exn
7272
AppOpticsAPM.logger.debug '[appoptics_apm/debug] log_exception called with nil exception'
73-
return
73+
return AppOpticsAPM::Context.toString
7474
end
7575

7676
opts.merge!(:ErrorClass => exn.class.name,
@@ -180,12 +180,11 @@ def log_start(layer, xtrace = nil, opts = {})
180180
#
181181
# Returns an xtrace metadata string if we are tracing
182182
def log_end(layer, opts = {})
183-
return unless AppOpticsAPM.tracing?
183+
return AppOpticsAPM::Context.toString unless AppOpticsAPM.tracing?
184184

185-
log_event(layer, :exit, AppOpticsAPM::Context.createEvent, opts) if AppOpticsAPM.tracing?
186-
AppOpticsAPM::Context.toString
185+
log_event(layer, :exit, AppOpticsAPM::Context.createEvent, opts)
187186
ensure
188-
# FIXME has_incoming_context commented out, it has importance for JRuby only and breaks ruby tests
187+
# FIXME has_incoming_context commented out, it has importance for JRuby only but breaks Ruby tests
189188
AppOpticsAPM::Context.clear # unless AppOpticsAPM.has_incoming_context?
190189
end
191190

@@ -206,7 +205,7 @@ def log_end(layer, opts = {})
206205
#
207206
# Returns an xtrace metadata string if we are tracing
208207
def log_entry(layer, opts = {}, op = nil)
209-
return unless AppOpticsAPM.tracing?
208+
return AppOpticsAPM::Context.toString unless AppOpticsAPM.tracing?
210209

211210
AppOpticsAPM.layer_op = op.to_sym if op
212211
log_event(layer, :entry, AppOpticsAPM::Context.createEvent, opts)
@@ -228,7 +227,7 @@ def log_entry(layer, opts = {}, op = nil)
228227
#
229228
# Returns an xtrace metadata string if we are tracing
230229
def log_info(layer, opts = {})
231-
return unless AppOpticsAPM.tracing?
230+
return AppOpticsAPM::Context.toString unless AppOpticsAPM.tracing?
232231

233232
log_event(layer, :info, AppOpticsAPM::Context.createEvent, opts)
234233
end
@@ -251,7 +250,7 @@ def log_info(layer, opts = {})
251250
#
252251
# Returns an xtrace metadata string if we are tracing
253252
def log_exit(layer, opts = {}, op = nil)
254-
return unless AppOpticsAPM.tracing?
253+
return AppOpticsAPM::Context.toString unless AppOpticsAPM.tracing?
255254

256255
AppOpticsAPM.layer_op = nil if op
257256
log_event(layer, :exit, AppOpticsAPM::Context.createEvent, opts)
@@ -270,7 +269,7 @@ def log_exit(layer, opts = {}, op = nil)
270269
# * +traces+ - An array with X-Trace strings returned from the requests
271270
#
272271
def log_multi_exit(layer, traces)
273-
return unless AppOpticsAPM.tracing?
272+
return AppOpticsAPM::Context.toString unless AppOpticsAPM.tracing?
274273
task_id = AppOpticsAPM::XTrace.task_id(AppOpticsAPM::Context.toString)
275274
event = AppOpticsAPM::Context.createEvent
276275
traces.each do |trace|
@@ -289,7 +288,7 @@ def log_multi_exit(layer, traces)
289288
# * +opts+ - A hash containing key/value pairs that will be reported along with this event
290289
def log_init(layer = :rack, opts = {})
291290
context = AppOpticsAPM::Metadata.makeRandom
292-
return unless context.isValid
291+
return AppOpticsAPM::Context.toString unless context.isValid
293292

294293
event = context.createEvent
295294
event.addInfo(APPOPTICS_STR_LAYER, layer.to_s)
@@ -299,6 +298,7 @@ def log_init(layer = :rack, opts = {})
299298
end
300299

301300
AppOpticsAPM::Reporter.sendStatus(event, context)
301+
AppOpticsAPM::Context.toString
302302
end
303303

304304
private
@@ -353,6 +353,7 @@ def log_event(layer, label, event, opts = {})
353353
end if !opts.nil? && opts.any?
354354

355355
AppOpticsAPM::Reporter.sendReport(event)
356+
AppOpticsAPM::Context.toString
356357
end
357358

358359
end

lib/appoptics_apm/api/tracing.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ module Tracing
5151
#
5252
# Returns the result of the block.
5353
def trace(layer, opts = {}, protect_op = nil)
54-
return if protect_op && AppOpticsAPM.layer_op == protect_op.to_sym
54+
return if !AppOpticsAPM.loaded || (protect_op && AppOpticsAPM.layer_op == protect_op.to_sym)
55+
5556
log_entry(layer, opts, protect_op)
5657
begin
5758
yield
@@ -158,6 +159,32 @@ def start_trace_with_target(layer, xtrace, target, opts = {})
158159
AppOpticsAPM::Context.clear
159160
end
160161
end
162+
163+
# Public: Set a ThreadLocal custom transaction name to be used when sending a trace or metrics for the
164+
# current transaction
165+
#
166+
# In addition to setting a transaction name here there is also a configuration
167+
# AppOpticsAPM::Config['transaction_name']['prepend_domain'] which allows to have the domain prepended
168+
# to the transaction name
169+
#
170+
# ===== Arguments
171+
# * +name+ - A non-empty string with the custom transaction name
172+
#
173+
def set_transaction_name(name)
174+
if name.is_a?(String) && name.strip != ''
175+
AppOpticsAPM.transaction_name = name
176+
else
177+
AppOpticsAPM.logger.debug "[appoptics_apm/api] Could not set transaction name, provided name is empty or not a String."
178+
end
179+
AppOpticsAPM.transaction_name
180+
end
181+
182+
183+
# this is provided for testing
184+
# returns the current transaction name
185+
def get_transaction_name
186+
AppOpticsAPM.transaction_name
187+
end
161188
end
162189
end
163190
end

lib/appoptics_apm/base.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ module AppOpticsAPMBase
4242
thread_local :sample_rate
4343
thread_local :layer
4444
thread_local :layer_op
45+
46+
# transaction_name is used for custom transaction naming
47+
# It needs to be globally accessible, but is only set by the request processors of the different frameworks
48+
# and read by rack
49+
thread_local :transaction_name
50+
4551
# Semaphore used during the test suite to test
4652
# global config options.
4753
thread_local :config_lock
@@ -54,7 +60,7 @@ module AppOpticsAPMBase
5460
# X-Trace request header, tracing may have already been started
5561
# by Joboe. Such a scenario occurs when the application is being
5662
# hosted by a Java container (such as Tomcat or Glassfish) and
57-
# JAppOpticsAPM has already initiated tracing. In this case, we shouldn't
63+
# AppOpticsAPM has already initiated tracing. In this case, we shouldn't
5864
# pickup the X-Trace context in the X-Trace header and we shouldn't
5965
# set the outgoing response X-Trace header or clear context.
6066
# Yeah I know. Yuck.

0 commit comments

Comments
 (0)