Conditions | 12 |
Total Lines | 55 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like telemetry.OtelHandler.Handle often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package telemetry |
||
62 | func (h OtelHandler) Handle(ctx context.Context, record slog.Record) error { |
||
63 | if ctx == nil { |
||
64 | return h.Next.Handle(ctx, record) |
||
65 | } |
||
66 | |||
67 | if !h.NoBaggage { |
||
68 | // Adding context baggage members to log record. |
||
69 | b := baggage.FromContext(ctx) |
||
70 | for _, m := range b.Members() { |
||
71 | record.AddAttrs(slog.String(m.Key(), m.Value())) |
||
72 | } |
||
73 | } |
||
74 | |||
75 | span := trace.SpanFromContext(ctx) |
||
76 | if span == nil || !span.IsRecording() { |
||
77 | return h.Next.Handle(ctx, record) |
||
78 | } |
||
79 | |||
80 | if !h.NoTraceEvents { |
||
81 | // Adding log info to span event. |
||
82 | eventAttrs := make([]attribute.KeyValue, 0, record.NumAttrs()) |
||
83 | eventAttrs = append(eventAttrs, attribute.String(slog.MessageKey, record.Message)) |
||
84 | eventAttrs = append(eventAttrs, attribute.String(slog.LevelKey, record.Level.String())) |
||
85 | eventAttrs = append(eventAttrs, attribute.String(slog.TimeKey, record.Time.Format(time.RFC3339Nano))) |
||
86 | record.Attrs(func(attr slog.Attr) bool { |
||
87 | otelAttr := h.slogAttrToOtelAttr(attr) |
||
88 | if otelAttr.Valid() { |
||
89 | eventAttrs = append(eventAttrs, otelAttr) |
||
90 | } |
||
91 | |||
92 | return true |
||
93 | }) |
||
94 | |||
95 | span.AddEvent("LogRecord", trace.WithAttributes(eventAttrs...)) |
||
96 | } |
||
97 | |||
98 | // Adding span info to log record. |
||
99 | spanContext := span.SpanContext() |
||
100 | if spanContext.HasTraceID() { |
||
101 | traceID := spanContext.TraceID().String() |
||
102 | record.AddAttrs(slog.String("TraceId", traceID)) |
||
103 | } |
||
104 | |||
105 | if spanContext.HasSpanID() { |
||
106 | spanID := spanContext.SpanID().String() |
||
107 | record.AddAttrs(slog.String("SpanId", spanID)) |
||
108 | } |
||
109 | |||
110 | // Setting span status if the log is an error. |
||
111 | // Purposely leaving as codes.Unset (default) otherwise. |
||
112 | if record.Level >= slog.LevelError { |
||
113 | span.SetStatus(codes.Error, record.Message) |
||
114 | } |
||
115 | |||
116 | return h.Next.Handle(ctx, record) |
||
117 | } |
||
202 |