doc.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2016 Datadog, Inc.
  5. // Package tracer contains Datadog's core tracing client. It is used to trace
  6. // requests as they flow across web servers, databases and microservices, giving
  7. // developers visibility into bottlenecks and troublesome requests. To start the
  8. // tracer, simply call the start method along with an optional set of options.
  9. // By default, the trace agent is considered to be found at "localhost:8126". In a
  10. // setup where this would be different (let's say 127.0.0.1:1234), we could do:
  11. //
  12. // tracer.Start(tracer.WithAgentAddr("127.0.0.1:1234"))
  13. // defer tracer.Stop()
  14. //
  15. // The tracing client can perform trace sampling. While the trace agent
  16. // already samples traces to reduce bandwidth usage, client sampling reduces
  17. // performance overhead. To make use of it, the package comes with a ready-to-use
  18. // rate sampler that can be passed to the tracer. To use it and keep only 30% of the
  19. // requests, one would do:
  20. //
  21. // s := tracer.NewRateSampler(0.3)
  22. // tracer.Start(tracer.WithSampler(s))
  23. //
  24. // More precise control of sampling rates can be configured using sampling rules.
  25. // This can be applied based on span name, service or both, and is used to determine
  26. // the sampling rate to apply. MaxPerSecond specifies max number of spans per second
  27. // that can be sampled per the rule and applies only to sampling rules of type
  28. // tracer.SamplingRuleSpan. If MaxPerSecond is not specified, the default is no limit.
  29. //
  30. // rules := []tracer.SamplingRule{
  31. // // sample 10% of traces with the span name "web.request"
  32. // tracer.NameRule("web.request", 0.1),
  33. // // sample 20% of traces for the service "test-service"
  34. // tracer.ServiceRule("test-service", 0.2),
  35. // // sample 30% of traces when the span name is "db.query" and the service
  36. // // is "postgres.db"
  37. // tracer.NameServiceRule("db.query", "postgres.db", 0.3),
  38. // // sample 100% of traces when service and name match these regular expressions
  39. // {Service: regexp.MustCompile("^test-"), Name: regexp.MustCompile("http\\..*"), Rate: 1.0},
  40. // // sample 50% of spans when service and name match these glob patterns with no limit on the number of spans
  41. // tracer.SpanNameServiceRule("^test-", "http\\..*", 0.5),
  42. // // sample 50% of spans when service and name match these glob patterns up to 100 spans per second
  43. // tracer.SpanNameServiceMPSRule("^test-", "http\\..*", 0.5, 100),
  44. // }
  45. // tracer.Start(tracer.WithSamplingRules(rules))
  46. // defer tracer.Stop()
  47. //
  48. // Sampling rules can also be configured at runtime using the DD_TRACE_SAMPLING_RULES and
  49. // DD_SPAN_SAMPLING_RULES environment variables. When set, it overrides rules set by tracer.WithSamplingRules.
  50. // The value is a JSON array of objects.
  51. // For trace sampling rules, the "sample_rate" field is required, the "name" and "service" fields are optional.
  52. // For span sampling rules, the "name" and "service", if specified, must be a valid glob pattern,
  53. // i.e. a string where "*" matches any contiguous substring, even an empty string,
  54. // and "?" character matches exactly one of any character.
  55. // The "sample_rate" field is optional, and if not specified, defaults to "1.0", sampling 100% of the spans.
  56. // The "max_per_second" field is optional, and if not specified, defaults to 0, keeping all the previously sampled spans.
  57. //
  58. // export DD_TRACE_SAMPLING_RULES='[{"name": "web.request", "sample_rate": 1.0}]'
  59. // export DD_SPAN_SAMPLING_RULES='[{"service":"test.?","name": "web.*", "sample_rate": 1.0, "max_per_second":100}]'
  60. //
  61. // To create spans, use the functions StartSpan and StartSpanFromContext. Both accept
  62. // StartSpanOptions that can be used to configure the span. A span that is started
  63. // with no parent will begin a new trace. See the function documentation for details
  64. // on specific usage. Each trace has a hard limit of 100,000 spans, after which the
  65. // trace will be dropped and give a diagnostic log message. In practice users should
  66. // not approach this limit as traces of this size are not useful and impossible to
  67. // visualize.
  68. //
  69. // See the contrib package ( https://pkg.go.dev/gopkg.in/DataDog/dd-trace-go.v1/contrib )
  70. // for integrating datadog with various libraries, frameworks and clients.
  71. //
  72. // All spans created by the tracer contain a context hereby referred to as the span
  73. // context. Note that this is different from Go's context. The span context is used
  74. // to package essential information from a span, which is needed when creating child
  75. // spans that inherit from it. Thus, a child span is created from a span's span context.
  76. // The span context can originate from within the same process, but also a
  77. // different process or even a different machine in the case of distributed tracing.
  78. //
  79. // To make use of distributed tracing, a span's context may be injected via a carrier
  80. // into a transport (HTTP, RPC, etc.) to be extracted on the other end and used to
  81. // create spans that are direct descendants of it. A couple of carrier interfaces
  82. // which should cover most of the use-case scenarios are readily provided, such as
  83. // HTTPCarrier and TextMapCarrier. Users are free to create their own, which will work
  84. // with our propagation algorithm as long as they implement the TextMapReader and TextMapWriter
  85. // interfaces. An example alternate implementation is the MDCarrier in our gRPC integration.
  86. //
  87. // As an example, injecting a span's context into an HTTP request would look like this:
  88. //
  89. // req, err := http.NewRequest("GET", "http://example.com", nil)
  90. // // ...
  91. // err := tracer.Inject(span.Context(), tracer.HTTPHeadersCarrier(req.Header))
  92. // // ...
  93. // http.DefaultClient.Do(req)
  94. //
  95. // Then, on the server side, to continue the trace one would do:
  96. //
  97. // sctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(req.Header))
  98. // // ...
  99. // span := tracer.StartSpan("child.span", tracer.ChildOf(sctx))
  100. //
  101. // In the same manner, any means can be used as a carrier to inject a context into a transport. Go's
  102. // context can also be used as a means to transport spans within the same process. The methods
  103. // StartSpanFromContext, ContextWithSpan and SpanFromContext exist for this reason.
  104. //
  105. // Some libraries and frameworks are supported out-of-the-box by using one
  106. // of our integrations. You can see a list of supported integrations here:
  107. // https://godoc.org/gopkg.in/DataDog/dd-trace-go.v1/contrib
  108. package tracer // import "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"