-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter/instana] Add implementation (#13620)
* Add Instana exporter implementation Signed-off-by: Martin Hickey <[email protected]> * Add unit tests Signed-off-by: Martin Hickey <[email protected]> * Add changelog Signed-off-by: Martin Hickey <[email protected]> * Add more unit tests Signed-off-by: Martin Hickey <[email protected]> * Update fater review Review comments: - #13620 (review) Signed-off-by: Martin Hickey <[email protected]> * Update after review Review comments: - #13620 (comment) - #13620 (comment) - #13620 (comment) - #13620 (comment) - #13620 (comment) - #13620 (comment) - #13620 (comment) - #13620 (comment) - #13620 (comment) Signed-off-by: Martin Hickey <[email protected]> Signed-off-by: Martin Hickey <[email protected]>
- Loading branch information
Showing
20 changed files
with
1,469 additions
and
17 deletions.
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
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,137 @@ | ||
// 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 "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter" | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"runtime" | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/consumer/consumererror" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/backend" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/converter" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/converter/model" | ||
) | ||
|
||
type instanaExporter struct { | ||
config *Config | ||
client *http.Client | ||
settings component.TelemetrySettings | ||
userAgent string | ||
} | ||
|
||
func (e *instanaExporter) start(_ context.Context, host component.Host) error { | ||
client, err := e.config.HTTPClientSettings.ToClient(host, e.settings) | ||
if err != nil { | ||
return err | ||
} | ||
e.client = client | ||
return nil | ||
} | ||
|
||
func (e *instanaExporter) pushConvertedTraces(ctx context.Context, td ptrace.Traces) error { | ||
converter := converter.NewConvertAllConverter(e.settings.Logger) | ||
spans := make([]model.Span, 0) | ||
|
||
hostID := "" | ||
resourceSpans := td.ResourceSpans() | ||
for i := 0; i < resourceSpans.Len(); i++ { | ||
resSpan := resourceSpans.At(i) | ||
|
||
resource := resSpan.Resource() | ||
|
||
hostIDAttr, ok := resource.Attributes().Get(backend.AttributeInstanaHostID) | ||
if ok { | ||
hostID = hostIDAttr.StringVal() | ||
} | ||
|
||
ilSpans := resSpan.ScopeSpans() | ||
for j := 0; j < ilSpans.Len(); j++ { | ||
converterBundle := converter.ConvertSpans(resource.Attributes(), ilSpans.At(j).Spans()) | ||
|
||
spans = append(spans, converterBundle.Spans...) | ||
} | ||
} | ||
|
||
bundle := model.Bundle{Spans: spans} | ||
if len(bundle.Spans) == 0 { | ||
// skip exporting, nothing to do | ||
return nil | ||
} | ||
|
||
req, err := bundle.Marshal() | ||
if err != nil { | ||
return consumererror.NewPermanent(err) | ||
} | ||
|
||
headers := map[string]string{ | ||
backend.HeaderKey: e.config.AgentKey, | ||
backend.HeaderHost: hostID, | ||
// Used only by the Instana agent and can be set to "0" for the exporter | ||
backend.HeaderTime: "0", | ||
} | ||
|
||
return e.export(ctx, e.config.Endpoint, headers, req) | ||
} | ||
|
||
func newInstanaExporter(cfg config.Exporter, set component.ExporterCreateSettings) *instanaExporter { | ||
iCfg := cfg.(*Config) | ||
userAgent := fmt.Sprintf("%s/%s (%s/%s)", set.BuildInfo.Description, set.BuildInfo.Version, runtime.GOOS, runtime.GOARCH) | ||
return &instanaExporter{ | ||
config: iCfg, | ||
settings: set.TelemetrySettings, | ||
userAgent: userAgent, | ||
} | ||
} | ||
|
||
func (e *instanaExporter) export(ctx context.Context, url string, header map[string]string, request []byte) error { | ||
url = strings.TrimSuffix(url, "/") + "/bundle" | ||
e.settings.Logger.Debug("Preparing to make HTTP request", zap.String("url", url)) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(request)) | ||
if err != nil { | ||
return consumererror.NewPermanent(err) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("User-Agent", e.userAgent) | ||
|
||
for name, value := range header { | ||
req.Header.Set(name, value) | ||
} | ||
|
||
resp, err := e.client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send a request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode >= 400 && resp.StatusCode <= 499 { | ||
return consumererror.NewPermanent(fmt.Errorf("error when sending payload to %s: %s", | ||
url, resp.Status)) | ||
} | ||
if resp.StatusCode >= 500 && resp.StatusCode <= 599 { | ||
return fmt.Errorf("error when sending payload to %s: %s", url, resp.Status) | ||
} | ||
|
||
return nil | ||
} |
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,95 @@ | ||
// 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 := newInstanaExporter(&cfg, componenttest.NewNopExporterCreateSettings()) | ||
ctx := context.Background() | ||
err := instanaExporter.start(ctx, componenttest.NewNopHost()) | ||
assert.NoError(t, err) | ||
|
||
err = instanaExporter.pushConvertedTraces(ctx, testutils.TestTraces.Clone()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestPushConvertedSimpleTraces(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 := newInstanaExporter(&cfg, componenttest.NewNopExporterCreateSettings()) | ||
ctx := context.Background() | ||
err := instanaExporter.start(ctx, componenttest.NewNopHost()) | ||
assert.NoError(t, err) | ||
|
||
err = instanaExporter.pushConvertedTraces(ctx, 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
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.
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,25 @@ | ||
// 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 backend // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/backend" | ||
|
||
const ( | ||
// AttributeInstanaHostID can be used to distinguish multiple hosts' data | ||
// being processed by a single collector (in a chained scenario) | ||
AttributeInstanaHostID = "instana.host.id" | ||
|
||
HeaderKey = "x-instana-key" | ||
HeaderHost = "x-instana-host" | ||
HeaderTime = "x-instana-time" | ||
) |
Oops, something went wrong.