MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / SendData

Method SendData

pkg/telemetry/exporter/cli/task.go:75–130  ·  view source on GitHub ↗

SendData a small task that sends telemetry data.

(ctx context.Context)

Source from the content-addressed store, hash-verified

73
74// SendData a small task that sends telemetry data.
75func (ct *cliTask) SendData(ctx context.Context) {
76 logger := log.FromContext(ctx)
77
78 st, err := store.New()
79 if err != nil {
80 logger.WithError(err).Debug("Failed to open telemetry database")
81 return
82 }
83
84 state, err := st.GetState()
85 if err != nil {
86 logger.WithError(err).Debug("Failed to retrieve telemetry state")
87 return // Skip the telemetry procedure if an error is found creating or fetching the previous state.
88 }
89
90 if !shouldSendTelemetry(state) {
91 return
92 }
93
94 cmdData, err := st.GetCmdData()
95 if err != nil {
96 logger.WithError(err).Debug("Failed to fetch commands usage data")
97 return
98 }
99
100 data := &models.TelemetryMessage{
101 UID: state.UID,
102 CLI: &models.CLITelemetry{CmdData: *cmdData},
103 OS: telemetry.OSTelemetryData(),
104 }
105
106 b, err := json.Marshal(data)
107 if err != nil {
108 logger.WithError(err).Debug("Failed to marshal telemetry information")
109 return
110 }
111
112 resp, err := http.DefaultClient.Post(ct.target, "application/json", bytes.NewReader(b))
113 if err != nil {
114 logger.WithError(err).Debug("Failed to send telemetry information")
115 return
116 }
117 defer resp.Body.Close()
118 io.Copy(io.Discard, resp.Body) // nolint:errcheck,gosec
119
120 // Operations after sending the telemetry data
121 if err := st.UpdateLastSent(); err != nil {
122 logger.WithError(err).Debug("Failed to update last_sent field in telemetry state")
123 return
124 }
125
126 if err := st.ResetCmdData(); err != nil {
127 logger.WithError(err).Debug("Failed to reset command usage data")
128 return
129 }
130}
131
132// SaveData process and stores data related to the CLI usage.

Callers

nothing calls this directly

Calls 12

FromContextFunction · 0.92
NewFunction · 0.92
shouldSendTelemetryFunction · 0.85
DebugMethod · 0.65
WithErrorMethod · 0.65
GetStateMethod · 0.65
GetCmdDataMethod · 0.65
MarshalMethod · 0.65
CloseMethod · 0.65
UpdateLastSentMethod · 0.65
ResetCmdDataMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected