Skip to content
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
merged 22 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7805f68
feat: add maxmind geoip provider
rogercoll Jun 10, 2024
780dc69
chore: add license headers
rogercoll Jun 10, 2024
3e49f02
feat: return error if no metadata found
rogercoll Jun 10, 2024
6a2f82e
docs: add provider README.md
rogercoll Jun 10, 2024
b9511bf
chore: add changelog entry
rogercoll Jun 10, 2024
d51ff81
chore: run goporto
rogercoll Jun 10, 2024
ecef836
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 11, 2024
05458bf
chore: unify go indirect dependencies block
rogercoll Jun 11, 2024
2feb8ba
refactor: use database_path configuration option
rogercoll Jun 11, 2024
c4a617b
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 12, 2024
8762af0
feat: skip unspecifed addresses
rogercoll Jun 17, 2024
5f17d2e
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 17, 2024
17be551
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 18, 2024
c955117
chore: rename name variable to langCode
rogercoll Jun 18, 2024
a0979ff
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 18, 2024
db1ffc9
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 19, 2024
91305ad
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 20, 2024
607105f
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 20, 2024
b2b034a
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 21, 2024
50486bf
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 25, 2024
56b6ccb
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 26, 2024
84f6ba8
Merge branch 'main' into add_maxmind_geoprovider
rogercoll Jun 27, 2024
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
27 changes: 27 additions & 0 deletions .chloggen/add_maxmind_geoprovider.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: geoipprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add MaxMind geoip provider for GeoIP2-City and GeoLite2-City databases.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32663]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
22 changes: 19 additions & 3 deletions processor/geoipprocessor/geoip_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package geoipprocessor // import "github.com/open-telemetry/opentelemetry-collec
import (
"context"
"errors"
"fmt"
"net"

"go.opentelemetry.io/collector/pdata/pcommon"
Expand All @@ -17,7 +18,11 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider"
)

var errIPNotFound = errors.New("no IP address found in the resource attributes")
var (
errIPNotFound = errors.New("no IP address found in the resource attributes")
errParseIP = errors.New("could not parse IP address")
errUnspecifiedIP = errors.New("unspecified address")
)

// newGeoIPProcessor creates a new instance of geoIPProcessor with the specified fields.
type geoIPProcessor struct {
Expand All @@ -31,15 +36,26 @@ func newGeoIPProcessor(resourceAttributes []attribute.Key) *geoIPProcessor {
}
}

// parseIP parses a string to a net.IP type and returns an error if the IP is invalid or unspecified.
func parseIP(strIP string) (net.IP, error) {
ip := net.ParseIP(strIP)
if ip == nil {
return nil, fmt.Errorf("%w address: %s", errParseIP, strIP)
} else if ip.IsUnspecified() {
return nil, fmt.Errorf("%w address: %s", errUnspecifiedIP, strIP)
}
return ip, nil
}

// ipFromResourceAttributes extracts an IP address from the given resource's attributes based on the specified fields.
// It returns the first IP address if found, or an error if no valid IP address is found.
func ipFromResourceAttributes(attributes []attribute.Key, resource pcommon.Resource) (net.IP, error) {
for _, attr := range attributes {
if ipField, found := resource.Attributes().Get(string(attr)); found {
ipAttribute := net.ParseIP(ipField.AsString())
// The attribute might contain a domain name. Skip any net.ParseIP error until we have a fine-grained error propagation strategy.
// TODO: propagate an error once error_mode configuration option is available (e.g. transformprocessor)
if ipAttribute != nil {
ipAttribute, err := parseIP(ipField.AsString())
if err == nil && ipAttribute != nil {
return ipAttribute, nil
}
}
Expand Down
17 changes: 17 additions & 0 deletions processor/geoipprocessor/geoip_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ func TestProcessPdata(t *testing.T) {
}),
},
},
{
name: "default source.ip attribute with an unspecified IP address should be skipped",
resourceAttributes: defaultResourceAttributes,
initResourceAttributes: []generateResourceFunc{
withAttributes([]attribute.KeyValue{
attribute.String(string(semconv.SourceAddressKey), "0.0.0.0"),
}),
},
geoLocationMock: func(context.Context, net.IP) (attribute.Set, error) {
return attribute.NewSet([]attribute.KeyValue{attribute.String("geo.city_name", "barcelona")}...), nil
},
expectedResourceAttributes: []generateResourceFunc{
withAttributes([]attribute.KeyValue{
attribute.String(string(semconv.SourceAddressKey), "0.0.0.0"),
}),
},
},
{
name: "custom resource attribute",
resourceAttributes: []attribute.Key{"ip"},
Expand Down
5 changes: 5 additions & 0 deletions processor/geoipprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ require (
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
github.com/maxmind/MaxMind-DB v0.0.0-20240605211347-880f6b4b5eb6
github.com/maxmind/mmdbwriter v1.0.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.103.0 // indirect
github.com/oschwald/geoip2-golang v1.11.0
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand All @@ -49,6 +53,7 @@ require (
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go4.org/netipx v0.0.0-20230824141953-6213f710f925 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions processor/geoipprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions processor/geoipprocessor/internal/convention/attributes.go
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"
)
6 changes: 6 additions & 0 deletions processor/geoipprocessor/internal/provider/geoipprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import (
"context"
"net"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/attribute"
)

// Config is the configuration of a GeoIPProvider.
type Config interface {
component.ConfigValidator
}

// GeoIPProvider defines methods for obtaining the geographical location based on the provided IP address.
type GeoIPProvider interface {
// Location returns a set of attributes representing the geographical location for the given IP address. It requires a context for managing request lifetime.
Expand Down
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:

- `database_path`: local file path to a GeoIP2-City or GeoLite2-City database.
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 "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider/maxmindprovider"

import (
"errors"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider"
)

// Config defines configuration for MaxMind provider.
type Config struct {
// DatabasePath section allows specifying a local GeoIP database
// file to retrieve the geographical metadata from.
DatabasePath string `mapstructure:"database_path"`
}

var _ provider.Config = (*Config)(nil)

// Validate implements provider.Config.
func (c *Config) Validate() error {
if c.DatabasePath == "" {
return errors.New("a local geoIP database path must be provided")
}
return nil
}
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 "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider/maxmindprovider"

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 (
// defaultLanguageCode specifies English as the default Geolocation language code, see https://dev.maxmind.com/geoip/docs/web-services/responses#languages
defaultLanguageCode = "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 code to be used in name retrieval, e.g. "en" or "pt-BR"
langCode string
}

var _ provider.GeoIPProvider = (*maxMindProvider)(nil)

func newMaxMindProvider(cfg *Config) (*maxMindProvider, error) {
geoReader, err := geoip2.Open(cfg.DatabasePath)
if err != nil {
return nil, fmt.Errorf("could not open geoip database: %w", err)
}

return &maxMindProvider{geoReader: geoReader, langCode: defaultLanguageCode}, 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.langCode])
// country
appendIfNotEmpty(conventions.AttributeGeoCountryName, city.Country.Names[g.langCode])
appendIfNotEmpty(conventions.AttributeGeoCountryIsoCode, city.Country.IsoCode)
// continent
appendIfNotEmpty(conventions.AttributeGeoContinentName, city.Continent.Names[g.langCode])
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.langCode])
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
}
Loading
Loading