AvroFormatter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. namespace MediaWiki\Logger\Monolog;
  21. use AvroIODatumWriter;
  22. use AvroIOBinaryEncoder;
  23. use AvroIOTypeException;
  24. use AvroSchema;
  25. use AvroStringIO;
  26. use AvroValidator;
  27. use Monolog\Formatter\FormatterInterface;
  28. /**
  29. * Log message formatter that uses the apache Avro format.
  30. *
  31. * @since 1.26
  32. * @author Erik Bernhardson <ebernhardson@wikimedia.org>
  33. * @copyright © 2015 Erik Bernhardson and Wikimedia Foundation.
  34. */
  35. class AvroFormatter implements FormatterInterface {
  36. /**
  37. * @var Magic byte to encode schema revision id.
  38. */
  39. const MAGIC = 0x0;
  40. /**
  41. * @var array Map from schema name to schema definition
  42. */
  43. protected $schemas;
  44. /**
  45. * @var AvroStringIO
  46. */
  47. protected $io;
  48. /**
  49. * @var AvroIOBinaryEncoder
  50. */
  51. protected $encoder;
  52. /**
  53. * @var AvroIODatumWriter
  54. */
  55. protected $writer;
  56. /**
  57. * @param array $schemas Map from Monolog channel to Avro schema.
  58. * Each schema can be either the JSON string or decoded into PHP
  59. * arrays.
  60. */
  61. public function __construct( array $schemas ) {
  62. $this->schemas = $schemas;
  63. $this->io = new AvroStringIO( '' );
  64. $this->encoder = new AvroIOBinaryEncoder( $this->io );
  65. $this->writer = new AvroIODatumWriter();
  66. }
  67. /**
  68. * Formats the record context into a binary string per the
  69. * schema configured for the records channel.
  70. *
  71. * @param array $record
  72. * @return string|null The serialized record, or null if
  73. * the record is not valid for the selected schema.
  74. */
  75. public function format( array $record ) {
  76. $this->io->truncate();
  77. $schema = $this->getSchema( $record['channel'] );
  78. $revId = $this->getSchemaRevisionId( $record['channel'] );
  79. if ( $schema === null || $revId === null ) {
  80. trigger_error( "The schema for channel '{$record['channel']}' is not available" );
  81. return null;
  82. }
  83. try {
  84. $this->writer->write_data( $schema, $record['context'], $this->encoder );
  85. } catch ( AvroIOTypeException $e ) {
  86. $errors = AvroValidator::getErrors( $schema, $record['context'] );
  87. $json = json_encode( $errors );
  88. trigger_error( "Avro failed to serialize record for {$record['channel']} : {$json}" );
  89. return null;
  90. }
  91. return chr( self::MAGIC ) . $this->encodeLong( $revId ) . $this->io->string();
  92. }
  93. /**
  94. * Format a set of records into a list of binary strings
  95. * conforming to the configured schema.
  96. *
  97. * @param array $records
  98. * @return string[]
  99. */
  100. public function formatBatch( array $records ) {
  101. $result = [];
  102. foreach ( $records as $record ) {
  103. $message = $this->format( $record );
  104. if ( $message !== null ) {
  105. $result[] = $message;
  106. }
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Get the writer for the named channel
  112. *
  113. * @param string $channel Name of the schema to fetch
  114. * @return \AvroSchema|null
  115. */
  116. protected function getSchema( $channel ) {
  117. if ( !isset( $this->schemas[$channel] ) ) {
  118. return null;
  119. }
  120. if ( !isset( $this->schemas[$channel]['revision'], $this->schemas[$channel]['schema'] ) ) {
  121. return null;
  122. }
  123. if ( !$this->schemas[$channel]['schema'] instanceof AvroSchema ) {
  124. $schema = $this->schemas[$channel]['schema'];
  125. if ( is_string( $schema ) ) {
  126. $this->schemas[$channel]['schema'] = AvroSchema::parse( $schema );
  127. } else {
  128. $this->schemas[$channel]['schema'] = AvroSchema::real_parse(
  129. $schema
  130. );
  131. }
  132. }
  133. return $this->schemas[$channel]['schema'];
  134. }
  135. /**
  136. * Get the writer for the named channel
  137. *
  138. * @param string $channel Name of the schema
  139. * @return int|null
  140. */
  141. public function getSchemaRevisionId( $channel ) {
  142. if ( isset( $this->schemas[$channel]['revision'] ) ) {
  143. return (int)$this->schemas[$channel]['revision'];
  144. }
  145. return null;
  146. }
  147. /**
  148. * convert an integer to a 64bits big endian long (Java compatible)
  149. * NOTE: certainly only compatible with PHP 64bits
  150. * @param int $id
  151. * @return string the binary representation of $id
  152. */
  153. private function encodeLong( $id ) {
  154. $high = ( $id & 0xffffffff00000000 ) >> 32;
  155. $low = $id & 0x00000000ffffffff;
  156. return pack( 'NN', $high, $low );
  157. }
  158. }