go-build
command.
custom_middleware_bundle
.
trace.NewSpanFromContext()
is a function that helps you create a new span from the current request context. When called, it returns two values: a fresh context with the newly created span embedded inside it, and the span itself. This method is particularly useful for tracing the execution of a piece of code within a web request, allowing you to measure and analyze its performance over time.
The function takes three parameters:
Context
: This is usually the current request’s context. However, you can also derive a new context from it, complete with timeouts and cancelations, to suit your specific needs.TracerName
: This is the identifier of the tracer that will be used to create the span. If you do not provide a name, the function will default to using the tyk
tracer.SpanName
: This parameter is used to set an initial name for the child span that is created. This name can be helpful for later identifying and referencing the span.GoPlugin_first-span
and it’s the first child of the GoPluginMiddleware
span.
trace.NewSpanFromContext()
can be further configured after its creation. You can modify its name and set its status:
AddFooBarHeader Testing
with an OK Status.
The second parameter of the SetStatus
method can accept a description parameter that is valid for ERROR statuses.
The available span statuses in ascending hierarchical order are:
SPAN_STATUS_UNSET
SPAN_STATUS_ERROR
SPAN_STATUS_OK
otel.status_code
tag with the OK status.
SetAttributes()
function allows you to set attributes on your spans, enriching each trace with additional, context-specific information.
The following example illustrates this functionality using the OpenTelemetry library’s implementation by Tyk
go_plugin
with a value of 1
on the span. This is just a demonstration; in practice, you might want to set attributes that carry meaningful data relevant to your tracing needs.
Attributes are key-value pairs. The value isn’t restricted to string data types; it can be any value, including numerical, boolean, or even complex data types, depending on your requirements. This provides flexibility and allows you to include rich, structured data within your spans.
The illustration below, shows how the go_plugin
attribute looks in Jaeger:
AddFooBarHeader
function creates a span and then calls NewFunc
, passing the updated context. The NewFunc
function starts a new span of its own, linked to the original through the context. It also simulates some processing time by sleeping for 1 second, then sets a new attribute on the second span. In a real-world scenario, the NewFunc
would contain actual code logic to be executed.
The illustration below, shows how this new child looks in Jaeger:
RecordError()
function records an error as an exception span event. However, this alone doesn’t change the span’s status to error. To mark the span as error, you need to make an additional call to the SetStatus()
function.
RecordError will record err as an exception span event for this span. An additional call to SetStatus is required if the Status of the Span should be set to Error, as this method does not change the Span status. If this span is not being recorded or err is nil then this method does nothing.Here’s an illustrative example with function calls generating a new span, setting attributes, setting an error status, and recording an error:
NewFuncWithError
function demonstrates error handling in OpenTelemetry. First, it creates a new span. Then it sets the status to error, and adds an attribute. Finally, it uses RecordError()
to log an error event. This two-step process ensures that both the error event is recorded and the span status is set to reflect the error.
""
(empty string): Overall server health"coprocess.Dispatcher"
: Specific service health for readiness checksNOT_SERVING
→ SERVING
(when ready)SERVING
→ NOT_SERVING
(immediate)SERVING
→ NOT_SERVING
(on failure)go.mod
: