aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/kotlin/moe/nea/ledger/utils
diff options
context:
space:
mode:
Diffstat (limited to 'mod/src/main/kotlin/moe/nea/ledger/utils')
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/utils/ScreenUtil.kt29
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt21
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt9
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/utils/telemetry/Span.kt3
4 files changed, 59 insertions, 3 deletions
diff --git a/mod/src/main/kotlin/moe/nea/ledger/utils/ScreenUtil.kt b/mod/src/main/kotlin/moe/nea/ledger/utils/ScreenUtil.kt
new file mode 100644
index 0000000..0305126
--- /dev/null
+++ b/mod/src/main/kotlin/moe/nea/ledger/utils/ScreenUtil.kt
@@ -0,0 +1,29 @@
+package moe.nea.ledger.utils
+
+import moe.nea.ledger.mixin.AccessorContainerDispenser
+import moe.nea.ledger.mixin.AccessorContainerHopper
+import net.minecraft.client.gui.GuiScreen
+import net.minecraft.client.gui.inventory.GuiContainer
+import net.minecraft.inventory.ContainerChest
+import net.minecraft.inventory.IInventory
+
+object ScreenUtil {
+ fun estimateInventory(screen: GuiScreen?): IInventory? {
+ if (screen !is GuiContainer) {
+ return null
+ }
+ val container = screen.inventorySlots ?: return null
+ if (container is ContainerChest)
+ return container.lowerChestInventory
+ if (container is AccessorContainerDispenser)
+ return container.dispenserInventory_ledger
+ if (container is AccessorContainerHopper)
+ return container.hopperInventory_ledger
+ return null
+
+ }
+
+ fun estimateName(screen: GuiScreen?): String? {
+ return estimateInventory(screen)?.name
+ }
+} \ No newline at end of file
diff --git a/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt
new file mode 100644
index 0000000..3953e09
--- /dev/null
+++ b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt
@@ -0,0 +1,21 @@
+package moe.nea.ledger.utils.network
+
+import com.google.gson.JsonElement
+import com.google.gson.JsonObject
+import moe.nea.ledger.utils.telemetry.ContextValue
+
+class RequestTrace(val request: Request) : ContextValue {
+ override fun serialize(): JsonElement {
+ return JsonObject().apply {
+ addProperty("url", request.url.toString())
+ addProperty("method", request.method.name)
+ addProperty("content-type", request.headers["content-type"])
+ addProperty("accept", request.headers["accept"])
+ }
+ }
+
+ companion object {
+ val KEY = "http_request"
+ fun createTrace(request: Request): Pair<String, RequestTrace> = KEY to RequestTrace(request)
+ }
+} \ No newline at end of file
diff --git a/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt
index a49c65a..8101527 100644
--- a/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt
+++ b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt
@@ -2,6 +2,8 @@ package moe.nea.ledger.utils.network
import moe.nea.ledger.utils.ErrorUtil
import moe.nea.ledger.utils.di.Inject
+import moe.nea.ledger.utils.telemetry.CommonKeys
+import moe.nea.ledger.utils.telemetry.ContextValue
import java.net.URL
import java.net.URLConnection
import java.security.KeyStore
@@ -38,7 +40,10 @@ class RequestUtil @Inject constructor(val errorUtil: ErrorUtil) {
fun createRequest(url: String) = createRequest(URL(url))
fun createRequest(url: URL) = Request(url, Request.Method.GET, null, mapOf())
- fun executeRequest(request: Request): Response {
+ fun executeRequest(request: Request): Response? = errorUtil.catch(
+ CommonKeys.EVENT_MESSAGE to ContextValue.string("Failed to execute request"),
+ RequestTrace.createTrace(request)
+ ) {
val connection = request.url.openConnection()
enhanceConnection(connection)
connection.setRequestProperty("accept-encoding", "gzip")
@@ -56,7 +61,7 @@ class RequestUtil @Inject constructor(val errorUtil: ErrorUtil) {
val text = stream.bufferedReader().readText()
stream.close()
// Do NOT call connection.disconnect() to allow for connection reuse
- return Response(request, text, connection.headerFields)
+ return@catch Response(request, text, connection.headerFields)
}
diff --git a/mod/src/main/kotlin/moe/nea/ledger/utils/telemetry/Span.kt b/mod/src/main/kotlin/moe/nea/ledger/utils/telemetry/Span.kt
index 0d680a9..8b8e284 100644
--- a/mod/src/main/kotlin/moe/nea/ledger/utils/telemetry/Span.kt
+++ b/mod/src/main/kotlin/moe/nea/ledger/utils/telemetry/Span.kt
@@ -2,9 +2,10 @@ package moe.nea.ledger.utils.telemetry
class Span(val parent: Span?) : AutoCloseable {
companion object {
+ val rootSpan = Span(null)
private val _current = object : InheritableThreadLocal<Span>() {
override fun initialValue(): Span {
- return Span(null)
+ return Span(rootSpan)
}
override fun childValue(parentValue: Span?): Span {