-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Hickey <[email protected]>
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
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
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,96 @@ | ||
// Copyright 2022, 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 instanaexporter | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/testutils" | ||
) | ||
|
||
func TestPushConvertedDefaultTraces(t *testing.T) { | ||
traceServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.WriteHeader(http.StatusAccepted) | ||
})) | ||
defer traceServer.Close() | ||
|
||
cfg := Config{ | ||
AgentKey: "key11", | ||
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: traceServer.URL}, | ||
Endpoint: traceServer.URL, | ||
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "valid")), | ||
} | ||
|
||
instanaExporter, err := newInstanaExporter(&cfg, | ||
componenttest.NewNopExporterCreateSettings()) | ||
assert.NoError(t, err) | ||
|
||
err = instanaExporter.pushConvertedTraces(context.Background(), testutils.TestTraces.Clone()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestPushConvertedSimpleTraces(t *testing.T) { | ||
got := make(chan string, 1) | ||
traceServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
assert.Equal(t, "key11", req.Header.Get("x-instana-key")) | ||
got <- req.Header.Get("Content-Type") | ||
rw.WriteHeader(http.StatusAccepted) | ||
})) | ||
defer traceServer.Close() | ||
|
||
cfg := Config{ | ||
AgentKey: "key11", | ||
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: traceServer.URL}, | ||
Endpoint: traceServer.URL, | ||
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "valid")), | ||
} | ||
|
||
instanaExporter, err := newInstanaExporter(&cfg, | ||
componenttest.NewNopExporterCreateSettings()) | ||
assert.NoError(t, err) | ||
|
||
err = instanaExporter.pushConvertedTraces(context.Background(), simpleTraces()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func simpleTraces() ptrace.Traces { | ||
return genTraces(pcommon.NewTraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}), nil) | ||
} | ||
|
||
func genTraces(traceID pcommon.TraceID, attrs map[string]interface{}) ptrace.Traces { | ||
traces := ptrace.NewTraces() | ||
rspans := traces.ResourceSpans().AppendEmpty() | ||
span := rspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty() | ||
span.SetTraceID(traceID) | ||
span.SetSpanID(pcommon.NewSpanID([8]byte{0, 0, 0, 0, 1, 2, 3, 4})) | ||
if attrs == nil { | ||
return traces | ||
} | ||
pcommon.NewMapFromRaw(attrs).Range(func(k string, v pcommon.Value) bool { | ||
rspans.Resource().Attributes().Insert(k, v) | ||
return true | ||
}) | ||
return traces | ||
} |
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,43 @@ | ||
// Copyright 2022, 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 testutils | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
) | ||
|
||
var ( | ||
testAttributes = map[string]string{"instana.agent": "agent1"} | ||
// TestTraces traces for tests. | ||
TestTraces = newTracesWithAttributeMap(testAttributes) | ||
) | ||
|
||
func fillAttributeMap(attrs pcommon.Map, mp map[string]string) { | ||
attrs.Clear() | ||
attrs.EnsureCapacity(len(mp)) | ||
for k, v := range mp { | ||
attrs.Insert(k, pcommon.NewValueString(v)) | ||
} | ||
} | ||
|
||
func newTracesWithAttributeMap(mp map[string]string) ptrace.Traces { | ||
traces := ptrace.NewTraces() | ||
resourceSpans := traces.ResourceSpans() | ||
rs := resourceSpans.AppendEmpty() | ||
fillAttributeMap(rs.Resource().Attributes(), mp) | ||
rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty() | ||
return traces | ||
} |