Skip to content

Commit

Permalink
Merge branch 'main' into duxing/WithCustomTag
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgajg1134 committed Aug 12, 2022
2 parents b4a432a + 8a68d59 commit 2847535
Show file tree
Hide file tree
Showing 107 changed files with 4,682 additions and 946 deletions.
18 changes: 8 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
version: 2.1

plain-go114: &plain-go116
plain-go117: &plain-go117
working_directory: /home/circleci/dd-trace-go.v1
docker:
- image: circleci/golang:1.16
- image: circleci/golang:1.17
environment:
GOPATH: "/home/circleci/go"

jobs:
metadata:
<<: *plain-go116
<<: *plain-go117

steps:
- checkout
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
go run checkcopyright.go
lint:
<<: *plain-go116
<<: *plain-go117

steps:
- checkout
Expand All @@ -71,10 +71,8 @@ jobs:
- run:
name: lint
command: |
go get -u golang.org/x/lint/golint
curl -L https://git.io/vp6lP | sh # https://github.com/alecthomas/gometalinter#binary-releases
./bin/gometalinter --disable-all --vendor --deadline=60s --enable=golint ./...
PACKAGE_NAMES=$(go list ./... | grep -v -e grpc.v12) # grpc.v12 is broken so skip it
go vet -- $PACKAGE_NAMES
test-core:
parameters:
Expand All @@ -90,7 +88,7 @@ jobs:
environment: # environment variables for the build itself
TEST_RESULTS: /tmp/test-results # path to where test results will be saved
CI_APP_SERVICE_NAME: dd-trace-go
<<: *plain-go116
<<: *plain-go117

steps:
- checkout
Expand Down Expand Up @@ -141,7 +139,7 @@ jobs:
CI_APP_SERVICE_NAME: dd-trace-go
working_directory: /home/circleci/dd-trace-go.v1
docker:
- image: circleci/golang:1.16
- image: circleci/golang:1.17
environment:
GOPATH: "/home/circleci/go"
- image: cassandra:3.7
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/appsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
runs-on: [ macos-latest, ubuntu-latest, windows-latest ]
go-version: [ 1.18, 1.17, 1.16 ]
go-version: [ 1.19, 1.18, 1.17 ]
cgo_enabled: # test it compiles with and without cgo
- 0
- 1
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
image: golang:${{ matrix.go-version }}-${{ matrix.distribution }}
strategy:
matrix:
go-version: [ 1.18, 1.17, 1.16 ]
go-version: [ 1.19, 1.18, 1.17 ]
distribution: [ bullseye, buster, alpine ]
fail-fast: false
steps:
Expand Down
13 changes: 10 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
### Contributing

Thanks for your interest in contributing! This is an open source project, so we appreciate community contributions.

