Skip to content

Commit

Permalink
[processor/geoipprocessor] Read both the client.address and the sourc…
Browse files Browse the repository at this point in the history
…e.address attributes
  • Loading branch information
bencehornak committed Jan 2, 2025
1 parent 22cd8a2 commit dd1e8cf
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 4 deletions.
2 changes: 1 addition & 1 deletion processor/geoipprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

## Description

The geoIP processor `geoipprocessor` enhances the attributes of a span, log, or metric by appending information about the geographical location of an IP address. To add geographical information, the IP address must be included in the attributes using the [`source.address` semantic conventions key attribute](https://github.com/open-telemetry/semantic-conventions/blob/v1.26.0/docs/general/attributes.md#source). By default, only the resource attributes will be modified. Please refer to [config.go](./config.go) for the config spec.
The geoIP processor `geoipprocessor` enhances the attributes of a span, log, or metric by appending information about the geographical location of an IP address. To add geographical information, the IP address must be included in the attributes using the [`client.address`](https://github.com/open-telemetry/semantic-conventions/blob/v1.29.0/docs/general/attributes.md#client-attributes) or the [`source.address`](https://github.com/open-telemetry/semantic-conventions/blob/v1.29.0/docs/general/attributes.md#source) semantic conventions key attribute. By default, only the resource attributes will be modified. Please refer to [config.go](./config.go) for the config spec.

### Geographical location metadata

Expand Down
5 changes: 4 additions & 1 deletion processor/geoipprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ var (
// defaultResourceAttributes holds a list of default resource attribute keys.
// These keys are used to identify an IP address attribute associated with the resource.
defaultResourceAttributes = []attribute.Key{
semconv.SourceAddressKey, // This key represents the standard source address attribute as defined in the OpenTelemetry semantic conventions.
// The client attributes are in use by the HTTP semantic conventions
semconv.ClientAddressKey,
// The source attributes are used when there is no client/server relationship between the two sides, or when that relationship is unknown
semconv.SourceAddressKey,
}
)

Expand Down
7 changes: 5 additions & 2 deletions processor/geoipprocessor/geoip_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processortest"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
Expand Down Expand Up @@ -121,6 +120,11 @@ var testCases = []struct {
goldenDir: "attribute_source_address",
context: record,
},
{
name: "client address located in inner attributes",
goldenDir: "attribute_client_address",
context: record,
},
}

func compareAllSignals(cfg component.Config, goldenDir string) func(t *testing.T) {
Expand Down Expand Up @@ -197,7 +201,6 @@ func TestProcessor(t *testing.T) {
baseProviderMock.LocationF = func(_ context.Context, sourceIP net.IP) (attribute.Set, error) {
if sourceIP.Equal(net.IPv4(1, 2, 3, 4)) {
return attribute.NewSet([]attribute.KeyValue{
semconv.SourceAddress("1.2.3.4"),
attribute.String(conventions.AttributeGeoCityName, "Boxford"),
attribute.String(conventions.AttributeGeoContinentCode, "EU"),
attribute.String(conventions.AttributeGeoContinentName, "Europe"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resourceLogs:
- resource:
attributes:
- key: ip
value:
stringValue: 1.2.2.1
scopeLogs:
- logRecords:
- attributes:
- key: client.address
value:
stringValue: 1.2.3.4
- key: host.name
value:
stringValue: HOST.ONE
- key: log.file.name
value:
stringValue: one.log
body:
stringValue: hello one
spanId: ""
traceId: ""
scope: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
resourceMetrics:
- resource:
attributes:
- key: ip
value:
stringValue: 1.2.2.1
schemaUrl: https://test-res-schema.com/schema
scopeMetrics:
- metrics:
- description: This also isn't a real metric
name: storage.amplitude
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "0"
attributes:
- key: a
value:
stringValue: AAAA
- key: client.address
value:
stringValue: 1.2.3.4
isMonotonic: false
unit: "1"
- name: delta.histogram.test
histogram:
aggregationTemporality: 1
dataPoints:
- explicitBounds: [0.01, 0.1, 1, 10, 100]
timeUnixNano: "1000000"
bucketCounts: [9, 12, 17, 8, 34]
attributes:
- key: client.address
value:
stringValue: 1.2.3.4
- name: summary.test
summary:
dataPoints:
- timeUnixNano: "1000000"
quantileValues:
- quantile: 0.25
value: 50
- quantile: 0.5
value: 20
- quantile: 0.75
value: 75
- quantile: 0.95
value: 10
attributes:
- key: client.address
value:
stringValue: 1.2.3.4
- gauge:
dataPoints:
- asDouble: 345
attributes:
- key: client.address
value:
stringValue: 1.2.3.4
- key: aaa
value:
stringValue: bbb
timeUnixNano: "1000000"
name: test.gauge
schemaUrl: https://test-scope-schema.com/schema
scope:
attributes:
- key: foo
value:
stringValue: bar
name: MyTestInstrument
version: 1.2.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
resourceSpans:
- resource:
attributes:
scopeSpans:
- scope: {}
spans:
- attributes:
- key: http.request.method
value:
stringValue: POST
- key: url.full
value:
stringValue: https://www.foo.bar/search?q=OpenTelemetry#SemConv
- key: http.response.status_code
value:
intValue: "201"
- key: ip
value:
stringValue: 1.2.2.1
- key: client.address
value:
stringValue: 1.2.3.4
endTimeUnixNano: "1581452773000000789"
events:
- attributes:
- key: event.attr1
value:
stringValue: foo2
- key: event.attr2
value:
stringValue: bar2
name: event2
timeUnixNano: "1581452773000000123"
name: span-elastic-http
parentSpanId: bcff497b5a47310f
spanId: ""
startTimeUnixNano: "1581452772000000321"
status: {}
traceId: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
resourceLogs:
- resource:
attributes:
- key: ip
value:
stringValue: 1.2.2.1
scopeLogs:
- logRecords:
- attributes:
- key: client.address
value:
stringValue: 1.2.3.4
- key: host.name
value:
stringValue: HOST.ONE
- key: log.file.name
value:
stringValue: one.log
- key: geo.city_name
value:
stringValue: Boxford
- key: geo.continent_code
value:
stringValue: EU
- key: geo.continent_name
value:
stringValue: Europe
- key: geo.country_iso_code
value:
stringValue: GB
- key: geo.country_name
value:
stringValue: United Kingdom
- key: geo.location.lat
value:
doubleValue: 1234
- key: geo.location.lon
value:
doubleValue: 5678
- key: geo.postal_code
value:
stringValue: OX1
- key: geo.region_iso_code
value:
stringValue: WBK
- key: geo.region_name
value:
stringValue: West Berkshire
- key: geo.timezone
value:
stringValue: Europe/London
body:
stringValue: hello one
spanId: ""
traceId: ""
scope: {}
Loading

0 comments on commit dd1e8cf

Please sign in to comment.