<p>I'd like to add some logging\stats to all our DB calls and was wondering if there's an easy way to do it?
I was hoping I could write my own driver and somehow "wrap" 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'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's a good direction, thanks! Although I was looking for something that can be done "behind the scenes" (i.e. just by adding a reference to my package).</p></pre>fortytw2: <pre><p>Extra easy if you're using postgres - throw in a quick </p>
<pre><code>db.Exec(`LOAD 'auto_explain';`)
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'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't valuable. And it's probably my last act of protest on reddit.</p></pre>skidooer: <pre><p>Where did the community say it wasn't valuable?</p>
<p>A downvote doesn't say anything. For all we know someone accidentally clicked on the wrong button.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传