-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
receiver/prometheus/internal: add createNodeAndResourcePdata for Prometheus->OTLP Pdata #3139
Merged
bogdandrutu
merged 4 commits into
open-telemetry:main
from
orijtech:receiver-prometheus-createNodeAndResource
Jun 7, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74e96e5
receiver/prometheus/internal: add createNodeAndResourcePdata for Prom…
odeke-em dd55c24
Use .Attributes to skip unsafe extraction plus use require.Equal
odeke-em 3977f60
Add comment and rename parity test check
odeke-em 6ea4b9e
Add createNodeAndResource unit test
odeke-em 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal | ||
|
||
import ( | ||
"net" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.opentelemetry.io/collector/translator/conventions" | ||
) | ||
|
||
func createNodeAndResourcePdata(job, instance, scheme string) pdata.Resource { | ||
host, port, err := net.SplitHostPort(instance) | ||
if err != nil { | ||
host = instance | ||
} | ||
resource := pdata.NewResource() | ||
attrs := resource.Attributes() | ||
attrs.UpsertString(conventions.AttributeServiceName, job) | ||
attrs.UpsertString(conventions.AttributeHostName, host) | ||
attrs.UpsertString(jobAttr, job) | ||
attrs.UpsertString(instanceAttr, instance) | ||
attrs.UpsertString(portAttr, port) | ||
attrs.UpsertString(schemeAttr, scheme) | ||
|
||
return resource | ||
} |
120 changes: 120 additions & 0 deletions
120
receiver/prometheusreceiver/internal/prom_to_otlp_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,120 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.opentelemetry.io/collector/translator/internaldata" | ||
) | ||
|
||
// Parity test to ensure that createNodeAndResource produces identical results to createNodeAndResourcePdata. | ||
func TestCreateNodeAndResourceEquivalence(t *testing.T) { | ||
job, instance, scheme := "converter", "ocmetrics", "http" | ||
ocNode, ocResource := createNodeAndResource(job, instance, scheme) | ||
mdFromOC := internaldata.OCToMetrics(ocNode, ocResource, | ||
// We need to pass in a dummy set of metrics | ||
// just to populate and allow for full conversion. | ||
[]*metricspb.Metric{ | ||
{ | ||
MetricDescriptor: &metricspb.MetricDescriptor{ | ||
Name: "m1", | ||
Description: "d1", | ||
Unit: "By", | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
fromOCResource := mdFromOC.ResourceMetrics().At(0).Resource().Attributes().Sort() | ||
byDirectOTLPResource := createNodeAndResourcePdata(job, instance, scheme).Attributes().Sort() | ||
|
||
require.Equal(t, byDirectOTLPResource, fromOCResource) | ||
} | ||
|
||
type jobInstanceDefinition struct { | ||
job, instance, host, scheme, port string | ||
} | ||
|
||
func makeResourceWithJobInstanceScheme(def *jobInstanceDefinition) pdata.Resource { | ||
resource := pdata.NewResource() | ||
attrs := resource.Attributes() | ||
// Using hardcoded values to assert on outward expectations so that | ||
// when variables change, these tests will fail and we'll have reports. | ||
attrs.UpsertString("service.name", def.job) | ||
attrs.UpsertString("host.name", def.host) | ||
attrs.UpsertString("job", def.job) | ||
attrs.UpsertString("instance", def.instance) | ||
attrs.UpsertString("port", def.port) | ||
attrs.UpsertString("scheme", def.scheme) | ||
return resource | ||
} | ||
|
||
func TestCreateNodeAndResourcePromToOTLP(t *testing.T) { | ||
tests := []struct { | ||
name, job string | ||
instance string | ||
scheme string | ||
want pdata.Resource | ||
}{ | ||
{ | ||
name: "all attributes proper", | ||
job: "job", instance: "localhost:8888", scheme: "http", | ||
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{ | ||
"job", "localhost:8888", "localhost", "http", "8888", | ||
}), | ||
}, | ||
{ | ||
name: "missing port", | ||
job: "job", instance: "myinstance", scheme: "https", | ||
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{ | ||
"job", "myinstance", "myinstance", "https", "", | ||
}), | ||
}, | ||
{ | ||
name: "blank scheme", | ||
job: "job", instance: "myinstance:443", scheme: "", | ||
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{ | ||
"job", "myinstance:443", "myinstance", "", "443", | ||
}), | ||
}, | ||
{ | ||
name: "blank instance, blank scheme", | ||
job: "job", instance: "", scheme: "", | ||
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{ | ||
"job", "", "", "", "", | ||
}), | ||
}, | ||
{ | ||
name: "blank instance, non-blank scheme", | ||
job: "job", instance: "", scheme: "http", | ||
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{ | ||
"job", "", "", "http", "", | ||
}), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := createNodeAndResourcePdata(tt.job, tt.instance, tt.scheme) | ||
require.Equal(t, got, tt.want) | ||
}) | ||
} | ||
} |
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.
@alolita Does anyone own the spec for prometheus mapping of the standard conventions? These seem somewhat arbitrary. If they can be excluded, I'd only add the prometheus specific attributes for now
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.
@anuraaga this is a straight up translation of what already exists in the current converter.
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.
I know - it doesn't block this PR but someone needs to be working on that.
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.
Cool!
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.
Maybe we need to open an issue to track this.
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 upsert the job to service.name? Shouldn't it be better to check if service.name has been set?