Skip to content

Commit

Permalink
Additional logging (#15)
Browse files Browse the repository at this point in the history
* [TH2-4730] Additional TRACE logging for future debug.
  • Loading branch information
isengrims authored Apr 7, 2023
1 parent f87ce6a commit 0e533c0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# AVRO codec (2.2.0)
# AVRO codec (2.2.1)
## Description
Designed for decode AVRO raw messages to parsed messages and encode back.
It is based on [th2-codec](https://github.com/th2-net/th2-codec).
Expand Down Expand Up @@ -80,6 +80,9 @@ Only one of settings `sessionAliasToDictionaryAlias` or `avroMessageIdToDictiona

## Release notes

### 2.2.1
* Added detailed TRACE logging for parsing into class com.exactpro.th2.codec.MessageDatumReader

### 2.2.0
+ Added mode resolution schema by session alias
+ Added support wildcard for session alias setting
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release_version=2.2.0
release_version=2.2.1
20 changes: 18 additions & 2 deletions src/main/kotlin/com/exactpro/th2/codec/MessageDatumReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.exactpro.th2.common.grpc.Message
import com.exactpro.th2.common.grpc.Value
import com.exactpro.th2.common.message.addField
import com.exactpro.th2.common.value.toValue
import com.google.protobuf.TextFormat.shortDebugString
import org.apache.avro.*
import org.apache.avro.data.TimeConversions.*
import org.apache.avro.generic.*
Expand All @@ -30,6 +31,7 @@ import java.io.IOException
import java.nio.ByteBuffer
import java.util.*
import javax.xml.bind.DatatypeConverter
import mu.KotlinLogging

class MessageDatumReader(schema: Schema, private val enableIdPrefixEnumFields: Boolean = false) :
GenericDatumReader<Message.Builder>(schema, schema, getData()) {
Expand Down Expand Up @@ -57,7 +59,6 @@ class MessageDatumReader(schema: Schema, private val enableIdPrefixEnumFields: B
override fun readField(r: Any, f: Schema.Field, oldDatum: Any?, decoder: ResolvingDecoder, state: Any?) {
var readValue = read(oldDatum, f.schema(), decoder)
var fieldName = f.name()

if (readValue is UnionData) {
val description = readValue.description
readValue = readValue.value
Expand All @@ -66,7 +67,9 @@ class MessageDatumReader(schema: Schema, private val enableIdPrefixEnumFields: B
}
}
if (readValue != null) {
(r as Message.Builder).addField(fieldName, readValue.convertToValue())
val convertedValue = readValue.convertToValue()
LOGGER.trace { "Read value ${f.name()}: ${shortDebugString(convertedValue)} (origin: $readValue)" }
(r as Message.Builder).addField(fieldName, convertedValue)
}
}

Expand Down Expand Up @@ -98,6 +101,18 @@ class MessageDatumReader(schema: Schema, private val enableIdPrefixEnumFields: B
return Message.newBuilder()
}

override fun convert(datum: Any?, schema: Schema?, type: LogicalType?, conversion: Conversion<*>?): Any {
val convertedValue = super.convert(datum, schema, type, conversion)
if(LOGGER.isTraceEnabled) {
val rawValueString = when(datum) {
is ByteBuffer -> datum.asHexString()
else -> datum.toString()
}
LOGGER.trace { "Converting value using logical type ${type?.name} from $rawValueString to $convertedValue" }
}
return convertedValue
}

private fun ByteBuffer.asHexString(): String {
val bytes = ByteArray(this.remaining())
this.get(bytes)
Expand All @@ -116,6 +131,7 @@ class MessageDatumReader(schema: Schema, private val enableIdPrefixEnumFields: B
val description: String
)
companion object {
private val LOGGER = KotlinLogging.logger { }
fun getData(): GenericData? {
return GenericData.get().apply {
addLogicalTypeConversion(Conversions.DecimalConversion())
Expand Down

0 comments on commit 0e533c0

Please sign in to comment.