-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command sending intake v2 trace/events using go agent (#7)
* feat: add command sending v2 intake events using agent
- Loading branch information
1 parent
87efbbd
commit 1222a4c
Showing
5 changed files
with
257 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"os/signal" | ||
|
||
"go.elastic.co/apm/v2" | ||
|
||
"github.com/elastic/apm-tools/pkg/tracegen" | ||
) | ||
|
||
func main() { | ||
var cfg tracegen.Config | ||
|
||
flag.Float64Var(&cfg.SampleRate, "sample-rate", 1.0, "set the sample rate. allowed value: min: 0.0001, max: 1.000") | ||
flag.Parse() | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt) | ||
defer cancel() | ||
if err := Main(ctx, cfg); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func Main(ctx context.Context, cfg tracegen.Config) error { | ||
uniqueName := suffixString("trace") | ||
serviceName := "service-" + uniqueName | ||
tracer, err := apm.NewTracer(serviceName, "0.0.1") | ||
if err != nil { | ||
return errors.New("failed to instantiate tracer") | ||
} | ||
|
||
cfg.TraceID = tracegen.NewRandomTraceID() | ||
err = tracegen.IndexIntakeV2Trace(ctx, cfg, tracer) | ||
|
||
// TODO: call otlp trace gen with given traceID | ||
return err | ||
} | ||
|
||
func suffixString(s string) string { | ||
const letter = "abcdefghijklmnopqrstuvwxyz" | ||
b := make([]byte, 6) | ||
for i := range b { | ||
b[i] = letter[rand.Intn(len(letter))] | ||
} | ||
return fmt.Sprintf("%s-%s", s, string(b)) | ||
} |
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,92 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 tracegen contains functions that generate a trace including transaction, | ||
// span, error, and logs using elastic APM Go agent and opentelemtry-go | ||
package tracegen | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math" | ||
"time" | ||
|
||
"go.elastic.co/apm/v2" | ||
) | ||
|
||
type Config struct { | ||
SampleRate float64 | ||
TraceID apm.TraceID | ||
} | ||
|
||
// IndexIntakeV2Trace generate a trace including a transaction, a span and an error | ||
func IndexIntakeV2Trace(ctx context.Context, cfg Config, tracer *apm.Tracer) error { | ||
if cfg.SampleRate < 0.0001 || cfg.SampleRate > 1.0 { | ||
return errors.New("invalid sample rate provided. allowed value: 0.0001 <= sample-rate <= 1.0") | ||
} | ||
cfg.SampleRate = math.Round(cfg.SampleRate*10000) / 10000 | ||
|
||
// set sample rate | ||
ts := apm.NewTraceState(apm.TraceStateEntry{ | ||
Key: "es", Value: fmt.Sprintf("s:%.4g", cfg.SampleRate), | ||
}) | ||
|
||
traceContext := apm.TraceContext{ | ||
Trace: cfg.TraceID, | ||
Options: apm.TraceOptions(0).WithRecorded(true), | ||
State: ts, | ||
} | ||
|
||
tx := tracer.StartTransactionOptions("parent-tx", "apmtool", apm.TransactionOptions{ | ||
TraceContext: traceContext, | ||
}) | ||
|
||
span := tx.StartSpanOptions("parent-span", "apmtool", apm.SpanOptions{ | ||
Parent: tx.TraceContext(), | ||
}) | ||
|
||
exit := tx.StartSpanOptions("exit-span", "apmtool", apm.SpanOptions{ | ||
Parent: span.TraceContext(), | ||
ExitSpan: true, | ||
}) | ||
|
||
exit.Context.SetServiceTarget(apm.ServiceTargetSpanContext{ | ||
Type: "service_type", | ||
Name: "service_name", | ||
}) | ||
|
||
exit.Duration = 999 * time.Millisecond | ||
exit.Outcome = "failure" | ||
|
||
// error | ||
e := tracer.NewError(errors.New("timeout")) | ||
e.Culprit = "timeout" | ||
e.SetSpan(exit) | ||
e.Send() | ||
exit.End() | ||
|
||
span.Duration = time.Second | ||
span.Outcome = "success" | ||
span.End() | ||
tx.Duration = 2 * time.Second | ||
tx.Outcome = "success" | ||
tx.End() | ||
tracer.Flush(ctx.Done()) | ||
|
||
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,36 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 tracegen | ||
|
||
import ( | ||
"encoding/binary" | ||
"math/rand" | ||
"time" | ||
|
||
"go.elastic.co/apm/v2" | ||
) | ||
|
||
// NewRandomTraceID returns randomly generated apm.TraceID | ||
// which is also compatible with otel's trace.TraceID | ||
func NewRandomTraceID() apm.TraceID { | ||
var traceID apm.TraceID | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
binary.LittleEndian.PutUint64(traceID[:8], r.Uint64()) | ||
binary.LittleEndian.PutUint64(traceID[8:], r.Uint64()) | ||
return traceID | ||
} |