Pull requests for bug fixes are welcome, but before submitting new features or changes to current functionalities [open an issue](https://github.com/DataDog/dd-trace-go/issues/new)
and discuss your ideas or propose the changes you wish to make. After a resolution is reached a PR can be submitted for review.
and discuss your ideas or propose the changes you wish to make. After a resolution is reached a PR can be submitted for review. PRs created before a decision has been reached may be closed.

For commit messages, try to use the same conventions as most Go projects, for example:
```
Expand All @@ -28,15 +30,20 @@ The code coverage report has a target of 90%. This is the goal, but is not a har

Please feel free to comment on a PR if there is any difficulty or confusion about any of the checks.

#### What to expect

We try to review new PRs within a week of them being opened. If more than two weeks have passed with no reply, please feel free to comment on the PR to bubble it up.

If a PR sits open for more than a month awaiting work or replies by the author, the PR may be closed due to staleness. If you would like to work on it again in the future, feel free to open a new PR and someone will review.

### Style guidelines

A set of [Style guidelines](https://github.com/DataDog/dd-trace-go/wiki/Style-guidelines) was added to our Wiki. Please spend some time browsing it.
It will help tremendously in avoiding comments and speeding up the PR process.


### Integrations

Please view our contrib [README.md](contrib/README.md) for information on new integrations.
Please view our contrib [README.md](contrib/README.md) for information on new integrations. If you need support for a new integration, please file an issue to discuss before opening a PR.

### Go Modules

Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Component,Origin,License,Copyright
import,io.opentracing,Apache-2.0,Copyright 2016-2017 The OpenTracing Authors
appsec,https://github.com/DataDog/libddwaf,Apache-2.0 OR BSD-3-Clause,Copyright (c) 2021 Datadog <info@datadoghq.com>
golang,https://go.googlesource.com/go,BSD-3-Clause,Copyright (c) 2009 The Go Authors
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ Datadog APM for Go is built upon dependencies defined in specific versions of th
<!-- NOTE: When updating the below section ensure you update the minimum supported version listed in the public docs here: https://docs.datadoghq.com/tracing/setup_overview/setup/go/?tab=containers#compatibility-requirements -->
| **Go Version** | **Support level** |
|----------------|-------------------------------------|
| 1.19 | [GA](#support-ga) |
| 1.18 | [GA](#support-ga) |
| 1.17 | [GA](#support-ga) |
| 1.16 | [Maintenance](#support-maintenance) |
| 1.17 | [Maintenance](#support-maintenance) |
| 1.16 | [Legacy](#support-legacy) |

* Datadog's Trace Agent >= 5.21.1

Expand Down
2 changes: 1 addition & 1 deletion appsec/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ExampleMonitorParsedHTTPBody() {
}

// Monitor HTTP request parsed body with a framework customized context type
func ExampleMonitorParsedHTTPBody_CustomContext() {
func ExampleMonitorParsedHTTPBody_customContext() {
r := echo.New()
r.Use(echotrace.Middleware())
r.POST("/body", func(c echo.Context) (e error) {
Expand Down
1 change: 1 addition & 0 deletions checkmilestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

//go:build ignore
// +build ignore

// This tool validates that the PR at the given URL has a milestone set.
Expand Down
33 changes: 33 additions & 0 deletions contrib/99designs/gqlgen/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.

// Package gqlgen provides functions to trace the 99designs/gqlgen package (https://github.com/99designs/gqlgen).
package gqlgen_test

import (
"log"
"net/http"

"github.com/99designs/gqlgen/example/todo"
"github.com/99designs/gqlgen/graphql/handler"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

gqlgentrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/99designs/gqlgen"
)

func Example() {
tracer.Start()
defer tracer.Stop()

t := gqlgentrace.NewTracer(
gqlgentrace.WithAnalytics(true),
gqlgentrace.WithServiceName("todo.server"),
)
h := handler.NewDefaultServer(todo.NewExecutableSchema(todo.New()))
h.Use(t)
http.Handle("/query", h)
log.Fatal(http.ListenAndServe(":8080", nil))
}
49 changes: 49 additions & 0 deletions contrib/99designs/gqlgen/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.

package gqlgen

import (
"math"

"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
)

const defaultServiceName = "graphql"

type config struct {
serviceName string
analyticsRate float64
}

// An Option configures the gqlgen integration.
type Option func(t *config)

func defaults(t *config) {
t.serviceName = defaultServiceName
t.analyticsRate = globalconfig.AnalyticsRate()
}

// WithAnalytics enables or disables Trace Analytics for all started spans.
func WithAnalytics(on bool) Option {
if on {
return WithAnalyticsRate(1.0)
}
return WithAnalyticsRate(math.NaN())
}

// WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.
func WithAnalyticsRate(rate float64) Option {
return func(t *config) {
t.analyticsRate = rate
}
}

// WithServiceName sets the given service name for the gqlgen server.
func WithServiceName(name string) Option {
return func(t *config) {
t.serviceName = name
}
}
128 changes: 128 additions & 0 deletions contrib/99designs/gqlgen/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.

// Package gqlgen contains an implementation of a gqlgen tracer, and functions
// to construct and configure the tracer. The tracer can be passed to the gqlgen
// handler (see package github.com/99designs/gqlgen/handler)
//
// Warning: Data obfuscation hasn't been implemented for graphql queries yet,
// any sensitive data in the query will be sent to Datadog as the resource name
// of the span. To ensure no sensitive data is included in your spans, always
// use parameterized graphql queries with sensitive data in variables.
package gqlgen

import (
"context"
"fmt"
"math"
"strings"
"time"

"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

const (
defaultGraphqlOperation = "graphql.request"

readOp = "graphql.read"
parsingOp = "graphql.parse"
validationOp = "graphql.validate"
)

type gqlTracer struct {
cfg *config
}

// NewTracer creates a graphql.HandlerExtension instance that can be used with
// a graphql.handler.Server.
// Options can be passed in for further configuration.
func NewTracer(opts ...Option) graphql.HandlerExtension {
cfg := new(config)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
return &gqlTracer{cfg: cfg}
}

func (t *gqlTracer) ExtensionName() string {
return "DatadogTracing"
}

func (t *gqlTracer) Validate(schema graphql.ExecutableSchema) error {
return nil // unimplemented
}

func (t *gqlTracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
opts := []ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeGraphQL),
tracer.ServiceName(t.cfg.serviceName),
}
if !math.IsNaN(t.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, t.cfg.analyticsRate))
}
var (
octx *graphql.OperationContext
)
name := defaultGraphqlOperation
if graphql.HasOperationContext(ctx) {
// Variables in the operation will be left out of the tags
// until obfuscation is implemented in the agent.
octx = graphql.GetOperationContext(ctx)
if octx.Operation != nil {
if octx.Operation.Operation == ast.Subscription {
// These are long running queries for a subscription,
// remaining open indefinitely until a subscription ends.
// Return early and do not create these spans.
return next(ctx)
}
name = fmt.Sprintf("%s.%s", ext.SpanTypeGraphQL, octx.Operation.Operation)
}
if octx.RawQuery != "" {
opts = append(opts, tracer.ResourceName(octx.RawQuery))
}
opts = append(opts, tracer.StartTime(octx.Stats.OperationStart))
}
var span ddtrace.Span
span, ctx = tracer.StartSpanFromContext(ctx, name, opts...)
defer func() {
var errs []string
for _, err := range graphql.GetErrors(ctx) {
errs = append(errs, err.Message)
}
var err error
if len(errs) > 0 {
err = fmt.Errorf(strings.Join(errs, ", "))
}
span.Finish(tracer.WithError(err))
}()

if octx != nil {
// Create child spans based on the stats in the operation context.
createChildSpan := func(name string, start, finish time.Time) {
var childOpts []ddtrace.StartSpanOption
childOpts = append(childOpts, tracer.StartTime(start))
childOpts = append(childOpts, tracer.ResourceName(name))
var childSpan ddtrace.Span
childSpan, _ = tracer.StartSpanFromContext(ctx, name, childOpts...)
childSpan.Finish(tracer.FinishTime(finish))
}
createChildSpan(readOp, octx.Stats.Read.Start, octx.Stats.Read.End)
createChildSpan(parsingOp, octx.Stats.Parsing.Start, octx.Stats.Parsing.End)
createChildSpan(validationOp, octx.Stats.Validation.Start, octx.Stats.Validation.End)
}
return next(ctx)
}

// Ensure all of these interfaces are implemented.
var _ interface {
graphql.HandlerExtension
graphql.ResponseInterceptor
} = &gqlTracer{}
Loading

0 comments on commit 2847535

Please sign in to comment.