1// Unless explicitly stated otherwise all files in this repository are licensed
2// under the Apache License Version 2.0.
3// This product includes software developed at Datadog (https://www.datadoghq.com/).
4// Copyright 2016 Datadog, Inc.
5
6package mgo
7
8import (
9	"context"
10	"math"
11
12	"gopkg.in/DataDog/dd-trace-go.v1/internal"
13)
14
15type mongoConfig struct {
16	ctx           context.Context
17	serviceName   string
18	analyticsRate float64
19}
20
21func newConfig() *mongoConfig {
22	rate := math.NaN()
23	if internal.BoolEnv("DD_TRACE_GIN_ANALYTICS_ENABLED", false) {
24		rate = 1.0
25	}
26	return &mongoConfig{
27		serviceName: "mongodb",
28		ctx:         context.Background(),
29		// analyticsRate: globalconfig.AnalyticsRate(),
30		analyticsRate: rate,
31	}
32}
33
34// DialOption represents an option that can be passed to Dial
35type DialOption func(*mongoConfig)
36
37// WithServiceName sets the service name for a given MongoDB context.
38func WithServiceName(name string) DialOption {
39	return func(cfg *mongoConfig) {
40		cfg.serviceName = name
41	}
42}
43
44// WithContext sets the context.
45func WithContext(ctx context.Context) DialOption {
46	return func(cfg *mongoConfig) {
47		cfg.ctx = ctx
48	}
49}
50
51// WithAnalytics enables Trace Analytics for all started spans.
52func WithAnalytics(on bool) DialOption {
53	return func(cfg *mongoConfig) {
54		if on {
55			cfg.analyticsRate = 1.0
56		} else {
57			cfg.analyticsRate = math.NaN()
58		}
59	}
60}
61
62// WithAnalyticsRate sets the sampling rate for Trace Analytics events
63// correlated to started spans.
64func WithAnalyticsRate(rate float64) DialOption {
65	return func(cfg *mongoConfig) {
66		if rate >= 0.0 && rate <= 1.0 {
67			cfg.analyticsRate = rate
68		} else {
69			cfg.analyticsRate = math.NaN()
70		}
71	}
72}
73