问题
I would like to have multiple DSNs in the tool I am writing. The tool is something that the users will give their own DSN to to track errors. I also want to track errors in my own DSN as well.
This was asked here as well, but the answer given was to have one global DSN, and then manually submit events through another Client.
Related and unanswered: How to use multiple DSN in Sentry (javascript/browser)
回答1:
Looking through the code, the most expedient and supportable way to do this appears to be to subclass Transport and make a very lightweight wrapper,
class MultiTransport(sentry_sdk.transport.Transport):
def __init__(self, options=None):
self.transports = []
options.pop('transport', None)
dsn = options.pop('dsn', [])
for d in dsn:
options['dsn'] = d
self.transports.append(
sentry_sdk.transport.HttpTransport(options)
)
super().__init__(options)
def capture_event(self, event):
for t in self.transports:
t.capture_event(event)
def flush(self, timeout, callback=None):
for t in self.transports:
t.flush(timeout, callback)
def kill(self):
for t in self.transports:
t.kill()
You initialize sentry with,
sentry = sentry_sdk.init(
transport=MultiTransport,
dsn=[dsn1, dsn2, dsn3])
I am unclear how exceptions in the for loops ought to be handled. Perhaps they should be handled and ignored so that the next DSN is also alerted.
Also, unclear is whether the flush()
callback
ought to be only called once, or once for each DSN.
As mentioned in the github answer, be sure to give a way for your users to opt out of your DSN as well as being careful what information may be relayed.
来源:https://stackoverflow.com/questions/58718196/how-to-have-multiple-global-dsn-in-sentry-sdk-in-python