Knarr/Handler/Tracing.pm

filter: Statementstmt 84.2% 64 / 76 Branchbran 50.0% 6 / 12 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 3 totaltotal 74.7% 86 / 115 CC 9 SCAR 8.4
CC
9
Cov
77%
__ANON__
6.0
_open_trace
3.7
__ANON__
3.0
CRAP 2.3
« Knarr/CLI.pm ↑ Summary Knarr/Handler/RequestLog.pm »
1 package Langertha::Knarr::Handler::Tracing;
2 # ABSTRACT: Decorator handler that records every request as a Langfuse trace
3 2 our $VERSION = '1.100';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 with 'Langertha::Knarr::Handler';
11  
12  
13 # Wraps an inner Knarr::Handler with Langfuse tracing. Each chat or stream
14 # request opens a trace+generation via $tracing->start_trace, then closes
15 # it with the assistant text via end_trace once the wrapped handler is done.
16 # The attribute is named "wrapped" rather than "inner" because Moose
17 # imports an inner() keyword used for augmented methods.
18  
19 2 has wrapped => (
20 is => 'ro',
21 required => 1,
22 );
23  
24 # Anything implementing start_trace($opts) → $info / end_trace($info, %opts).
25 # In production this is a Langertha::Knarr::Tracing instance; tests can
26 # pass a mock that records calls.
27 2 has tracing => (
28 is => 'ro',
29 required => 1,
30 );
31  
32 # Optional: a label injected into the trace metadata's "engine" field when
33 # the wrapped handler doesn't have a more specific name.
34 has engine_label => (
35 is => 'ro',
36 isa => 'Maybe[Str]',
37 3 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 2 );
39  
40 sub _open_trace {
41 2 ▶ my ($self, $request) = @_;
Subroutine _open_trace: called CC=3 SCAR=13.1
42 2 ▶ return $self->tracing->start_trace(
Condition40%2/5
$request->model // ""
l!l
20
$self->engine_label // ref $self->wrapped
l!l&&r!l&&!r
020
MC/DC0%0/1
$request->model // ""
$request->model
MC/DC0%0/2
$self->engine_label // ref $self->wrapped
$self->engine_label ref $self->wrapped
Truth table50%1/2
A
A= $request->model
Aresult
11
00
Truth table33%1/3
A // B
A= $self->engine_labelB= ref $self->wrapped
ABresult
1X1
011
000
43 model => ( $request->model // '' ),
44 engine => ( $self->engine_label // ref( $self->wrapped ) ),
45 messages => $request->messages,
46 params => {
47 temperature => $request->temperature,
48 max_tokens => $request->max_tokens,
49 tools => $request->tools,
50 },
51 format => $request->protocol,
52 );
53 }
54  
55 sub _close_trace {
56 1 ▶ my ($self, $trace, $r) = @_;
Subroutine _close_trace: called CC=2 SCAR=7.1
57 1 my $resp = Langertha::Knarr::Response->coerce($r);
58 1 ▶ $self->tracing->end_trace(
Branch: 1 branch
BranchTrueFalseTotal
$resp->usage ? : 0 1 1
59 $trace,
60 output => $resp->content,
61 model => $resp->model,
62 ( $resp->usage ? ( usage => $resp->usage ) : () ),
63 );
64 }
65  
66 1 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=2 SCAR=7.6
Pod undocumented
67 1 my ($self, $session, $request) = @_;
68 1 my $trace = $self->_open_trace($request);
69 1 my $result = eval { $self->wrapped->handle_chat_f( $session, $request ) };
70 1 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 1 1
71 0 $self->tracing->end_trace( $trace, error => "$err" );
72 0 die $err;
73 }
74 my $f = $result->then( sub {
75 1 ▶ my ($r) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 $self->_close_trace( $trace, $r );
77 1 return Future->done($r);
78 })->else( sub {
79 0 ▶ my ($err) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 $self->tracing->end_trace( $trace, error => "$err" );
81 0 return Future->fail($err);
82 1 });
83 1 return await $f;
84 2 }
85  
86 1 ▶async sub handle_stream_f {
Subroutine handle_stream_f: called CC=2 SCAR=7.4
Pod undocumented
87 1 my ($self, $session, $request) = @_;
88 1 my $trace = $self->_open_trace($request);
89  
90 1 my $upstream_stream;
91 1 my $err = do {
92 1 local $@;
93 1 eval { $upstream_stream = $self->wrapped->handle_stream_f( $session, $request )->get; };
94 1 $@;
95 };
96 1 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 1 1
97 0 $self->tracing->end_trace( $trace, error => "$err" );
98 0 die $err;
99 }
100  
101 1 my $accumulated = '';
102 1 my $closed = 0;
103  
104 return Langertha::Knarr::Stream->new(
105 source => sub {
106 $upstream_stream->next_chunk_f->then( sub {
107 4 ▶ my ($delta) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
108 4 ▶ if ( defined $delta ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delta) 3 1 4
109 3 $accumulated .= $delta;
110 3 return Future->done($delta);
111 }
112 1 ▶ unless ( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed) 1 0 1
113 1 $closed = 1;
114 1 $self->tracing->end_trace(
115 $trace,
116 output => $accumulated,
117 model => $request->model,
118 );
119 }
120 1 return Future->done(undef);
121 })->else( sub {
122 0 ▶ my ($e) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
123 0 ▶ unless ( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed) 0 0 0
124 0 $closed = 1;
125 0 $self->tracing->end_trace( $trace, error => "$e" );
126 }
127 0 return Future->fail($e);
128 4 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
129 },
130 1 );
131 2 }
132  
133 1 ▶sub list_models { $_[0]->wrapped->list_models }
Subroutine list_models: called CC=1 SCAR=0.0
Pod undocumented
134  
135 2 __PACKAGE__->meta->make_immutable;
136 2 1;
137  
138 __END__
139  
140 =pod
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 Langertha::Knarr::Handler::Tracing - Decorator handler that records every request as a Langfuse trace
147  
148 =head1 VERSION
149  
150 version 1.100
151  
152 =head1 SYNOPSIS
153  
154 use Langertha::Knarr::Tracing;
155 use Langertha::Knarr::Handler::Tracing;
156  
157 my $tracing = Langertha::Knarr::Tracing->new(config => $config);
158 $handler = Langertha::Knarr::Handler::Tracing->new(
159 wrapped => $handler,
160 tracing => $tracing,
161 );
162  
163 =head1 DESCRIPTION
164  
165 Decorator handler that opens a Langfuse trace + generation around every
166 chat or stream request and closes it with the assistant text once the
167 inner handler resolves (or fails). Streaming requests accumulate every
168 delta into a single output before closing the trace, so the Langfuse
169 view shows the full assembled response.
170  
171 C<knarr start> mounts this automatically when
172 the config supplies Langfuse credentials.
173  
174 =head2 wrapped
175  
176 Required. The inner L<Langertha::Knarr::Handler> being decorated. The
177 attribute is named C<wrapped> rather than C<inner> because Moose
178 imports an C<inner()> keyword used for augmented methods.
179  
180 =head2 tracing
181  
182 Required. A L<Langertha::Knarr::Tracing> instance (or any object
183 implementing C<start_trace> / C<end_trace>).
184  
185 =head2 engine_label
186  
187 Optional. A string injected into the trace metadata's C<engine> field.
188 Defaults to C<< ref($self->wrapped) >>.
189  
190 =head1 SUPPORT
191  
192 =head2 Issues
193  
194 Please report bugs and feature requests on GitHub at
195 L<https://github.com/Getty/langertha-knarr/issues>.
196  
197 =head2 IRC
198  
199 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
200  
201 =head1 CONTRIBUTING
202  
203 Contributions are welcome! Please fork the repository and submit a pull request.
204  
205 =head1 AUTHOR
206  
207 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
208  
209 =head1 COPYRIGHT AND LICENSE
210  
211 This software is copyright (c) 2026 by Torsten Raudssus.
212  
213 This is free software; you can redistribute it and/or modify it under
214 the same terms as the Perl 5 programming language system itself.
215  
216 =cut
« Knarr/CLI.pm ↑ Summary Knarr/Handler/RequestLog.pm »