| 1 | # Copyright 2018 - present MongoDB, Inc. | ||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||
| 4 | # you may not use this file except in compliance with the License. | ||||||||||||||||||||||
| 5 | # You may obtain a copy of the License at | ||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||
| 12 | # See the License for the specific language governing permissions and | ||||||||||||||||||||||
| 13 | # limitations under the License. | ||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | 60 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | 60 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | package MongoDB::Role::_CommandMonitoring; | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | # MongoDB role to add command monitoring support to Ops | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 60 | ▶ | use version; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 23 | 60 | our $VERSION = 'v2.2.2'; | |||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 60 | ▶ | use Moo::Role; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 26 | 60 | ▶ | use BSON; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 27 | 60 | ▶ | use BSON::Raw; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 28 | 60 | ▶ | use MongoDB::_Types -types, 'to_IxHash'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 29 | 60 | ▶ | use Tie::IxHash; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | 60 | ▶ | use Safe::Isa; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 31 | 60 | ▶ | use Time::HiRes qw/time/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 32 | 60 | ▶ | use namespace::clean; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 60 | requires qw/monitoring_callback db_name/; | |||||||||||||||||||||
| 35 | 60 | has command_start_time => ( is => 'rw', ); | |||||||||||||||||||||
| 36 | 60 | has command_start_event => ( is => 'rw', ); | |||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | sub publish_command_started { | ||||||||||||||||||||
|
Subroutine
publish_command_started: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||
| 39 | 0 | my ( $self, $link, $command, $request_id ) = @_; | |||||||||||||||||||||
| 40 | 0 | ▶ | return unless $self->monitoring_callback; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | 0 | ▶ | if ( $command->$_can('_as_tied_hash') ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 43 | 0 | $command = $command->_as_tied_hash; | |||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||
| 45 | 0 | $command = _to_tied_ixhash($command); | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | 0 | my $command_name = tied(%$command)->Keys(0); | |||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 0 | ▶ | my $event = { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | type => 'command_started', | ||||||||||||||||||||||
| 51 | databaseName => $self->db_name, | ||||||||||||||||||||||
| 52 | commandName => $command_name, | ||||||||||||||||||||||
| 53 | command => ( | ||||||||||||||||||||||
| 54 | _needs_redaction($command_name) | ||||||||||||||||||||||
| 55 | ? _to_tied_ixhash([]) | ||||||||||||||||||||||
| 56 | : $command, | ||||||||||||||||||||||
| 57 | ), | ||||||||||||||||||||||
| 58 | requestId => $request_id, | ||||||||||||||||||||||
| 59 | connectionId => $link->address, | ||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | # Cache for constructing matching succeeded/failed event later | ||||||||||||||||||||||
| 63 | 0 | $self->command_start_event($event); | |||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | # Guard against exceptions in the callback | ||||||||||||||||||||||
| 66 | 0 | eval { $self->monitoring_callback->($event) }; | |||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | # Set the time last so it doesn't include all the work above | ||||||||||||||||||||||
| 69 | 0 | $self->command_start_time(time); | |||||||||||||||||||||
| 70 | 0 | return; | |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | 0 | ▶ | sub publish_command_reply { | ||||||||||||||||||||
|
Subroutine
publish_command_reply: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||
| 74 | 0 | my ( $self, $bson ) = @_; | |||||||||||||||||||||
| 75 | 0 | ▶ | return unless $self->monitoring_callback; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | # Record duration early before doing work to prepare success/fail | ||||||||||||||||||||||
| 78 | # events | ||||||||||||||||||||||
| 79 | 0 | my $duration = time - $self->command_start_time(); | |||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 0 | my $start_event = $self->command_start_event(); | |||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | 0 | ▶ | my $reply = | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 84 | ref($bson) eq 'HASH' | ||||||||||||||||||||||
| 85 | ? $bson | ||||||||||||||||||||||
| 86 | : BSON->new()->decode_one($bson); | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | my $event = { | ||||||||||||||||||||||
| 89 | databaseName => $start_event->{databaseName}, | ||||||||||||||||||||||
| 90 | commandName => $start_event->{commandName}, | ||||||||||||||||||||||
| 91 | requestId => $start_event->{requestId}, | ||||||||||||||||||||||
| 92 | connectionId => $start_event->{connectionId}, | ||||||||||||||||||||||
| 93 | durationSecs => $duration, | ||||||||||||||||||||||
| 94 | reply => ( | ||||||||||||||||||||||
| 95 | _needs_redaction($start_event->{commandName}) | ||||||||||||||||||||||
| 96 | 0 | ▶ | ? {} | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 97 | : $reply, | ||||||||||||||||||||||
| 98 | ), | ||||||||||||||||||||||
| 99 | }; | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 0 | ▶ | if ( $reply->{ok} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 102 | 0 | $event->{type} = 'command_succeeded'; | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||
| 105 | 0 | $event->{type} = 'command_failed'; | |||||||||||||||||||||
| 106 | 0 | $event->{failure} = _extract_errmsg($reply); | |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | # Guard against exceptions in the callback | ||||||||||||||||||||||
| 110 | 0 | eval { $self->monitoring_callback->($event) }; | |||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 0 | return; | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 0 | ▶ | sub publish_command_exception { | ||||||||||||||||||||
|
Subroutine
publish_command_exception: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||
| 116 | 0 | my ($self, $err) = @_; | |||||||||||||||||||||
| 117 | 0 | ▶ | return unless $self->monitoring_callback; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | # Record duration early before doing work to prepare success/fail | ||||||||||||||||||||||
| 120 | # events | ||||||||||||||||||||||
| 121 | 0 | my $duration = time - $self->command_start_time(); | |||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 0 | my $start_event = $self->command_start_event(); | |||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | my $event = { | ||||||||||||||||||||||
| 126 | type => "command_failed", | ||||||||||||||||||||||
| 127 | databaseName => $start_event->{databaseName}, | ||||||||||||||||||||||
| 128 | commandName => $start_event->{commandName}, | ||||||||||||||||||||||
| 129 | requestId => $start_event->{requestId}, | ||||||||||||||||||||||
| 130 | connectionId => $start_event->{connectionId}, | ||||||||||||||||||||||
| 131 | 0 | durationSecs => $duration, | |||||||||||||||||||||
| 132 | reply => {}, | ||||||||||||||||||||||
| 133 | failure => "$err", | ||||||||||||||||||||||
| 134 | eval_error => $err, | ||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | # Guard against exceptions in the callback | ||||||||||||||||||||||
| 138 | 0 | eval { $self->monitoring_callback->($event) }; | |||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | 0 | ▶ | sub publish_legacy_write_started { | ||||||||||||||||||||
|
Subroutine
publish_legacy_write_started: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 144 | 0 | my ( $self, $link, $cmd_name, $op_doc, $request_id ) = @_; | |||||||||||||||||||||
| 145 | 0 | my $method = "_convert_legacy_$cmd_name"; | |||||||||||||||||||||
| 146 | 0 | return $self->publish_command_started( $link, $self->$method($op_doc), $request_id ); | |||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 0 | ▶ | sub publish_legacy_reply_succeeded { | ||||||||||||||||||||
|
Subroutine
publish_legacy_reply_succeeded: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||
| 150 | 0 | my ($self, $result) = @_; | |||||||||||||||||||||
| 151 | 0 | ▶ | my $batchfield = ref($self) eq "MongoDB::Op::_Query" ? "firstBatch" : "nextBatch"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | my $reply = { | ||||||||||||||||||||||
| 154 | ok => 1, | ||||||||||||||||||||||
| 155 | cursor => { | ||||||||||||||||||||||
| 156 | id => $result->{cursor_id}, | ||||||||||||||||||||||
| 157 | ns => $self->full_name, | ||||||||||||||||||||||
| 158 | 0 | $batchfield => [ @{$result->{docs}} ], | |||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||
| 160 | }; | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 0 | return $self->publish_command_reply($reply); | |||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | 0 | ▶ | sub publish_legacy_query_error { | ||||||||||||||||||||
|
Subroutine
publish_legacy_query_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 166 | 0 | my ($self, $result) = @_; | |||||||||||||||||||||
| 167 | |||||||||||||||||||||||
| 168 | 0 | my $reply = { | |||||||||||||||||||||
| 169 | %$result, | ||||||||||||||||||||||
| 170 | ok => 0, | ||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | 0 | return $self->publish_command_reply($reply); | |||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||
| 175 | |||||||||||||||||||||||
| 176 | 0 | ▶ | sub _needs_redaction { | ||||||||||||||||||||
|
Subroutine
_needs_redaction: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 177 | 0 | my ($name) = @_; | |||||||||||||||||||||
| 178 | 0 | ▶ | return 1 if grep { $name eq $_ } qw( | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 179 | authenticate | ||||||||||||||||||||||
| 180 | saslStart | ||||||||||||||||||||||
| 181 | saslContinue | ||||||||||||||||||||||
| 182 | getnonce | ||||||||||||||||||||||
| 183 | createUser | ||||||||||||||||||||||
| 184 | updateUser | ||||||||||||||||||||||
| 185 | copydbgetnonce | ||||||||||||||||||||||
| 186 | copydbsaslstart | ||||||||||||||||||||||
| 187 | copydb | ||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||
| 189 | 0 | return 0; | |||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||
| 191 | |||||||||||||||||||||||
| 192 | 0 | ▶ | sub _convert_legacy_insert { | ||||||||||||||||||||
|
Subroutine
_convert_legacy_insert: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 193 | 0 | my ( $self, $op_doc ) = @_; | |||||||||||||||||||||
| 194 | 0 | ▶ | $op_doc = [$op_doc] unless ref $op_doc eq 'ARRAY'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 195 | return [ | ||||||||||||||||||||||
| 196 | insert => $self->coll_name, | ||||||||||||||||||||||
| 197 | documents => $op_doc, | ||||||||||||||||||||||
| 198 | 0 | @{ $self->write_concern->as_args }, | |||||||||||||||||||||
| 199 | ]; | ||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | # Duplicated from MongoDB::CommandResult | ||||||||||||||||||||||
| 203 | 0 | ▶ | sub _extract_errmsg { | ||||||||||||||||||||
|
Subroutine
_extract_errmsg: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||
| 204 | 0 | my ($output) = @_; | |||||||||||||||||||||
| 205 | 0 | for my $err_key (qw/$err err errmsg/) { | |||||||||||||||||||||
| 206 | 0 | ▶ | return $output->{$err_key} if exists $output->{$err_key}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||
| 208 | 0 | ▶ | if ( exists $output->{writeConcernError} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 209 | 0 | return $output->{writeConcernError}{errmsg}; | |||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||
| 211 | 0 | return ""; | |||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||
| 213 | |||||||||||||||||||||||
| 214 | 0 | ▶ | sub _convert_legacy_update { | ||||||||||||||||||||
|
Subroutine
_convert_legacy_update: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 215 | 0 | my ( $self, $op_doc ) = @_; | |||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | return [ | ||||||||||||||||||||||
| 218 | update => $self->coll_name, | ||||||||||||||||||||||
| 219 | updates => [ | ||||||||||||||||||||||
| 220 | update => $self->coll_name, | ||||||||||||||||||||||
| 221 | updates => [$op_doc], | ||||||||||||||||||||||
| 222 | ], | ||||||||||||||||||||||
| 223 | 0 | @{ $self->write_concern->as_args }, | |||||||||||||||||||||
| 224 | ]; | ||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||
| 226 | |||||||||||||||||||||||
| 227 | 0 | ▶ | sub _convert_legacy_delete { | ||||||||||||||||||||
|
Subroutine
_convert_legacy_delete: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 228 | 0 | my ( $self, $op_doc ) = @_; | |||||||||||||||||||||
| 229 | |||||||||||||||||||||||
| 230 | return [ | ||||||||||||||||||||||
| 231 | delete => $self->coll_name, | ||||||||||||||||||||||
| 232 | deletes => [$op_doc], | ||||||||||||||||||||||
| 233 | 0 | @{ $self->write_concern->as_args }, | |||||||||||||||||||||
| 234 | ]; | ||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||
| 236 | |||||||||||||||||||||||
| 237 | 0 | ▶ | sub _decode_preencoded { | ||||||||||||||||||||
|
Subroutine
_decode_preencoded: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||
| 238 | 0 | my ($obj) = @_; | |||||||||||||||||||||
| 239 | 0 | my $codec = BSON->new; | |||||||||||||||||||||
| 240 | 0 | my $type = ref($obj); | |||||||||||||||||||||
| 241 | 0 | ▶ | if ( $type eq 'BSON::Raw' ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 242 | 0 | return $codec->decode_one( $obj->{bson} ); | |||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||
| 244 | elsif ( $type eq 'Tie::IxHash' ) { | ||||||||||||||||||||||
| 245 | 0 | tie my %out, "Tie::IxHash"; | |||||||||||||||||||||
| 246 | 0 | $out{$_} = _decode_preencoded( $obj->FETCH($_) ) for $obj->Keys; | |||||||||||||||||||||
| 247 | 0 | return \%out; | |||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||
| 249 | elsif ( $type eq 'ARRAY' ) { | ||||||||||||||||||||||
| 250 | 0 | return [ map { _decode_preencoded($_) } @$obj ]; | |||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||
| 252 | elsif ( $type eq 'HASH' ) { | ||||||||||||||||||||||
| 253 | 0 | return { map { ; $_ => _decode_preencoded( $obj->{$_} ) } keys %$obj }; | |||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||
| 255 | 0 | return $obj; | |||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||
| 257 | |||||||||||||||||||||||
| 258 | 0 | ▶ | sub _to_tied_ixhash { | ||||||||||||||||||||
|
Subroutine
_to_tied_ixhash: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||
| 259 | 0 | my ($in) = @_; | |||||||||||||||||||||
| 260 | 0 | my $type = ref($in); | |||||||||||||||||||||
| 261 | 0 | my %out; | |||||||||||||||||||||
| 262 | 0 | ▶ | if ( $type eq 'ARRAY' ) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 263 | # earlier type checks should ensure even elements | ||||||||||||||||||||||
| 264 | 0 | tie %out, "Tie::IxHash", map { _decode_preencoded($_) } @$in; | |||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||
| 266 | elsif ( $type eq "Tie::IxHash" ) { | ||||||||||||||||||||||
| 267 | 0 | tie %out, "Tie::IxHash"; | |||||||||||||||||||||
| 268 | 0 | $out{$_} = _decode_preencoded( $in->FETCH($_) ) for $in->Keys; | |||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||
| 270 | elsif ( $in->$_can('_as_tied_hash') ) { | ||||||||||||||||||||||
| 271 | 0 | %out = %{ $in->_as_tied_hash() }; | |||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||
| 273 | 0 | tie %out, "Tie::IxHash", map { ; $_ => _decode_preencoded( $in->{$_} ) } keys %$in; | |||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||
| 275 | 0 | return \%out; | |||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||
| 277 | |||||||||||||||||||||||
| 278 | 60 | 1; | |||||||||||||||||||||