-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathapplication.py
189 lines (141 loc) · 6.02 KB
/
application.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright 2010 New Relic, 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 threading
import warnings
import newrelic.api.import_hook
import newrelic.core.agent
import newrelic.core.config
class Application:
_lock = threading.Lock()
_instances = {}
_delayed_callables = {}
@staticmethod
def _instance(name, activate=True):
if name is None:
name = newrelic.core.config.global_settings().app_name
# Try first without lock. If we find it we can return.
instance = Application._instances.get(name, None)
if not instance and activate:
# Ensure we grab a reference to the agent before grabbing
# the lock, else startup callback on agent initialisation
# could deadlock as it tries to create a application when
# we already have the lock held.
agent = newrelic.core.agent.agent_instance()
with Application._lock:
# Now try again with lock so that only one gets
# to create and add it.
instance = Application._instances.get(name, None)
if not instance:
instance = Application(name, agent)
Application._instances[name] = instance
return instance
@staticmethod
def run_on_initialization(name, callback):
Application._delayed_callables[name] = callback
def __init__(self, name, agent=None):
self._name = name
self._linked = {}
self.enabled = True
if agent is None:
agent = newrelic.core.agent.agent_instance()
self._agent = agent
callback = Application._delayed_callables.get(name)
if callback:
callback(self)
@property
def name(self):
return self._name
@property
def global_settings(self):
return self._agent.global_settings()
@property
def settings(self):
global_settings = self._agent.global_settings()
if global_settings.debug.ignore_all_server_settings:
return global_settings
return self._agent.application_settings(self._name)
@property
def active(self):
return self.settings is not None
def activate(self, timeout=None):
# If timeout not supplied then the default from the global
# configuration will later be used. Note that the timeout only
# applies on the first call to activate the application.
self._agent.activate_application(
self._name, self._linked, timeout, newrelic.api.import_hook._uninstrumented_modules
)
def shutdown(self):
pass
@property
def linked_applications(self):
return list(self._linked.keys())
def link_to_application(self, name):
self._linked[name] = True
def record_exception(self, exc=None, value=None, tb=None, params=None, ignore_errors=None):
# Deprecation Warning
warnings.warn(
("The record_exception function is deprecated. Please use the new api named notice_error instead."),
DeprecationWarning,
stacklevel=2,
)
self.notice_error(error=(exc, value, tb), attributes=params, ignore=ignore_errors)
def notice_error(self, error=None, attributes=None, expected=None, ignore=None, status_code=None):
if not self.active:
return
self._agent.notice_error(
self._name, error=error, attributes=attributes, expected=expected, ignore=ignore, status_code=status_code
)
def record_custom_metric(self, name, value):
if self.active:
self._agent.record_custom_metric(self._name, name, value)
def record_custom_metrics(self, metrics):
if self.active and metrics:
self._agent.record_custom_metrics(self._name, metrics)
def record_dimensional_metric(self, name, value, tags=None):
if self.active:
self._agent.record_dimensional_metric(self._name, name, value, tags)
def record_dimensional_metrics(self, metrics):
if self.active and metrics:
self._agent.record_dimensional_metrics(self._name, metrics)
def record_custom_event(self, event_type, params):
if self.active:
self._agent.record_custom_event(self._name, event_type, params)
def record_ml_event(self, event_type, params):
if self.active:
self._agent.record_ml_event(self._name, event_type, params)
def record_transaction(self, data):
if self.active:
self._agent.record_transaction(self._name, data)
def record_log_event(self, message, level=None, timestamp=None, attributes=None, priority=None):
if self.active:
self._agent.record_log_event(
self._name, message, level, timestamp, attributes=attributes, priority=priority
)
def normalize_name(self, name, rule_type="url"):
if self.active:
return self._agent.normalize_name(self._name, name, rule_type)
return name, False
def compute_sampled(self):
if not self.active or not self.settings.distributed_tracing.enabled:
return False
return self._agent.compute_sampled(self._name)
def application_instance(name=None, activate=True):
return Application._instance(name, activate=activate)
def register_application(name=None, timeout=None):
instance = application_instance(name)
instance.activate(timeout)
return instance
def application_settings(name=None):
instance = application_instance(name)
return instance.settings