pico
created pr with
35.1
added 35.2
1: 77aaa29 ! 1: 8d56535 reactor(metric-drain): use caddy json format
-: ------- > 2: a336041 wip
-: ------- > 3: 7ae45b3 chore: wrap
-: ------- > 4: bfa5c4f done
added 35.3
1: 8d56535 < -: ------- reactor(metric-drain): use caddy json format
-: ------- > 1: c7eeb12 reactor(metric-drain): use caddy access logs
2: a336041 < -: ------- wip
3: 7ae45b3 < -: ------- chore: wrap
4: bfa5c4f < -: ------- done
added 35.4
1: c7eeb12 ! 1: 4e0839a reactor(metric-drain): use caddy access logs
changed status to
accepted
cmds
checkout latest patchset:
ssh pr.pico.sh print 35 | git am -3checkout any patchset in a patch request:
ssh pr.pico.sh print 35.[rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 35set PR to open (enables RSS notifications):
ssh pr.pico.sh pr open 35set PR to draft (stops RSS notifications):
ssh pr.pico.sh pr draft 35
Patchset
35.2
chore: wrap
Eric Bower
2024-11-23T02:41:15ZSemantic diff summary
0 added,
4 modified,
0 signature changed,
0 removed
across 1 analyzed file
+71
-41
auth/api.go
#
| ... | ... | @@ -588,8 +588,8 @@ type AccessLogReq struct { | |
| 588 | 588 | Host string `json:"host"` | |
| 589 | 589 | Uri string `json:"uri"` | |
| 590 | 590 | Headers struct { | |
| 591 | - | UserAgent string `json:"User-Agent"` | |
| 592 | - | Referer string `json:"Referer"` | |
| 591 | + | UserAgent []string `json:"User-Agent"` | |
| 592 | + | Referer string `json:"Referer"` | |
| 593 | 593 | } `json:"headers"` | |
| 594 | 594 | Tls struct { | |
| 595 | 595 | ServerName string `json:"server_name"` |
| ... | ... | @@ -657,36 +657,72 @@ func deserializeCaddyAccessLog(dbpool db.DB, access *CaddyAccessLog) (*db.Analyt | |
| 657 | 657 | Host: host, | |
| 658 | 658 | Path: path, | |
| 659 | 659 | IpAddress: access.Request.ClientIP, | |
| 660 | - | UserAgent: access.Request.Headers.UserAgent, | |
| 660 | + | UserAgent: strings.Join(access.Request.Headers.UserAgent, " "), | |
| 661 | 661 | Referer: access.Request.Headers.Referer, // TODO: I don't see referer in the access log | |
| 662 | 662 | Status: access.Status, | |
| 663 | 663 | }, nil | |
| 664 | 664 | } | |
| 665 | 665 | ||
| 666 | - | func containerDrainSub(ctx context.Context, logger *slog.Logger) { | |
| 666 | + | // this feels really stupid because i'm taking containter-drain, | |
| 667 | + | // filtering it, and then sending it to metric-drain. The | |
| 668 | + | // metricDrainSub function listens on the metric-drain and saves it. | |
| 669 | + | // So why not just call the necessary functions to save the visit? | |
| 670 | + | // We want to be able to have pipe as a debugging tool which means we | |
| 671 | + | // can sub to `metric-drain` and have a nice clean output to look use. | |
| 672 | + | func containerDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger) { | |
| 673 | + | info := shared.NewPicoPipeClient() | |
| 667 | 674 | drain := pipe.NewReconnectReadWriteCloser( | |
| 668 | 675 | ctx, | |
| 669 | 676 | logger, | |
| 670 | - | shared.NewPicoPipeClient(), | |
| 671 | - | "container logs", | |
| 677 | + | info, | |
| 678 | + | "container drain", | |
| 672 | 679 | "sub container-drain -k", | |
| 673 | 680 | 100, | |
| 674 | 681 | -1, | |
| 675 | 682 | ) | |
| 676 | 683 | ||
| 677 | - | fmt.Println("WTFFFFFF") | |
| 678 | - | scanner := bufio.NewScanner(drain) | |
| 679 | - | for scanner.Scan() { | |
| 680 | - | line := scanner.Text() | |
| 681 | - | fmt.Println("HMMMM", line) | |
| 682 | - | if strings.Contains(line, "http.log.access") { | |
| 683 | - | clean := strings.TrimSpace(line) | |
| 684 | - | fmt.Println("LINE", clean) | |
| 685 | - | // TODO: send to metric drain | |
| 684 | + | send := pipe.NewReconnectReadWriteCloser( | |
| 685 | + | ctx, | |
| 686 | + | logger, | |
| 687 | + | info, | |
| 688 | + | "from container drain to metric drain", | |
| 689 | + | "pub metric-drain -b=false", | |
| 690 | + | 100, | |
| 691 | + | -1, | |
| 692 | + | ) | |
| 693 | + | ||
| 694 | + | for { | |
| 695 | + | scanner := bufio.NewScanner(drain) | |
| 696 | + | for scanner.Scan() { | |
| 697 | + | line := scanner.Text() | |
| 698 | + | if strings.Contains(line, "http.log.access") { | |
| 699 | + | clean := strings.TrimSpace(line) | |
| 700 | + | visit, err := accessLogToVisit(dbpool, clean) | |
| 701 | + | if err != nil { | |
| 702 | + | logger.Error("could not convert access log to a visit", "err", err) | |
| 703 | + | continue | |
| 704 | + | } | |
| 705 | + | jso, err := json.Marshal(visit) | |
| 706 | + | if err != nil { | |
| 707 | + | logger.Error("could not marshal json of a visit", "err", err) | |
| 708 | + | continue | |
| 709 | + | } | |
| 710 | + | _, _ = send.Write(jso) | |
| 711 | + | } | |
| 686 | 712 | } | |
| 687 | 713 | } | |
| 688 | 714 | } | |
| 689 | 715 | ||
| 716 | + | func accessLogToVisit(dbpool db.DB, line string) (*db.AnalyticsVisits, error) { | |
| 717 | + | accessLog := CaddyAccessLog{} | |
| 718 | + | err := json.Unmarshal([]byte(line), &accessLog) | |
| 719 | + | if err != nil { | |
| 720 | + | return nil, err | |
| 721 | + | } | |
| 722 | + | ||
| 723 | + | return deserializeCaddyAccessLog(dbpool, &accessLog) | |
| 724 | + | } | |
| 725 | + | ||
| 690 | 726 | func metricDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger, secret string) { | |
| 691 | 727 | drain := metrics.ReconnectReadMetrics( | |
| 692 | 728 | ctx, |
| ... | ... | @@ -696,35 +732,29 @@ func metricDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger, secr | |
| 696 | 732 | -1, | |
| 697 | 733 | ) | |
| 698 | 734 | ||
| 699 | - | scanner := bufio.NewScanner(drain) | |
| 700 | - | for scanner.Scan() { | |
| 701 | - | line := scanner.Text() | |
| 702 | - | accessLog := CaddyAccessLog{} | |
| 703 | - | err := json.Unmarshal([]byte(line), &accessLog) | |
| 704 | - | if err != nil { | |
| 705 | - | logger.Error("json unmarshal", "err", err) | |
| 706 | - | continue | |
| 707 | - | } | |
| 708 | - | ||
| 709 | - | visit, err := deserializeCaddyAccessLog(dbpool, &accessLog) | |
| 710 | - | if err != nil { | |
| 711 | - | logger.Error("cannot deserialize access log", "err", err) | |
| 712 | - | continue | |
| 713 | - | } | |
| 714 | - | err = shared.AnalyticsVisitFromVisit(visit, dbpool, secret) | |
| 715 | - | if err != nil { | |
| 716 | - | if !errors.Is(err, shared.ErrAnalyticsDisabled) { | |
| 717 | - | logger.Info("could not record analytics visit", "reason", err) | |
| 735 | + | for { | |
| 736 | + | scanner := bufio.NewScanner(drain) | |
| 737 | + | for scanner.Scan() { | |
| 738 | + | line := scanner.Text() | |
| 739 | + | visit, err := accessLogToVisit(dbpool, line) | |
| 740 | + | if err != nil { | |
| 741 | + | logger.Error("could not convert access log to a visit", "err", err) | |
| 742 | + | continue | |
| 743 | + | } | |
| 744 | + | err = shared.AnalyticsVisitFromVisit(visit, dbpool, secret) | |
| 745 | + | if err != nil { | |
| 746 | + | if !errors.Is(err, shared.ErrAnalyticsDisabled) { | |
| 747 | + | logger.Info("could not record analytics visit", "reason", err) | |
| 748 | + | } | |
| 718 | 749 | } | |
| 719 | - | } | |
| 720 | 750 | ||
| 721 | - | logger.Info("inserting visit", "visit", visit) | |
| 722 | - | err = dbpool.InsertVisit(visit) | |
| 723 | - | if err != nil { | |
| 724 | - | logger.Error("could not insert visit record", "err", err) | |
| 751 | + | logger.Info("inserting visit", "visit", visit) | |
| 752 | + | err = dbpool.InsertVisit(visit) | |
| 753 | + | if err != nil { | |
| 754 | + | logger.Error("could not insert visit record", "err", err) | |
| 755 | + | } | |
| 725 | 756 | } | |
| 726 | 757 | } | |
| 727 | - | fmt.Println("DROPINNGGGGGGG") | |
| 728 | 758 | } | |
| 729 | 759 | ||
| 730 | 760 | func authMux(apiConfig *shared.ApiConfig) *http.ServeMux { |
| ... | ... | @@ -796,7 +826,7 @@ func StartApiServer() { | |
| 796 | 826 | // gather metrics in the auth service | |
| 797 | 827 | go metricDrainSub(ctx, db, logger, cfg.Secret) | |
| 798 | 828 | // convert container logs to access logs | |
| 799 | - | // go containerDrainSub(ctx, logger) | |
| 829 | + | go containerDrainSub(ctx, db, logger) | |
| 800 | 830 | ||
| 801 | 831 | defer ctx.Done() | |
| 802 | 832 |