Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REPL mode telemetry enabling/disabling #1895

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 65 additions & 1 deletion src/API.jl
Expand Up @@ -8,11 +8,12 @@ import Random
using Dates
import LibGit2

import ..depots, ..depots1, ..logdir, ..devdir
import ..depots, ..depots1, ..logdir, ..devdir, ..pkg_server
import ..Operations, ..GitTools, ..Pkg, ..UPDATED_REGISTRY_THIS_SESSION
using ..Types, ..TOML
using ..Types: VersionTypes
using ..BinaryPlatforms
using ..PlatformEngines: modify_telemetry_file
using ..Artifacts: artifact_paths

include("generate.jl")
Expand Down Expand Up @@ -962,6 +963,69 @@ function activate(f::Function, new_project::AbstractString)
end
end

function set_telemetry(server, val::Bool)
modify_telemetry_file(server) do info
info["telemetry"] = val
end
abled = Dict(false => "disabled", true => "enabled")
if server === nothing
println("Telemetry ", abled[val], " for all future Pkg servers")
else
println("Telemetry ", abled[val], " for ", server)
end
end

function set_telemetry(val::Bool, all::Bool = false)
if all
# iterate over all servers:
for depot in depots()
if !isdir(joinpath(depot, "servers"))
continue
end
for server in readdir(joinpath(depot, "servers"))
if !isfile(joinpath(depot, "servers", server, "telemetry.toml"))
continue
end

set_telemetry(string("https://", server), val)
end
end

# Set default for future servers
set_telemetry(nothing, val)
else
# Set for current server
set_telemetry(pkg_server(), val)
end
end

# Entrypoints for REPL mode
telemetry_enable(;all::Bool = false) = set_telemetry(true, all)
telemetry_disable(;all::Bool = false) = set_telemetry(false, all)
function telemetry_status()
abled = Dict(true => "enabled", false => "disabled")
modify_telemetry_file(nothing) do info
println("Default: ", abled[get(info, "telemetry", true)])
end

# Next, iterate over all servers:
for depot in depots()
if !isdir(joinpath(depot, "servers"))
continue
end

for server in readdir(joinpath(depot, "servers"))
if !isfile(joinpath(depot, "servers", server, "telemetry.toml"))
continue
end

modify_telemetry_file("https://$(server)") do info
println(server, ": ", abled[get(info, "telemetry", true)])
end
end
end
end

########
# Undo #
########
Expand Down
58 changes: 57 additions & 1 deletion src/PlatformEngines.jl
Expand Up @@ -852,9 +852,65 @@ const telemetry_file_lock = ReentrantLock()
const telemetry_notice_printed = Ref(false)

telemetry_notice(server::AbstractString=pkg_server()) = """
LEGAL NOTICE: package operations send anonymous data about your system to $server (your current package server), including the operating system and Julia versions you are using, and a random client UUID. Running `Pkg.telemetryinfo()` will show exactly what data is sent. See https://julialang.org/legal/data/ for more details about what this data is used for, how long it is retained, and how to opt out of sending it.
LEGAL NOTICE: package operations send anonymous data about your system to $server (your current package server), including the operating system and Julia versions you are using, and a random client UUID. Running `Pkg.telemetryinfo()` will show exactly what data is sent. See https://julialang.org/legal/data/ for more details about what this data is used for, how long it is retained, and details on how to customize its behavior. To enable/disable this for your current Julia installation, use the `telemetry enable/disable` command in the Pkg REPL.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thank you!

"""

"""
modify_telemetry_file(f::Function, server=pkg_server())

Do-block method for modifying a telemetry TOML file; if `server` is set to `nothing`,
opens the defaults telemetry TOML file which is used to set set Pkg server values.
"""
function modify_telemetry_file(f::Function, server=pkg_server())
filepath = nothing
if server === nothing
# Search for an existent defaults file
for depot in depots()
defaults_file = joinpath(depot, "servers", "telemetry.toml")
if isfile(defaults_file)
filepath = defaults_file
end
end

# If none exists, we will create one in depots1
if filepath === nothing
filepath = joinpath(depots1(), "servers", "telemetry.toml")
end
else
server_dir = get_server_dir(server, server)
filepath = joinpath(server_dir, "telemetry.toml")
end

info = Dict()
if isfile(filepath)
try info = TOML.parsefile(filepath)
catch err
@warn "replacing malformed telemetry file" filepath err
end
end

# Call user function with `info`
try
f(info)

# If everything goes well, then write out the file again
if isempty(info)
rm(filepath; force=true)
else
mkpath(dirname(filepath))
tmp = tempname()
let info = info
open(tmp, write=true) do io
TOML.print(io, info, sorted=true)
end
end
mv(tmp, filepath, force=true)
end
catch e
rethrow(e)
end
end

function get_telemetry_headers(url::AbstractString, notify::Bool=true)
headers = String[]
server = pkg_server()
Expand Down
63 changes: 62 additions & 1 deletion src/REPLMode/command_declarations.jl
Expand Up @@ -447,6 +447,67 @@ Display information about installed registries.
pkg> registry status
```
""",
]
],
], #registry
"telemetry" => CommandDeclaration[
[ :name => "enable",
:api => API.telemetry_enable,
:description => "Enable Pkg telemetry headers",
:option_spec => OptionDeclaration[
[:name => "all", :api => :all => true],
],
:help => md"""
telemetry enable [--all]

Enable Pkg telemetry headers for the current Julia Pkg server.
To enable for all current and future servers, use `--all`.

!!! compat "Julia 1.6"
Pkg's telemetry header handling requires at least Julia 1.1.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Pkg's telemetry header handling requires at least Julia 1.1.
Pkg's telemetry header handling requires at least Julia 1.6.


**Examples**
```
pkg> telemetry enable
```
""",
], [
:name => "disable",
:api => API.telemetry_disable,
:description => "Disable Pkg telemetry headers",
:option_spec => OptionDeclaration[
[:name => "all", :api => :all => true],
],
:help => md"""
telemetry disable [--all]

Disable Pkg telemetry headers for the current Julia Pkg server.
To enable for all current and future servers, use `--all`.

!!! compat "Julia 1.6"
Pkg's telemetry header handling requires at least Julia 1.1.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Pkg's telemetry header handling requires at least Julia 1.1.
Pkg's telemetry header handling requires at least Julia 1.6.


**Examples**
```
pkg> telemetry disable
```
""",
], [
:name => "status",
:api => API.telemetry_status,
:description => "Display Pkg telemetry header status",
:help => md"""
telemetry status

Display Pkg telemetry header status for all known servers.

!!! compat "Julia 1.6"
Pkg's telemetry header handling requires at least Julia 1.1.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Pkg's telemetry header handling requires at least Julia 1.1.
Pkg's telemetry header handling requires at least Julia 1.6.


**Examples**
```
pkg> telemetry status
```
""",
],
], #telemetry
] #command_declarations