{"record":{"id":"bc066962263682b4","repo":"crowdsecurity/crowdsec","slug":"error-connecting-to-websocket","errorCode":null,"errorMessage":"error connecting to websocket","messagePattern":"error connecting to websocket","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go","lineNumber":283,"sourceCode":"\t\tdialer.Proxy = func(req *http.Request) (*url.URL, error) {\n\t\t\treq.SetBasicAuth(lc.config.Username, lc.config.Password)\n\t\t\treturn nil, nil\n\t\t}\n\t}\n\n\trequestHeader := http.Header{}\n\tfor k, v := range lc.requestHeaders {\n\t\trequestHeader.Add(k, v)\n\t}\n\tlc.Logger.Infof(\"Connecting to %s\", u)\n\n\tconn, resp, err := dialer.Dial(u, requestHeader)\n\tif resp != nil && resp.Body != nil {\n\t\t_ = resp.Body.Close()\n\t}\n\tif err != nil {\n\t\tlc.Logger.Errorf(\"Error connecting to websocket, err: %s\", err)\n\t\treturn responseChan, errors.New(\"error connecting to websocket\")\n\t}\n\n\tlc.t.Go(func() error {\n\t\tdefer conn.Close()\n\t\tfor {\n\t\t\tjsonResponse := &LokiResponse{}\n\n\t\t\terr = conn.ReadJSON(jsonResponse)\n\t\t\tif err != nil {\n\t\t\t\tlc.Logger.Errorf(\"Error reading from websocket: %s\", err)\n\t\t\t\treturn fmt.Errorf(\"websocket error: %w\", err)\n\t\t\t}\n\n\t\t\tresponseChan <- jsonResponse\n\t\t}\n\t})\n\n\treturn responseChan, nil","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go#L265-L301","documentation":"The Loki client failed to establish the WebSocket connection used by the /loki/api/v1/tail endpoint. The underlying dial error is logged, and the tailer returns this generic error to the caller via the response channel of Tail().","triggerScenarios":"dialer.Dial fails in Tail(): DNS failure, connection refused (Loki down or wrong port), TLS handshake failure with sslDisposition mismatch, proxy issues, or a non-101 HTTP response from the server.","commonSituations":"Loki not running, wrong loki_url/DSN host or port, HTTPS configured against a plain-HTTP Loki, auth required (401/403), or network/firewall blocking the upgrade request.","solutions":["Check Loki is reachable: curl http://<host>:3100/ready, fix loki_url host/port","Verify sslDisposition/tls matches the server scheme (http vs https)","Inspect the preceding log line 'Error connecting to websocket' for the root cause","Confirm auth credentials/tenant headers if Loki requires them","Check firewalls/proxies allow the WebSocket upgrade"],"exampleFix":"// before\nloki_url: http://localhost:3199\n// after\nloki_url: http://localhost:3100","handlingStrategy":"retry","validationCode":"resp, err := http.Get(lokiURL + \"/ready\")\nif err != nil || resp.StatusCode != 200 {\n    return errors.New(\"loki not reachable\")\n}","typeGuard":null,"tryCatchPattern":"for {\n    ch, err := client.Tail(ctx)\n    if err != nil {\n        log.Printf(\"loki tail failed: %v; retrying in 10s\", err)\n        time.Sleep(10 * time.Second)\n        continue\n    }\n    break\n}","preventionTips":["Monitor Loki /ready endpoint","Match TLS settings to the server scheme","Check credentials and tenant headers","Log the underlying dial error for diagnosis"],"tags":["network","websocket","loki","connection"],"backgroundTag":"connection-refused","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}