eris

 view release on metacpan or  search on metacpan

lib/eris/log/context/caddy.pm  view on Meta::CPAN

package eris::log::context::caddy;
# ABSTRACT: Convert the caddy JSON structure to the CEE format

use Const::Fast;
use Moo;
use namespace::autoclean;
with qw(
    eris::role::context
);

our $VERSION = '0.009'; # VERSION


sub sample_messages {
    my @msgs = split /\r?\n/, <<'EOF';
Jul  4 23:37:51 app2 caddy[2140]: {"level":"info","ts":1751672271.1656768,"logger":"http.log.access.log7","msg":"handled request","request":{"remote_ip":"172.69.176.57","remote_port":"44552","client_ip":"172.69.176.57","proto":"HTTP/2.0","method":"GE...
Jul  4 23:37:52 app2 caddy[2140]: {"level":"info","ts":1751672272.5860772,"logger":"http.log.access.log7","msg":"handled request","request":{"remote_ip":"162.158.106.254","remote_port":"28088","client_ip":"162.158.106.254","proto":"HTTP/2.0","method"...
EOF
    return @msgs;
}


my %mapping = qw(
    method   action
    size     out_bytes
    status   status
);

sub contextualize_message {
    my ($self,$log) = @_;

    my $c = $log->context;

    my %ctx = ();
    if ( my $r = $c->{request} ) {
        $ctx{src_ip} = $r->{$_} for qw(remote_ip client_ip);
        if ( my $h = $r->{headers} ) {
            foreach my $k ( qw(X-Forwarded-For Cf-Connecting-Ip) ) {
                next unless $h->{$k};
                foreach my $v ( @{ $h->{$k} } ) {
                    $ctx{src_ip} = $v;
                }
            }
            if ( my $uas = $h->{'User-Agent'} ) {
                $ctx{prod} = $uas->[-1];
            }
        }

        $ctx{proto_app}  = $r->{proto};
        $ctx{dst} = $r->{host};
        $ctx{file} = $r->{uri};
    }

    foreach my $k ( keys %mapping ) {
        $ctx{$mapping{$k}} = $c->{$k} if length $c->{$k};
    }
    $ctx{response_ms} = 1000 * $ctx{duration} if $ctx{duration};

    $log->add_context($self->name,\%ctx) if keys %ctx;
}


1;

__END__

=pod

=head1 NAME

eris::log::context::caddy - Convert the caddy JSON structure to the CEE format

=head1 VERSION

version 0.009

=head1 CONSUMES

=over 4

=item * L<eris::role::context>

=item * L<eris::role::plugin>

=back

=head1 METHODS

=head2 contextualize_message

Converts the caddy JSON log into the CEE style of the eris schemas.

=for Pod::Coverage sample_messages

=head1 SEE ALSO

L<eris::log::contextualizer>, L<eris::role::context>



( run in 0.558 second using v1.01-cache-2.11-cpan-13bb782fe5a )