sql DB profiling

agolangf · · 497 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;d like to add some logging\stats to all our DB calls and was wondering if there&#39;s an easy way to do it? I was hoping I could write my own driver and somehow &#34;wrap&#34; the original (mySql or Postgres) driver, redirecting calls and keeping some stats but the <code>drivers</code> hash is private in the database\sql package. Any other ideas?</p> <hr/>**评论:**<br/><br/>themissedsymphony: <pre><p><a href="https://github.com/ExpansiveWorlds/instrumentedsql" rel="nofollow">https://github.com/ExpansiveWorlds/instrumentedsql</a> is quite good for wrapping and then logging!</p></pre>eyalpost: <pre><p>Nice! I wasn&#39;t even aware that both the mysql and postgres drivers expose the Driver struct so I can reference it directly. Thanks!</p></pre>monkey_that: <pre><p>You can wrap sql.DB in another struct, then implement needed interfaces such as Query, Exec that will pass all the arguments to an actual sql.DB, in the mean time you can measure and log.</p> <p>For more advanced method look at this <a href="https://github.com/marcusolsson/goddd">https://github.com/marcusolsson/goddd</a></p></pre>eyalpost: <pre><p>That&#39;s a good direction, thanks! Although I was looking for something that can be done &#34;behind the scenes&#34; (i.e. just by adding a reference to my package).</p></pre>fortytw2: <pre><p>Extra easy if you&#39;re using postgres - throw in a quick </p> <pre><code>db.Exec(`LOAD &#39;auto_explain&#39;;`) db.Exec(`SET auto_explain.log_min_duration = 0;`) </code></pre> <p>then check the logs of your postgres instance (running in the foreground, in docker makes this super easy for development)</p></pre>AnimalMachine: <pre><p>edit: i had supplied some profiling tips, but it was getting down voted. <a href="/r/golang" rel="nofollow">/r/golang</a> is getting to be as good as the rest of reddit it seems. :(</p></pre>GentooMonk: <pre><p>sadly :(</p></pre>zuzzas: <pre><blockquote> <p>i had supplied some profiling tips, but it was getting down voted</p> </blockquote> <p>And you&#39;ve removed your potentially useful advice, because a couple of imaginary internet points were taken from you?</p></pre>AnimalMachine: <pre><p>Community said it wasn&#39;t valuable. And it&#39;s probably my last act of protest on reddit.</p></pre>skidooer: <pre><p>Where did the community say it wasn&#39;t valuable?</p> <p>A downvote doesn&#39;t say anything. For all we know someone accidentally clicked on the wrong button.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

497 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传