-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[processor/geoip] Add maxmind geoprovider #33451
Merged
andrzej-stencel
merged 22 commits into
open-telemetry:main
from
rogercoll:add_maxmind_geoprovider
Jun 27, 2024
Merged
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7805f68
feat: add maxmind geoip provider
rogercoll 780dc69
chore: add license headers
rogercoll 3e49f02
feat: return error if no metadata found
rogercoll 6a2f82e
docs: add provider README.md
rogercoll b9511bf
chore: add changelog entry
rogercoll d51ff81
chore: run goporto
rogercoll ecef836
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 05458bf
chore: unify go indirect dependencies block
rogercoll 2feb8ba
refactor: use database_path configuration option
rogercoll c4a617b
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 8762af0
feat: skip unspecifed addresses
rogercoll 5f17d2e
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 17be551
Merge branch 'main' into add_maxmind_geoprovider
rogercoll c955117
chore: rename name variable to langCode
rogercoll a0979ff
Merge branch 'main' into add_maxmind_geoprovider
rogercoll db1ffc9
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 91305ad
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 607105f
Merge branch 'main' into add_maxmind_geoprovider
rogercoll b2b034a
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 50486bf
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 56b6ccb
Merge branch 'main' into add_maxmind_geoprovider
rogercoll 84f6ba8
Merge branch 'main' into add_maxmind_geoprovider
rogercoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
processor/geoipprocessor/internal/convention/attributes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package conventions // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/convention" | ||
|
||
// TODO: replace for semconv once https://github.com/open-telemetry/semantic-conventions/issues/1033 is closed. | ||
const ( | ||
// AttributeGeoCityName represents the attribute name for the city name in geographical data. | ||
AttributeGeoCityName = "geo.city_name" | ||
|
||
// AttributeGeoPostalCode represents the attribute name for the city postal code. | ||
AttributeGeoPostalCode = "geo.postal_code" | ||
|
||
// AttributeGeoCountryName represents the attribute name for the country name in geographical data. | ||
AttributeGeoCountryName = "geo.country_name" | ||
|
||
// AttributeGeoCountryIsoCode represents the attribute name for the Two-letter ISO Country Code. | ||
AttributeGeoCountryIsoCode = "geo.country_iso_code" | ||
|
||
// AttributeGeoContinentName represents the attribute name for the continent name in geographical data. | ||
AttributeGeoContinentName = "geo.continent_name" | ||
|
||
// AttributeGeoContinentIsoCode represents the attribute name for the Two-letter Continent Code. | ||
AttributeGeoContinentCode = "geo.continent_code" | ||
|
||
// AttributeGeoRegionName represents the attribute name for the region name in geographical data. | ||
AttributeGeoRegionName = "geo.region_name" | ||
|
||
// AttributeGeoRegionIsoCode represents the attribute name for the Two-letter ISO Region Code. | ||
AttributeGeoRegionIsoCode = "geo.region_iso_code" | ||
|
||
// AttributeGeoTimezone represents the attribute name for the timezone. | ||
AttributeGeoTimezone = "geo.timezone" | ||
|
||
// AttributeGeoLocationLat represents the attribute name for the latitude. | ||
AttributeGeoLocationLat = "geo.location.lat" | ||
|
||
// AttributeGeoLocationLon represents the attribute name for the longitude. | ||
AttributeGeoLocationLon = "geo.location.lon" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
processor/geoipprocessor/internal/provider/maxmindprovider/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# MaxMind GeoIP Provider | ||
|
||
This package provides a MaxMind GeoIP provider for use with the OpenTelemetry GeoIP processor. It leverages the [geoip2-golang package](https://github.com/oschwald/geoip2-golang) to query geographical information associated with IP addresses from MaxMind databases. See recommended clients: https://dev.maxmind.com/geoip/docs/databases#api-clients | ||
|
||
# Features | ||
|
||
- Supports GeoIP2-City and GeoLite2-City database types. | ||
- Retrieves and returns geographical metadata for a given IP address. The generated attributes follow the internal [Geo conventions](../../convention/attributes.go). | ||
|
||
## Configuration | ||
|
||
The following configuration must be provided: | ||
|
||
- `geoip_database_path`: local file path to a GeoIP2-City or GeoLite2-City database. |
27 changes: 27 additions & 0 deletions
27
processor/geoipprocessor/internal/provider/maxmindprovider/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package maxmind | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider" | ||
) | ||
|
||
// Config defines configuration for MaxMind provider. | ||
type Config struct { | ||
// GeoIPDatabasePath section allows specifying a local GeoIP database | ||
// file to retrieve the geographical metadata from. | ||
GeoIPDatabasePath string `mapstructure:"geoip_database_path"` | ||
rogercoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var _ provider.Config = (*Config)(nil) | ||
|
||
// Validate implements provider.Config. | ||
func (c *Config) Validate() error { | ||
if c.GeoIPDatabasePath == "" { | ||
return errors.New("a local geoIP database path must be provided") | ||
} | ||
return nil | ||
} |
103 changes: 103 additions & 0 deletions
103
processor/geoipprocessor/internal/provider/maxmindprovider/provider.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package maxmind | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/oschwald/geoip2-golang" | ||
"go.opentelemetry.io/otel/attribute" | ||
|
||
conventions "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/convention" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider" | ||
) | ||
|
||
var ( | ||
// defaultLocale specifies English as the default Geolocation name, see https://dev.maxmind.com/geoip/docs/web-services/responses#languages | ||
defaultLocale = "en" | ||
geoIP2CityDBType = "GeoIP2-City" | ||
geoLite2CityDBType = "GeoLite2-City" | ||
|
||
errUnsupportedDB = errors.New("unsupported geo IP database type") | ||
errNoMetadataFound = errors.New("no geo IP metadata found") | ||
) | ||
|
||
type maxMindProvider struct { | ||
geoReader *geoip2.Reader | ||
// language/locale to be used in name retrieval | ||
name string | ||
andrzej-stencel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var _ provider.GeoIPProvider = (*maxMindProvider)(nil) | ||
|
||
func newMaxMindProvider(cfg *Config) (*maxMindProvider, error) { | ||
geoReader, err := geoip2.Open(cfg.GeoIPDatabasePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not open geoip database: %w", err) | ||
} | ||
|
||
return &maxMindProvider{geoReader: geoReader, name: defaultLocale}, nil | ||
} | ||
|
||
// Location implements provider.GeoIPProvider for MaxMind. If a non City database type is used or no metadata is found in the database, an error will be returned. | ||
func (g *maxMindProvider) Location(_ context.Context, ipAddress net.IP) (attribute.Set, error) { | ||
switch g.geoReader.Metadata().DatabaseType { | ||
case geoIP2CityDBType, geoLite2CityDBType: | ||
attrs, err := g.cityAttributes(ipAddress) | ||
if err != nil { | ||
return attribute.Set{}, err | ||
} else if len(*attrs) == 0 { | ||
return attribute.Set{}, errNoMetadataFound | ||
} | ||
return attribute.NewSet(*attrs...), nil | ||
default: | ||
return attribute.Set{}, fmt.Errorf("%w type: %s", errUnsupportedDB, g.geoReader.Metadata().DatabaseType) | ||
} | ||
} | ||
|
||
// cityAttributes returns a list of key-values containing geographical metadata associated to the provided IP. The key names are populated using the internal geo IP conventions package. If the an invalid or nil IP is provided, an error is returned. | ||
func (g *maxMindProvider) cityAttributes(ipAddress net.IP) (*[]attribute.KeyValue, error) { | ||
attributes := make([]attribute.KeyValue, 0, 11) | ||
|
||
city, err := g.geoReader.City(ipAddress) | ||
rogercoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// The exact set of top-level keys varies based on the particular GeoIP2 web service you are using. If a key maps to an undefined or empty value, it is not included in the JSON object. The following anonymous function appends the given key-value only if the value is not empty. | ||
appendIfNotEmpty := func(keyName, value string) { | ||
if value != "" { | ||
attributes = append(attributes, attribute.String(keyName, value)) | ||
} | ||
} | ||
|
||
// city | ||
appendIfNotEmpty(conventions.AttributeGeoCityName, city.City.Names[g.name]) | ||
// country | ||
appendIfNotEmpty(conventions.AttributeGeoCountryName, city.Country.Names[g.name]) | ||
appendIfNotEmpty(conventions.AttributeGeoCountryIsoCode, city.Country.IsoCode) | ||
// continent | ||
appendIfNotEmpty(conventions.AttributeGeoContinentName, city.Continent.Names[g.name]) | ||
appendIfNotEmpty(conventions.AttributeGeoContinentCode, city.Continent.Code) | ||
// postal code | ||
appendIfNotEmpty(conventions.AttributeGeoPostalCode, city.Postal.Code) | ||
// region | ||
if len(city.Subdivisions) > 0 { | ||
// The most specific subdivision is located at the last array position, see https://github.com/maxmind/GeoIP2-java/blob/2fe4c65424fed2c3c2449e5530381b6452b0560f/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java#L112 | ||
mostSpecificSubdivision := city.Subdivisions[len(city.Subdivisions)-1] | ||
appendIfNotEmpty(conventions.AttributeGeoRegionName, mostSpecificSubdivision.Names[g.name]) | ||
appendIfNotEmpty(conventions.AttributeGeoRegionIsoCode, mostSpecificSubdivision.IsoCode) | ||
} | ||
|
||
// location | ||
appendIfNotEmpty(conventions.AttributeGeoTimezone, city.Location.TimeZone) | ||
if city.Location.Latitude != 0 && city.Location.Longitude != 0 { | ||
attributes = append(attributes, attribute.Float64(conventions.AttributeGeoLocationLat, city.Location.Latitude), attribute.Float64(conventions.AttributeGeoLocationLon, city.Location.Longitude)) | ||
} | ||
|
||
return &attributes, err | ||
} |
129 changes: 129 additions & 0 deletions
129
processor/geoipprocessor/internal/provider/maxmindprovider/provider_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package maxmind | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"os" | ||
"testing" | ||
|
||
"github.com/maxmind/MaxMind-DB/pkg/writer" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/attribute" | ||
|
||
conventions "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/convention" | ||
) | ||
|
||
func TestInvalidNewProvider(t *testing.T) { | ||
_, err := newMaxMindProvider(&Config{}) | ||
require.ErrorContains(t, err, "could not open geoip database: open : no such file or directory") | ||
|
||
_, err = newMaxMindProvider(&Config{GeoIPDatabasePath: "no valid path"}) | ||
require.ErrorContains(t, err, "could not open geoip database: open no valid path: no such file or directory") | ||
} | ||
|
||
// generateLocalDB generates *.mmdb databases files given a source directory data. It uses a the writer functionality provided by MaxMind-Db/pkg/writer | ||
func generateLocalDB(t *testing.T, sourceData string) string { | ||
tmpDir, err := os.MkdirTemp("", "") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
w, err := writer.New(sourceData, tmpDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = w.WriteGeoIP2TestDB() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return tmpDir | ||
} | ||
|
||
// TestProviderLocation asserts that the MaxMind provider adds the geo location data given an IP. | ||
func TestProviderLocation(t *testing.T) { | ||
tmpDBfiles := generateLocalDB(t, "./testdata") | ||
defer os.RemoveAll(tmpDBfiles) | ||
|
||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
testDatabase string | ||
sourceIP net.IP | ||
expectedAttributes attribute.Set | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
name: "nil IP address", | ||
testDatabase: "GeoIP2-City-Test.mmdb", | ||
expectedErrMsg: "IP passed to Lookup cannot be nil", | ||
}, | ||
{ | ||
name: "unsupported database type", | ||
sourceIP: net.IPv4(0, 0, 0, 0), | ||
testDatabase: "GeoIP2-ISP-Test.mmdb", | ||
expectedErrMsg: "unsupported geo IP database type type: GeoIP2-ISP", | ||
}, | ||
{ | ||
name: "no IP metadata in database", | ||
sourceIP: net.IPv4(0, 0, 0, 0), | ||
testDatabase: "GeoIP2-City-Test.mmdb", | ||
expectedErrMsg: "no geo IP metadata found", | ||
}, | ||
{ | ||
name: "all attributes should be present for IPv4 using GeoLite2-City database", | ||
sourceIP: net.IPv4(1, 2, 3, 4), | ||
testDatabase: "GeoLite2-City-Test.mmdb", | ||
expectedAttributes: attribute.NewSet([]attribute.KeyValue{ | ||
attribute.String(conventions.AttributeGeoCityName, "Boxford"), | ||
attribute.String(conventions.AttributeGeoContinentCode, "EU"), | ||
attribute.String(conventions.AttributeGeoContinentName, "Europe"), | ||
attribute.String(conventions.AttributeGeoCountryIsoCode, "GB"), | ||
attribute.String(conventions.AttributeGeoCountryName, "United Kingdom"), | ||
attribute.String(conventions.AttributeGeoTimezone, "Europe/London"), | ||
attribute.String(conventions.AttributeGeoRegionIsoCode, "WBK"), | ||
attribute.String(conventions.AttributeGeoRegionName, "West Berkshire"), | ||
attribute.String(conventions.AttributeGeoPostalCode, "OX1"), | ||
attribute.Float64(conventions.AttributeGeoLocationLat, 1234), | ||
attribute.Float64(conventions.AttributeGeoLocationLon, 5678), | ||
}...), | ||
}, | ||
{ | ||
name: "subset attributes for IPv6 IP using GeoIP2-City database", | ||
sourceIP: net.ParseIP("2001:220::"), | ||
testDatabase: "GeoIP2-City-Test.mmdb", | ||
expectedAttributes: attribute.NewSet([]attribute.KeyValue{ | ||
attribute.String(conventions.AttributeGeoContinentCode, "AS"), | ||
attribute.String(conventions.AttributeGeoContinentName, "Asia"), | ||
attribute.String(conventions.AttributeGeoCountryIsoCode, "KR"), | ||
attribute.String(conventions.AttributeGeoCountryName, "South Korea"), | ||
attribute.String(conventions.AttributeGeoTimezone, "Asia/Seoul"), | ||
attribute.Float64(conventions.AttributeGeoLocationLat, 1), | ||
attribute.Float64(conventions.AttributeGeoLocationLon, 1), | ||
}...), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// prepare provider | ||
provider, err := newMaxMindProvider(&Config{GeoIPDatabasePath: tmpDBfiles + "/" + tt.testDatabase}) | ||
assert.NoError(t, err) | ||
|
||
// assert metrics | ||
actualAttributes, err := provider.Location(context.Background(), tt.sourceIP) | ||
if tt.expectedErrMsg != "" { | ||
assert.EqualError(t, err, tt.expectedErrMsg) | ||
return | ||
} | ||
|
||
assert.True(t, tt.expectedAttributes.Equals(&actualAttributes)) | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...r/geoipprocessor/internal/provider/maxmindprovider/testdata/GeoIP2-Anonymous-IP-Test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{}] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this a separate set
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure, it was automatically added by my editor's formatter (maybe when working from a go.mod subdirectory?). Require blocks unified in 05458bf