1
0

transaction.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package newrelic
  2. import (
  3. "net/http"
  4. )
  5. // Transaction represents a request or a background task.
  6. // Each Transaction should only be used in a single goroutine.
  7. type Transaction interface {
  8. // If StartTransaction is called with a non-nil http.ResponseWriter then
  9. // the Transaction may be used in its place. This allows
  10. // instrumentation of the response code and response headers.
  11. http.ResponseWriter
  12. // End finishes the current transaction, stopping all further
  13. // instrumentation. Subsequent calls to End will have no effect.
  14. End() error
  15. // Ignore ensures that this transaction's data will not be recorded.
  16. Ignore() error
  17. // SetName names the transaction. Transactions will not be grouped
  18. // usefully if too many unique names are used.
  19. SetName(name string) error
  20. // NoticeError records an error. The first five errors per transaction
  21. // are recorded (this behavior is subject to potential change in the
  22. // future).
  23. NoticeError(err error) error
  24. // AddAttribute adds a key value pair to the current transaction. This
  25. // information is attached to errors, transaction events, and error
  26. // events. The key must contain fewer than than 255 bytes. The value
  27. // must be a number, string, or boolean. Attribute configuration is
  28. // applied (see config.go).
  29. //
  30. // For more information, see:
  31. // https://docs.newrelic.com/docs/agents/manage-apm-agents/agent-metrics/collect-custom-attributes
  32. AddAttribute(key string, value interface{}) error
  33. // StartSegmentNow allows the timing of functions, external calls, and
  34. // datastore calls. The segments of each transaction MUST be used in a
  35. // single goroutine. Consumers are encouraged to use the
  36. // `StartSegmentNow` functions which checks if the Transaction is nil.
  37. // See segments.go
  38. StartSegmentNow() SegmentStartTime
  39. // CreateDistributedTracePayload creates a payload to link the calls
  40. // between transactions. This method never returns nil. Instead, it may
  41. // return a shim implementation whose methods return empty strings.
  42. // CreateDistributedTracePayload should be called every time an outbound
  43. // call is made since the payload contains a timestamp.
  44. //
  45. // StartExternalSegment calls CreateDistributedTracePayload, so you
  46. // should not need to use this method for typical outbound HTTP calls.
  47. // Just use StartExternalSegment!
  48. CreateDistributedTracePayload() DistributedTracePayload
  49. // AcceptDistributedTracePayload is used at the beginning of a
  50. // transaction to identify the caller.
  51. //
  52. // Application.StartTransaction calls this method automatically if a
  53. // payload is present in the request headers (under the key
  54. // DistributedTracePayloadHeader). Therefore, this method does not need
  55. // to be used for typical HTTP transactions.
  56. //
  57. // AcceptDistributedTracePayload should be used as early in the
  58. // transaction as possible. It may not be called after a call to
  59. // CreateDistributedTracePayload.
  60. //
  61. // The payload parameter may be a DistributedTracePayload or a string.
  62. AcceptDistributedTracePayload(t TransportType, payload interface{}) error
  63. }
  64. // DistributedTracePayload is used to instrument connections between
  65. // transactions and applications.
  66. type DistributedTracePayload interface {
  67. // HTTPSafe serializes the payload into a string containing http safe
  68. // characters.
  69. HTTPSafe() string
  70. // Text serializes the payload into a string. The format is slightly
  71. // more compact than HTTPSafe.
  72. Text() string
  73. }
  74. const (
  75. // DistributedTracePayloadHeader is the header used by New Relic agents
  76. // for automatic trace payload instrumentation.
  77. DistributedTracePayloadHeader = "Newrelic"
  78. )
  79. // TransportType represents the type of connection that the trace payload was
  80. // transported over.
  81. type TransportType struct{ name string }
  82. // TransportType names used across New Relic agents:
  83. var (
  84. TransportUnknown = TransportType{name: "Unknown"}
  85. TransportHTTP = TransportType{name: "HTTP"}
  86. TransportHTTPS = TransportType{name: "HTTPS"}
  87. TransportKafka = TransportType{name: "Kafka"}
  88. TransportJMS = TransportType{name: "JMS"}
  89. TransportIronMQ = TransportType{name: "IronMQ"}
  90. TransportAMQP = TransportType{name: "AMQP"}
  91. TransportQueue = TransportType{name: "Queue"}
  92. TransportOther = TransportType{name: "Other"}
  93. )