Merge branch 'import/ss/graphite' into ss/graphite
[collectd.git] / src / write_mongodb.c
index 23cca68..4deb24d 100644 (file)
@@ -1,6 +1,8 @@
 /**
  * collectd - src/write_mongodb.c
- * Copyright (C) 2010  Florian Forster, Akkarit Sangpetch
+ * Copyright (C) 2010  Florian Forster
+ * Copyright (C) 2010  Akkarit Sangpetch
+ * Copyright (C) 2012  Chris Lundquist
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -22,7 +24,8 @@
  *
  * Authors:
  *   Florian Forster <ff at octo.it>
- *   Akkarit Sangpetch <asangpet@andrew.cmu.edu>
+ *   Akkarit Sangpetch <asangpet at andrew.cmu.edu>
+ *   Chris Lundquist <clundquist at bluebox.net>
  **/
 
 #include "collectd.h"
@@ -89,6 +92,7 @@ static int wm_write (const data_set_t *ds, /* {{{ */
     else
       assert (23 == 42);
   }
+  /* We must finish the record, other wise the insert will fail */
   bson_finish(&record);
 
   pthread_mutex_lock (&node->lock);
@@ -119,18 +123,15 @@ static int wm_write (const data_set_t *ds, /* {{{ */
   if(status != MONGO_OK)
   {
     ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
-    if(node->conn->err == MONGO_BSON_INVALID)
-    {
-      ERROR (node->conn->errstr);
-    } else if ( record.err)
-    {
-      ERROR (record.errstr);
-    }
-
+    if (node->conn->err == MONGO_BSON_INVALID)
+      ERROR ("write_mongodb plugin: %s", node->conn->errstr);
+    else if (record.err)
+      ERROR ("write_mongodb plugin: %s", record.errstr);
   }
 
-
   pthread_mutex_unlock (&node->lock);
+  /* free our resource as not to leak memory */
+  bson_destroy(&record);
 
   return (0);
 } /* }}} int wm_write */