Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ For details about compatibility between different releases, see the **Commitment
### Fixed

- Fixed searching users in User management in the Console.
- CLI no longer causes a panic when running from a docker container in a Linux machine.

### Security

Expand Down
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ RUN rm -rf /srv/ttn-lorawan/lorawan-webhook-templates/.git

FROM alpine:3.21

RUN addgroup -g 886 thethings && adduser -u 886 -S -G thethings thethings
RUN addgroup -g 886 thethings && \
adduser -u 886 -S -G thethings thethings && \
mkdir -p /home/thethings && \
chown thethings:thethings /home/thethings

RUN apk --update --no-cache add ca-certificates curl

Expand All @@ -41,4 +44,7 @@ ENV TTN_LW_HEALTHCHECK_URL=http://localhost:1885/healthz

HEALTHCHECK --interval=1m --timeout=5s CMD curl -f $TTN_LW_HEALTHCHECK_URL || exit 1

RUN mkdir -p /home/thethings/.cache && chown -R thethings:thethings /home/thethings/.cache
ENV XDG_CACHE_HOME=/home/thethings/.cache

USER thethings:thethings
43 changes: 25 additions & 18 deletions pkg/telemetry/exporter/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,50 +79,50 @@ func cliStatePath() (string, error) {
}

// getCLIState returns the telemetry state, if it doesn't exist it creates a file with default values for the state.
func getCLIState() (*cliStateTelemetry, bool, error) {
func getCLIState() (*cliStateTelemetry, error) {
folderPath, err := ttnPath()
if err != nil {
return nil, false, err
return nil, err
}
if _, err := os.Stat(folderPath); err != nil {
if !os.IsNotExist(err) {
return nil, false, err
return nil, err
}
// Creates the folder since it does not exist.
if err := os.Mkdir(folderPath, 0o755); err != nil {
return nil, false, err
return nil, err
}
}

statePath, err := cliStatePath()
if err != nil {
return nil, false, err
return nil, err
}

cliState := &cliStateTelemetry{}

if _, err := os.Stat(statePath); err != nil {
if !os.IsNotExist(err) {
return nil, false, err
return nil, err
}

cliState.UID = uuid.NewString()
cliState.LastSent = time.Now()
return cliState, true, nil
return cliState, nil
}

b, err := os.ReadFile(statePath)
if err != nil {
return nil, false, err
return nil, err
}
if err := yaml.Unmarshal(b, cliState); err != nil {
return nil, false, err
return nil, err
}
return cliState, false, nil
return cliState, nil
}

func shouldSendTelemetry(t time.Time) bool {
return time.Now().After(t.Add(defaultTimeout))
func shouldSendTelemetry(st *cliStateTelemetry) bool {
return st != nil && time.Now().After(st.LastSent.Add(defaultTimeout))
}

type cliTask struct {
Expand Down Expand Up @@ -163,18 +163,23 @@ func NewCLITelemetry(opts ...Option) Task {
// Run a small task that send telemetry data once a day.
func (ct *cliTask) Run(ctx context.Context) {
logger := log.FromContext(ctx)
state, send, err := getCLIState()
state, err := getCLIState()
if err != nil {
logger.WithError(err).Debug("Failed to retrieve telemetry state")
return // Skip the telemetry procedure if an error is found creating or fetching the previous state.
}

if send || shouldSendTelemetry(state.LastSent) {
if shouldSendTelemetry(state) {
state.LastSent = time.Now()
if statePath, err := cliStatePath(); err != nil {

statePath, err := cliStatePath()
if err != nil {
logger.WithError(err).Debug("Failed to retrieve telemetry state file path")
} else {
// If no error fetching the state file path, update the last sent timestamp.
state.Write(statePath) //nolint:errcheck
return
}
if err := state.Write(statePath); err != nil {
logger.WithError(err).Debug("Failed to write new telemetry state")
return
}

b, err := json.Marshal(&models.TelemetryMessage{
Expand All @@ -186,13 +191,15 @@ func (ct *cliTask) Run(ctx context.Context) {
logger.WithError(err).Debug("Failed to marshal telemetry information")
return
}

resp, err := http.DefaultClient.Post(ct.target, "application/json", bytes.NewReader(b))
if err != nil {
logger.WithError(err).Debug("Failed to send telemetry information")
return
}
defer resp.Body.Close()
io.Copy(io.Discard, resp.Body) // nolint:errcheck

logger.Info("Sent telemetry information")
}
}
Loading