@Override
  public byte[] encode(ILoggingEvent event) {
    var baos = new ByteArrayOutputStream();
    try (var generator = jsonFactory.createGenerator(baos)) {
      generator.writeStartObject();

      // https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields
      // https://github.com/googleapis/java-logging-logback/blob/main/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java

      writeTimestamp(generator, event);
      writeSeverity(generator, event);
      writeLogger(generator, event);
      writeMessage(generator, event);
      writeThread(generator, event);

      writeServiceContext(generator);
      writeTraceContext(generator);

      var mdc = event.getMDCPropertyMap();
      writeMdc(generator, mdc);

      writeKeyValue(generator, event);

      if ("ERROR".equals(event.getLevel().toString())) {
        writeError(generator, event, mdc);
      }
      writeStackTrace(generator, event);

      generator.writeEndObject();
      generator.writeRaw('\n');
      generator.flush();
    } catch (NullPointerException | IOException e) {
      return logFallbackError(event, e);
    }
    return baos.toByteArray();
  }
@Test
  void encode_otel() {

    var tracer = otelTesting.getOpenTelemetry().getTracer("test");

    var span = tracer.spanBuilder("log").startSpan();
    byte[] msg = null;
    try (var scope = span.makeCurrent()) {
      var e = mockEvent();
      when(e.getLevel()).thenReturn(Level.DEBUG);
      when(e.getFormattedMessage()).thenReturn("oha, sup?");

      msg = encoder.encode(e);
    }

    var traceId = span.getSpanContext().getTraceId();
    var spanId = span.getSpanContext().getSpanId();

    assertMatchesJson(
        """
                      {"logger":"com.example.MyLogger","logging.googleapis.com/spanId":"%s","logging.googleapis.com/trace":"%s","logging.googleapis.com/trace_sampled":true,"message":"oha, sup?","severity":"DEBUG","thread_name":"main","time":"2024-08-09T14:13:33Z"}
                      """
            .formatted(spanId, traceId),
        msg);
  }