in operatortrace-go/pkg/client/tracing_client.go [185:240]
func (tc *tracingClient) EndTrace(ctx context.Context, obj client.Object, opts ...client.PatchOption) (client.Object, error) {
ctx, span := startSpanFromContext(ctx, tc.Logger, tc.Tracer, obj, tc.scheme, fmt.Sprintf("EndTrace %s %s", obj.GetObjectKind().GroupVersionKind().Kind, obj.GetName()))
defer span.End()
annotations := obj.GetAnnotations()
if annotations == nil {
return obj, nil
}
// get the current object and ensure that current object has the expected traceid and spanid annotations
currentObjFromServer := obj.DeepCopyObject().(client.Object)
err := tc.Reader.Get(ctx, client.ObjectKeyFromObject(obj), currentObjFromServer)
if err != nil {
span.RecordError(err)
}
// compare the traceid and spanid from currentobj to ensure that the traceid and spanid are not changed
if currentObjFromServer.GetAnnotations()[constants.TraceIDAnnotation] != obj.GetAnnotations()[constants.TraceIDAnnotation] {
tc.Logger.Info("TraceID has changed, skipping patch", "object", obj.GetName())
span.RecordError(fmt.Errorf("TraceID has changed, skipping patch: object %s", obj.GetName()))
return obj, nil
}
// Remove the traceid and spanid annotations and create a patch
original := obj.DeepCopyObject().(client.Object)
patch := client.MergeFrom(original)
delete(annotations, constants.TraceIDAnnotation)
delete(annotations, constants.SpanIDAnnotation)
obj.SetAnnotations(annotations)
tc.Logger.Info("Patching object", "object", obj.GetName())
// Use the Patch function to apply the patch
err = tc.Client.Patch(ctx, obj, patch, opts...)
if err != nil {
span.RecordError(err)
}
original = obj.DeepCopyObject().(client.Object)
// remove the traceid and spanid conditions from the object and create a status().patch
deleteConditionAsMap("TraceID", obj, tc.scheme)
deleteConditionAsMap("SpanID", obj, tc.scheme)
patch = client.MergeFrom(original)
tc.Logger.Info("Patching object status", "object", obj.GetName())
err = tc.Client.Status().Patch(ctx, obj, patch)
if err != nil {
span.RecordError(err)
}
return obj, err
}