Golangでリクエストごとのクエリログが見たい!! kamakura.go #4 / go-query-and-request-logger

Golangでリクエストごとのクエリログが見たい!! kamakura.go #4 / go-query-and-request-logger

8d6c54ebb889a5fe3f77b763d563fd9f?s=128

serinuntius

May 25, 2018
Tweet

Transcript

  1. GolangͰϦΫΤετ͝ͱͷ ΫΤϦϩά͕Έ͍ͨʂʂ 2018-05-25 kamakura.go #4 LT ໘ന๏ਓΧϠοΫ ۔઒Ѯ @_serinuntius

  2. ஫ҙ
 ⚠ܕ੒෼গͳΊͰ͢⚠

  3. Ξϯέʔτ

  4. ISUCON ஌ͬͯΒͬ͠ΌΔํʙ

  5. ISUCON ग़ͨ͜ͱ͋ΔΑͬͯํʙ

  6. ISUCONͱ͸ʁ

  7. ͓୊ͱͳΔWebαʔϏεΛ
 ܾΊΒΕͨϨΪϡϨʔγϣϯͷதͰ
 ݶք·Ͱߴ଎ԽΛਤΔ
 νϡʔχϯάότϧɺ
 ͦΕ͕ISUCONͰ͢ɻ ISUCONެࣜBlogͷdescriptionΑΓ

  8. ͪͳΈʹ๻͸ ࣾ಺ISUCON͔͠ ग़ͨ͜ͱ͋Γ·ͤΜ

  9. ࣾ಺ISUCONͰݟ͑ͨ՝୊ • ΫΤϦͷൃߦճ਺Λߟྀ͠ͳ͚Ε͹͍͚ͳ͍
 (ൃߦճ਺͕গͳ͍ɺ஗͍͚ͩͷΫΤϦ΋͋Δ) w w • Ͳ͜ͰΫΤϦ͕ൃߦ͞Ε͍ͯΔ͔΋஌Γ͍ͨ • ΋ͬͱݴ͑͹ͲͷϦΫΤετͰ


    ͲΕ͚ͩݺ͹ΕͯΔ͔΋஌Γ͍ͨ • GoͰग़͍ͨ(ܕ͕͋ͬͨํ͕ΞϓϦͷϦϑΝΫλ͕͠қ͍)
  10. ࣾ಺ISUCONͰͷ ൓লΛ׆͔ͨ͢Ίʹ

  11. ΞΫηεϩΨʔ
 ͱ ΫΤϦϩΨʔΛ࡞ͬͨ

  12. github.com/serinuntius/graqt
 ര஀

  13. ࢖͍ํ εςοϓ1~import~ • 2ͭͷύοέʔδΛimport͢Δ

  14. ࢖͍ํ εςοϓ2
 ~driverΛ؀ڥม਺Ͱ੾Γସ͑Δ~ • driver੾Γସ͑ͱtraceΛΦϯΦϑ͢Δ

  15. ࢖͍ํ εςοϓ3
 ~middleware௥Ճ~ • aliceΛ࢖͏

  16. ࢖͍ํ εςοϓ4
 ~SQLݺͼग़͠ݩͷcontextରԠ~ • http.Request͔ΒcontextऔΓग़ͯ͠ɺ
 ݺͼग़͢ͱ͖ʹ౉͢

  17. ٕज़తͳ͜ͱ

  18. ϦΫΤετʹUUIDΛຒΊࠐΉ • middleware.go UUID=>ϦΫΤετͱΫΤϦΛඥ෇͚ΔͨΊ

  19. ϦΫΤετʹUUIDΛຒΊࠐΉ • middleware.go UUID=>ϦΫΤετͱΫΤϦΛඥ෇͚ΔͨΊ ίϯςΩετʹೖΕΔཧ༝
 =>ޙͰΫΤϦͱUUIDΛඥ෇͚ΔͨΊ

  20. ϋΠύϑΥʔϚϯεͳ ߏ଄ԽϩΨʔ • uber-go/zap • RLogger͕request༻ͷzapͷLogger ࠓճͷਃ͠༁ఔ౓ͷܕཁૉ

  21. ϋΠύϑΥʔϚϯεͳ ߏ଄ԽϩΨʔ {"level":"info", "ts":1525082389.1337767, "caller":"graqt/middleware.go:25", "msg":"",
 "time":0.011566198, "request_id":"5914e629-6746-42c5- z342-702f224f48e1", "path":"/user",

    "method":"POST" } • ܕΛࢦఆͯ͠Δ͔Β଎͍
  22. ΫΤϦΛτϨʔε͢Δ • shogo82148/go-sql-proxy • database/sql͸ͲΜͳDBʹ΋ରԠͰ͖ΔΑ͏ʹͳͬͯ ͍Δ • database/sql/driverʹ͋ΔinterfaceΛຬͨͯ͠Ε͹OK • ϓϩΩγ࡞ͬͯͦͷ··౉ͤ͹ϩάΛు͚ΔͷͰ͸ʁ

    • ͱ͍͏ൃ૝Ͱ࡞ΒΕͨΒ͍͠Ͱ͢*1 *1GoݴޠͰSQLͷτϨʔεΛ͢Δ
 https://shogo82148.github.io/blog/2015/05/13/golang-sql-proxy/
  23. ௒φΠεͳinterfaceͩʂʂ
 (਒͑੠) ࠓճͷืूཁ߲ˠ

  24. ΫΤϦΛτϨʔε͢Δ • go-sql-proxyʹ͸ඪ४ͰΫΤϦϩΨʔ͕͋Δ • ࠓճ͸UUIDΛϩάʹు͖͍ͨؔ܎Ͱ
 ࣗલͰఆٛ

  25. ΫΤϦτϨʔαʔͷ࣮૷ • tracer.go

  26. ΫΤϦτϨʔαʔͷ࣮૷ • preܥͰݱࡏ࣌ࠁΛฦ͢ • cͷcontextʹrequestͷuuid͕ೖ͍ͬͯΔ • ctxʹpreܥͷݱࡏ࣌ࠁ͕ೖ͍ͬͯΔ

  27. ࣮ࡍʹు͔ΕΔϩά • request.log
 {"level":"info","ts":1525082389.1337767,"caller":"graqt/middleware.go: 25","msg":"","time":0.011566198,"request_id":"5914e629-6746-42c5- b342-702f224f48e1","path":"/user","method":"POST"} • query.log
 {"level":"info","ts":1525082389.1336403,"caller":"graqt/tracer.go: 20","msg":"Exec","query":"INSERT

    INTO `user` (email,age) VALUES (?, ?)","args":[{"Name":"","Ordinal": 1,"Value":"hoge1525082389130688404@hoge.com"}, {"Name":"","Ordinal":2,"Value":57}],"time": 0.001157627,"request_id":"5914e629-6746-42c5-b342-702f224f48e1"}
  28. ͑ɺϩάు͚ͩ͘ʁ • ͪΐͬͱ଴ͬͯͯ • alpΈ͍ͨͳϩάΛूܭ͢ΔCLIΛ੡࡞த • ϦΫΤετ͝ͱʹΫΤϦ΋ݟΕΔΑ͏ʹ͠·͢ • tigΛΊͬͪΌγϯϓϧʹͨ͠ૢ࡞ײͷΠϝʔδ

  29. ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί)

  30. ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒

  31. ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒ ISUCON༧બ௨աͯ͠
 ϒϩάͰ঺հ͢Δ

  32. ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒ ISUCON༧બ௨աͯ͠
 ϒϩάͰ঺հ͢Δ (୭͔͕graqt࢖ͬͯ) 
 ISUCON༏উ͢Δ

  33. ϩʔυϚοϓ ϩά͕ग़ΔΑ͏ʹͳͬͨ Kamakura.goͰLT (ΠϚίί) viewerͷ࡞੒ ISUCON༧બ௨աͯ͠
 ϒϩάͰ঺հ͢Δ (୭͔͕graqt࢖ͬͯ) 
 ISUCON༏উ͢Δ

    100ສͰ๻ʹ
 ম೑ᇋͬͯ͘ΕΔ
  34. ·ͱΊ • serinuntius/graqtΛ࢖͑͹ɺ
 ISUCONʹศརͦ͏ͳϩά͕ు͚Δ • ISUCONҎ֎ʹ΋΋ͪΖΜ࢖͑Δ • OSSͳͷͰποίϛͱ͔ؾܰʹͲ͏ͧ • ISUCON༏উͯ͠ম೑ᇋͬͯ͘Ε