Skip to content

chore(livestream): Return Country Code from Livestream geo stream#45765

Merged
jordanm-posthog merged 4 commits intomasterfrom
jordanm-posthog/addCountryCodeToGeo
Jan 27, 2026
Merged

chore(livestream): Return Country Code from Livestream geo stream#45765
jordanm-posthog merged 4 commits intomasterfrom
jordanm-posthog/addCountryCodeToGeo

Conversation

@jordanm-posthog
Copy link
Copy Markdown
Contributor

Problem

The Livestream geo feed only returns lat / lng but country code would be useful for the Web Analytics live dashboard.

Changes

Adds the IP's country code to ResponseGeoEvent

How did you test this code?

Updated tests, tested locally

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Publish to changelog?

no

@jordanm-posthog jordanm-posthog requested review from a team and orian January 23, 2026 16:22
Copy link
Copy Markdown
Member

@lricoy lricoy left a comment

Choose a reason for hiding this comment

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

LGTM, but low context. Wait for Pawel if you're unsure about it

}

subscription := events.Subscription{
SubID: atomic.AddUint64(&subID, 1),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the linting change expected?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, but it won't hurt :) I was playing around with running gofmt as a pre-commit hook, so this change will make it's way in eventually.

@jordanm-posthog
Copy link
Copy Markdown
Contributor Author

LGTM, but low context. Wait for Pawel if you're unsure about it

I'll give Pawel some time to review. I'm not too worried though, geo hasn't seen any traffic in the last week so it's pretty low stakes

Copy link
Copy Markdown
Contributor

@orian orian left a comment

Choose a reason for hiding this comment

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

The geo wasn't used for the last 1.5y, I'm not sure how fast it is, I was thinking about remove it but didn't have enough time. When I've joined it also had some bugs (e.g. it was global, without proper filtering per team/token).

Comment thread livestream/geo/geoip.go Outdated
Comment thread livestream/geo/geoip.go Outdated
Comment thread livestream/events/kafka.go Outdated
phEvent.Lat, phEvent.Lng, err = geolocator.Lookup(ipStr)
phEvent.Lat, phEvent.Lng, phEvent.CountryCode, err = geolocator.Lookup(ipStr)
if err != nil && err.Error() != "invalid IP address" { // An invalid IP address is not an error on our side
// TODO capture error to PostHog
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is terrible idea, some metric may be useful

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I imagine this is terrible because it's slow? If that's the case, maybe a custom unmarshaler would help.

Copy link
Copy Markdown
Contributor

@orian orian Jan 26, 2026

Choose a reason for hiding this comment

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

IMO this is potentially millions of errors with nothing very useful, and this is not a real error, just a regular flow.

We probably could log a line with failing IP, not sure how big would be a value out of it? I don't think this service is right place to validate the IP.

Also, it's probably me who wrote this comment :D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ohh, I see. I thought your comment was on getting the country code :) I don't think logging the IP would be much value since there isn't really any action we could take to resolve it. Your initial comment about a metric is sound though, it'd be nice to have so visibility into how often we can't find the location.

Copy link
Copy Markdown
Contributor

@orian orian Jan 26, 2026

Choose a reason for hiding this comment

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

If we want to drastically improve the performance of livestream, I would skip processing events for tokens that have no subscription. Sure, when someone opens liveview the number of open sessions would be empty, but probably 95% of events could be skipped.

Other, not that crazy idea: instead using tokens: use real team_id. The tokens map perfectly to team_ids and this map would be not that big.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we want to drastically improve the performance of livestream, I would skip processing events for tokens that have no subscription

I have a change for that locally already :) I just need to test a few things before I open a PR.

Other, not that crazy idea: instead using tokens: use real team_id. The tokens map perfectly to team_ids and this map would be not that big.

I can take a look into this as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@orian , I'm going to complete this PR. I have a follow up PR to start skipping messages with no subscribers here: #45977.

I figure there will be some discussion on the implementation details, so I don't want to tie it up with the one :)

@jordanm-posthog
Copy link
Copy Markdown
Contributor Author

The geo wasn't used for the last 1.5y, I'm not sure how fast it is, I was thinking about remove it but didn't have enough time. When I've joined it also had some bugs (e.g. it was global, without proper filtering per team/token).

What are your thoughts on merging geo with the events stream in that case? I have a draft PR here to do that. I was a bit worried about speed initially, but maybe that isn't an issue if it's opt-in + we drop messages for slow clients

@jordanm-posthog jordanm-posthog merged commit b2fa2a9 into master Jan 27, 2026
104 checks passed
@jordanm-posthog jordanm-posthog deleted the jordanm-posthog/addCountryCodeToGeo branch January 27, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants