Skip to content

Commit 2f498df

Browse files
committed
fixes #11
1 parent 5ac6ba5 commit 2f498df

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

lib/mongo.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ defmodule Mongo do
9292
* `:auth` - List of additional users to authenticate as a keyword list with
9393
`:username` and `:password` keys (optional)
9494
* `:auth_source` - The database to authenticate against
95+
* `:appname` - The name of the application used the driver for the MongoDB-Handshake
9596
* `:set_name` - The name of the replica set to connect to (required if
9697
connecting to a replica set)
9798
* `:type` - a hint of the topology type. See `t:initial_type/0` for

lib/mongo/mongo_db_connection.ex

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ defmodule Mongo.MongoDBConnection do
5050
result =
5151
with {:ok, state} <- tcp_connect(opts, state),
5252
{:ok, state} <- maybe_ssl(opts, state),
53-
{:ok, state} <- wire_version(state),
53+
{:ok, state} <- hand_shake(opts, state),
5454
{:ok, state} <- maybe_auth(opts, state) do
5555
{:ok, state}
5656
end
@@ -117,21 +117,64 @@ defmodule Mongo.MongoDBConnection do
117117
end
118118
end
119119

120-
defp wire_version(state) do
120+
defp wire_version(state, client \\ nil) do
121121
# wire version
122122
# https://github.com/mongodb/mongo/blob/master/src/mongo/db/wire_version.h
123-
case Utils.command(-1, [ismaster: 1], state) do
123+
124+
cmd = [ismaster: 1, client: client] |> filter_nils()
125+
126+
case Utils.command(-1, cmd, state) do
124127
{:ok, %{"ok" => ok, "maxWireVersion" => version} = response} when ok == 1 -> {:ok, %{update_limits(state, response) | wire_version: version}}
125128
{:ok, %{"ok" => ok}} when ok == 1 -> {:ok, %{state | wire_version: 0}}
126129
{:ok, %{"ok" => ok, "errmsg" => msg, "code" => code}} when ok == 0 ->
127130
err = Mongo.Error.exception(message: msg, code: code)
128131
{:disconnect, err, state}
129132
{:disconnect, _, _} = error -> error
130133
end
134+
end
131135

136+
defp hand_shake(opts, state) do
137+
wire_version(state, driver(opts[:appname] || "My killer app"))
138+
end
139+
140+
defp driver(appname) do
141+
142+
driver_version = case :application.get_key(:mongodb_driver, :vsn) do
143+
{:ok, version} -> to_string(version)
144+
_ -> "??"
145+
end
132146

147+
[architecture, name | _rest] = String.split(to_string(:erlang.system_info(:system_architecture)), "-")
148+
149+
version = case :os.version() do
150+
{one, two, tree} -> to_string(one) <> "." <> to_string(two) <> "." <> to_string(tree)
151+
s -> s
152+
end
153+
154+
plattform = "Elixir (" <> System.version() <> "), Erlang/OTP (" <> to_string(:erlang.system_info(:otp_release)) <> "), ERTS (" <> to_string(:erlang.system_info(:version)) <> ")"
155+
156+
type = elem(:os.type(), 1)
157+
%{
158+
client: %{
159+
application: %{name: appname}
160+
},
161+
driver: %{
162+
name: "mongodb_driver",
163+
version: driver_version
164+
},
165+
os: %{
166+
type: type,
167+
name: pretty_name(name),
168+
architecture: architecture,
169+
version: version
170+
},
171+
platform: plattform
172+
}
133173
end
134174

175+
defp pretty_name("apple"), do: "Mac OS X"
176+
defp pretty_name(name), do: name
177+
135178
##
136179
#
137180
# Updates the limits from the isMaster response:
@@ -239,5 +282,8 @@ defmodule Mongo.MongoDBConnection do
239282
end)
240283
end
241284

285+
defp filter_nils(keyword) when is_list(keyword) do
286+
Enum.reject(keyword, fn {_key, value} -> is_nil(value) end)
287+
end
242288

243289
end

0 commit comments

Comments
 (0)